Tag Archives: Apex

Test Setup Method in Salesforce Apex

In Spring ’15 release Salesforce has introduced new @testSetup annotation for Test class method. @testSetup annotation defined methods are used for creating common test records, and that records are available for all test methods in the class.

@testSetup methods create common test data easily and efficiently, and reduce test execution times especially when we are working with many records.

Example:

@iSTest
private class AccountControllerTest {
    
    //Test Setup Method
    @testsetup
    static void createAccount() {
        Account acc = new Account();
        acc.Name = 'Salesforce';
        insert acc;
    }
    
    //Test Method
    public static testMethod void test() {
        System.assertEquals(1, [SELECT COUNT() FROM Account Where Name = 'Salesforce']);
    }
}

Considerations:

  • Records created by @testSetup method are available to all test methods in the test class.
  • @testSetup methods are executed first in the test class before any test methods.
  • One Test class can have only one @testSetup method. If multiple @testSetup method is written in a Test class then sequence of execution of those methods are not guaranteed.
  • If the test class or a test method has access to organization data by using the @isTest(SeeAllData=true) annotation, test setup methods aren’t supported in this class.
  • If any error occurss during the execution in @testSetup method, then entire test class fails.
  • The data created by @testSetup methods will be rolled back after the completly execution of the test class.
  • If a test setup method calls a non-test method of another class, no code coverage is calculated for the non-test method.

Invocable Method in Salesforce

We use @InvocableMethod annotation, whenever we want to invoke an apex method from the Process Builder then that method must be declared as @InvocableMethod, otherwise it can’t be accessed in Process Builder.

Invocable Annotation Method Example:

public class AccountAction {
    @InvocableMethod(label='Update Account' description='Update the list of accounts to the specified account IDs.')
    public static void updateAccounts(List<Id> ids) {
       
    }
}

Use Cases of Invocable Method:

  • The Invocable method must be static and public or global, and its class must be an outer class.
  • Only one method in a class can have the InvocableMethod annotation.
  • Triggers can’t reference Invocable methods.
  • Invocable method will not accept more than one argument as a method parameter
    Other annotations can’t be used with the InvocableMethod annotation.

Inputs and Outputs of Invocable Method:
There can be at most one input parameter and its data type must be one of the following:

  • A list of a primitive data type or a list of lists of a primitive data type – the generic Object type is not supported.
  • A list of an sObject type or a list of lists of an sObject type – the generic sObject type is not supported.
  • A list of a user-defined type, containing variables of the supported types and with the InvocableVariable annotation. Create a custom global or public Apex class to implement your data type, and make sure your class contains at least one member variable with the invocable variable annotation.
  • If the return type is not Null, the data type returned by the method must be one of the following:
    • A list of a primitive data type or a list of lists of a primitive data type – the generic Object type is not supported.
    • A list of an sObject type or a list of lists of an sObject type – the generic sObject type is not supported.
    • A list of a user-defined type, containing variables of the supported types and with the InvocableVariable annotation. Create a custom global or public Apex class to implement your data type, and make sure your class contains at least one member variable with the invocable variable annotation.

Invocable Methods in Managed Packages:

  • We can use InvocableMethod in packages, but once we add an Invocable method, we can’t remove it from later versions of the package.
  • Public Invocable methods can be referred to by flows and processes within the managed package.
  • Global Invocable methods can be referred to anywhere in the subscriber org. Only global Invocable methods appear in the Cloud Flow Designer and Process Builder in the subscriber org.