On this post, we are going to learn how to use Apex Remote as a data source and pass the recordId from the FlexCard.
Earlier I wrote a post on how to use Apex Remote using FlexCards but that didn’t cover how you can pass the recordId. If you want to read that article you can check it here.
Creating the Apex Class
The Apex Class needs to implements the VlocityOpenInterface.
Class needs to global and you need to use the OmniStudio namespace.
global with sharing class FlexCardAccountRemoteClass implements vlocity_ins.VlocityOpenInterface
The default implementation requires the method invokeMethod with one string methodName parameter and three map parameters. These would be coming from the FlexCard.
global Boolean invokeMethod(String methodName, Map<String,Object> inputMap, Map<String,Object> outMap, Map<String,Object> options)
Next, let’s implement our logic.
- Same class can be used for multiple functionalities by segrating to different methods
- Call a private function and pass the input and output maps
- If we are passing recordID, get the map value for that ID and cast from Object to Id value
- Then use it in our query
- Finally assign the output of the query to the output map
global Boolean invokeMethod(String methodName, Map<String,Object> inputMap, Map<String,Object> outMap, Map<String,Object> options) {
Boolean result = false;
try {
// check methodName
if(methodName == 'getAccountRecords') {
getAccountRecords(inputMap, outMap);
result = true;
}
} catch(Exception e) {
system.debug('The error is ' + e.getMessage());
}
return result;
}
private void getAccountRecords(Map<String,Object> inputMap, Map<String,Object> outMap) {
// cast the object to Id
Id accountId = (Id) inputMap.get('recordId');
List<Account> accountList = [SELECT Id, Name, Website, Phone FROM Account WHERE Id =: accountId];
outMap.put('account', accountList);
}
Configure the Data Source in the FlexCard
Create a new FlexCard and select Apex Remote from the Data Source Type and fill in the details
- Remote Class = getAccountRecords
- Remote Method = getAccountRecords
- Input Map
- recordId
- %recorId%
- Result JSON Path = [“account”]
If you can also go to the Setup tab and configure the Data Source after.
Display the Information in the FlexCard
Go back to the Canvas and add Text field, populate the data from the fields.
Preview the record and add Test Parameter with recordId = AccountId.
There you go, hope you found this basic tutorial useful. Hit the comments below.
Source code available in github here.