Vlocity Facts #54 || How to Invoke a DataMapper from LWC in Omnistudio (Vlocity)
Instead of writing custom Apex classes and their associated test classes, developers can use DataMappers within LWCs to handle data operations in a more efficient, declarative way. DataMappers are designed to read, transform, and write Salesforce data without the need for extensive Apex logic. They provide a low-code approach to data integration, which simplifies development and reduces dependency on custom code.
To call a DataMapper from a custom Lightning Web Component (LWC), we use the getDataHandler method provided by Omnistudio’s. This method handles communication with backend services like DataRaptors, Integration Procedures.
import { LightningElement } from "lwc";
import { OmniscriptBaseMixin } from "omnistudio/omniscriptBaseMixin"; //replace omnistudio with vlocity_ins/vlocity_cmt or with your installed package namespace
import util from "omnistudio/utility";
We need to create the request data and input parameters and pass it to getDataHandler method.
// Method to call the DataRaptor callDataRaptor() { // Step 1: Prepare the request object for DataRaptor let drRequest = { type: "DataRaptor", value: { bundleName: "AccountDetailsDR", // Replace with your actual DataRaptor bundle name inputMap: "{}", // Input parameters (to be filled below) optionsMap: "{}" // Optional parameters }, }; // Step 2: Add dynamic input parameters to the inputMap drRequest.value.inputMap = JSON.stringify({ accountId: this.omniJsonData?.accountId, region: this.omniJsonData?.region });
Note: Use the format "this.omniJsonData?.accountId" where accountId is the data we want to pass it from omniscript to lwc. We can pass other details as well not only from omniscript.
We have to stringify the input which we are going to pass into the callDataRaptor() function.
Full code:
import { LightningElement } from "lwc"; import { OmniscriptBaseMixin } from "omnistudio/omniscriptBaseMixin"; //replace omnistudio with vlocity_ins/vlocity_cmt or with your installed package namespace import util from "omnistudio/utility"; export default class DataraptorCallerLwc extends OmniscriptBaseMixin(LightningElement) { // Method to call the DataRaptor callDataRaptor() { // Step 1: Prepare the request object for DataRaptor let drRequest = { type: "DataRaptor", value: { bundleName: "AccountDetailsDR", // Replace with your actual DataRaptor bundle name inputMap: "{}", // Input parameters (to be filled below) optionsMap: "{}"
} }; // Step 2: Add dynamic input parameters to the inputMap drRequest.value.inputMap = JSON.stringify({ accountId: this.omniJsonData?.accountId, region: this.omniJsonData?.region }); // Step 3: Call the DataRaptor using util.getDataHandler util.getDataHandler(JSON.stringify(drRequest)) .then((response) => { const parsedResponse = JSON.parse(response); console.log("DataRaptor Response:", parsedResponse); // Step 4: Optionally load the response data into Omniscript using omniApplyCallResp this.omniApplyCallResp({ accountDetails: parsedResponse }); }) .catch((error) => { console.error("DataRaptor Call Error:", error); }); } }
In this way we can call DataRaptor from LWC.
Connect with me on LinkedIn for more updates/scenarios related to Omnistudio.
Comments
Post a Comment