Tag Archives: Future Method

Invoke Future Method From Process Builder

Sample Code:

public class ContactProcessBuilderHandler {
    
    @InvocableMethod 
    public static void sendNotification(List<String> contactIds) {
        sendEmailToContacts(contactIds);
    }
    
    @future //Use callout = true, for callouts
    public static void sendEmailToContacts(List<String> contactIds) {
        //Write your business logic
    }
}

Invoke Apex Callout From Process Builder

Sample Code:

public class ContactProcessBuilderHandler {
    
    @InvocableMethod 
    public static void sendContacts(List<Contact> conList) {
        string jsonData = JSON.serialize(conList);
        sendContactsToOracle(jsonData);
    }
    
    @future(callout = true)
    public static void sendContactsToOracle(string jsonData) {
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        req.setEndpoint('https://your endpoint url');
        req.setMethod('POST');
        req.setHeader('Authorization', 'Authorization Header');
        req.setHeader('Content-Type', 'application/json');
        req.setBody(jsonData);
        req.setCompressed(true); 
		res = http.send(req);
    }
}

Test Class For Future Method

Future Method:

public class AccountHelper {
	@future
	public static void UpdateAccounts(List<Id> accountIds){
		//Get those records based on the IDs
		List<Account> accList = [SELECT Name FROM Account WHERE Id IN : accountIds];
		//Process records
	}
}

Test Class:

@isTest
public class TestAccountHelper {
    
    private static User CreateUser(){
        String orgId = UserInfo.getOrganizationId();
        String standardUserProfileId = [SELECT Id From Profile WHERE Name = 'Standard User'].Id;
        User u = new User(
            FirstName = 'Test',
            LastName = 'User',
            Alias = 'TestUser', 
            profileId = standardUserProfileId ,
            Email = orgId + '@test.org',
            Username = orgId + '@test.org',
            EmailEncodingKey = 'ISO-8859-1',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_US',  
            TimeZoneSidKey = 'America/Los_Angeles'
        );
        insert u;
        return u;
    }
    
    private static Account CreateAccount(){
        Account acc = new Account( Name = 'Test Account', 
                                  Type = 'Technology Partner', 
                                  Industry = 'Technology',
                                  Phone = '9898989898'
                                 );
        insert acc;
        return acc;
    }
    
    private static testMethod void testUpdateAccounts(){
        
        User u = CreateUser();
        System.runAs(u) {
            Account acc = CreateAccount();
            List<Id> accountIds = new List<Id>();
            accountIds.add(acc.Id);
            
            Test.startTest();
            AccountHelper.UpdateAccounts(accountIds);
            Test.stopTest();
        }
        //Verify account is inserted
        List<Account> accList = [SELECT Id From Account WHERE Name = 'Test Account'];
        System.assertEquals(1, accList.size());
    }
}