Apex Remote Call from Vlocity OmniScript and Vlocity Card
Vlocity Provides power tools like Dataraptor to Query, Insert, and Update the Records, however, if we would need to perform any business logic check or any complex logic we need to call the apex class.
Apex Remote call from Vlocity OmniScript -
Requirement -
As a part of Product Service, Agent will capture the customer details and purchased product details. The ticket will be created in the background and will be assigned to a service queue-based problem statement.
Here, we are trying to create an Omniscript using that agent can log tickets as a part of the product service setup.
Basic Setup -
As a part of the basic setup, I have created a few input text boxes to take inputs after that, I have placed them on Remote Action to call the apex methods -
Remote Action Setup-
In order to call the Apex Remote functions, we would need to drag the Remote Action from the Available Components.
Remote Action is available on the left side tab views. Please refer to the below snapshot -
As a part of Remote Action Configuration, we need to mention the Element Name, Remote Class, Remote Method.
Remote Class: <Mention the Apex Class Name>
Remote Method: <Metion the Method Name>
Parameters Configuration:
As a part of Remote Action, we can pass values to controller in the below two ways. I will mention more details on these in the "How to Debug OmniScript Remote Issue".
- All the OmniScript JSON nodes will be passed to method. We can use Input Map in order to access those with Step Names, we need to JSON parsing in that case to read the values
- If we would need to read few input fields/ JSON nodes, we can follow below approach -
Apex Class: Sharing the Sample code below
In the VlocityOpenInterface interface we have method call invokeMethod.
We would need to write our custom logic here
- First String Parameter will hold the method name, the same value that we have mentioned while Remote Action configuration
- Input Map will return us all the JSON nodes from OmniScript and Extra Payload details from Configurations
- Output Map will hold the return data from the Custom Method. After performing custom Logic,
- if we want to pass anything to controller, we need to use the option also
/* * Class: CreateContactController * Descriptions: In order to call the Apex Class from the OmniScript * and Vlocity Card, we need to implement vlocity_cmt.VlocityOpenInterface interface * */ global class CreateContactController implements vlocity_cmt.VlocityOpenInterface { /* * Method Name: invokeMethod * Return Type: Boolean * Input Parameters: String, Map<String,Object>, Map<String,Object>, Map<String,Object> * Descriptions: In the VlocityOpenInterface interface we have method call invokeMethod. * We would need to write our custom logic here * Input Map will return us all the JSON nodes from OmniScript and Extra Payload details from Configurations * Output Map will hold the return data from the Custom Method. After performing custom Logic, * if we want to pass anything to controller, we need to use the output * */ global Boolean invokeMethod(String methodName, Map<String,Object> input, Map<String,Object> output, Map<String,Object> option){ System.debug('***input**** ' + input); //MethodName Will be same as the value that we have mentioned while calling the Remote Action if(methodName == 'CreateContact'){ //Creating Instance of Contact Contact contactToCreate = new Contact(); contactToCreate.FirstName = getFieldValue(input,'fName'); contactToCreate.LastName = getFieldValue(input,'lName'); contactToCreate.Email = getFieldValue(input,'email'); contactToCreate.Phone = getFieldValue(input,'phone'); //Checking the Profile Accessiblity to Create the Contact SObjectAccessDecision decision = Security.stripInaccessible( AccessType.CREATABLE, new List<Contact>{contactToCreate}); if(!decision.getRecords().isEmpty()){ insert decision.getRecords(); output.put('contactId',decision.getRecords()[0].Id); } } return true; } /* * Method Name: getFieldValue * Parameter: Map<String,Object>, String * Return Type: String * Description: It will return the value from Map after reading the value */ private String getFieldValue(Map<String,Object> input, String key){ if(input.containsKey(key)){ return String.valueOf(input.get(key)); } return ''; } }
How to Read Returned value from Apex Method:
In order to read the values, we can refer %NodeName%. While creating of the outmap, the node name we will be mentioning here, we need to mention the same
Configuration from the OS
No comments:
Post a Comment