Vlocity/Omnistudio

Call Remote Action From Apex Class (Vlocity)

A Remote action in Vlocity/OmniStudio is essentially an Apex class primarily utilized within OmniScripts and Integration Procedures.

However, in this example, we'll demonstrate how to invoke it from another Apex class. This method is versatile and can be employed in various Apex code contexts, including within the Developer Console.

Call Remote Action From Apex Class

Remote Action

Before proceeding, it's essential to set up a remote action. In this example, we'll create a straightforward scenario in which this method retrieves a list of Salesforce accounts.

global without sharing class RemoteActionExample implements vlocity_ins.VlocityOpenInterface{
    public Boolean invokeMethod(String methodName, Map<String,Object> inputMap, Map<String,Object> outputMap, Map<String,Object> options){
        if (methodName == 'getAccounts'){
            getAccounts(inputMap, outputMap, options);
        }
        return true;
    }

    public static void getAccounts(Map<String,Object> inputMap, Map<String,Object> outputMap, Map<String, Object> options){
        // IMPLEMENT YOUR LOGIC HERE

        Id accountId = Id.valueOf((String)inputMap.get('recordId'));
        Account acc = [SELECT Id, Name, Industry FROM Account WHERE Id =: accountId];
        outputMap.put('account', acc);
    }
}

Call it using Apex Code

After creating the remote action, the next step is to execute it using Apex code. Ensure that you provide the same parameters as those defined in your remote action method. Below is an illustrative example.

public static final RemoteActionExample raClass = new RemoteActionExample();
Map<String, Object> input = new Map<String, Object>();
Map<String, Object> output = new Map<String, Object>();
Map<String, Object> options = new Map<String, Object>();
input.put('recordId', '001K000000Asi5QAC');

raClass.invokeMethod('getAccounts', input, output, options);
system.debug(output) // RESPONSE OF THE REMOTE ACTION