您在此处:
Omnistudio 数据映射器从 Apex 调用
从 Summer’25 开始,要从 Apex 调用数据映射器,请调用 ConnectApi.OmniDesignerConnect.executeDataMapper(bundleName, apexInput) Connect API,指定数据映射器的名称及其所需的输入数据。此 API 会替换 vlocity_ins.DRGlobal.processObjectsJSON() 方法。与之前的方法相比,该连接 API 消除了对受管软件包的依赖,并为 Apex 类中的数据映射器调用提供了高达 60% 的性能提高。
按如下方式指定输入:
-
对于 JSON 对象,使用
Map<String, Object>。将字符串设置为 JSON 键,将对象设置为 JSON 值。 -
对于 JSON 对象的数组,使用
List<Map<String, Object>>。将字符串设置为 JSON 键,将对象设置为 JSON 值。 -
或者,您可以将输入指定为包含数据映射器所需的 JSON 输入的字符串。
有关 DRGlobal 类方法的详细信息,该类提供多种调用数据映射器的方法,请参见 DRGlobal 类和方法。
数据映射器提取或转换示例
要在 Apex 中处理数据映射器提取或转换返回的结果,请将输出反序列化到Map<String, Object>或List<Map<String,
Object>>。
/* Specify Data Mapper extract or transform to call */
String bundleName = 'DataMapperName';
/* Populate the input JSON */
Map<String, Object> objectList = new Map<String, Object>{'MyKey'=>'MyValue'};
/* Call the Data Mapper */
String jsonString = JSON.serialize(objectList);
List<String> jsonInputData = new List<String>();
jsonInputData.add(jsonString);
ConnectApi.DataMapperExecuteInputRepresentation apexInput = new ConnectApi.DataMapperExecuteInputRepresentation();
apexInput.dataMapperInput = jsonInputData;
apexInput.inputType = 'JSON';
ConnectApi.DataMapperExecuteOptionsRepresentation options = new ConnectApi.DataMapperExecuteOptionsRepresentation();
options.locale = null;
options.shouldSendLegacyResponse = true;
apexInput.options = options;
ConnectApi.DataMapperExecuteOutputRepresentation output = ConnectApi.OmniDesignerConnect.executeDataMapper(bundleName, apexInput);
/* Process the results returned by a Data Mapper Extract or Transform */
List<String> innerResponse = output.response;
for (String currentResponse : innerResponse){
Map<String, Object> outerMap = (Map<String, Object>) JSON.deserializeUntyped(currentResponse);
List<String> keys = new List<String>(outerMap.keySet());
System.debug(outerMap.get('response'));
}数据映射器加载示例
数据映射器加载会返回 JSON,其中包含有关操作创建或更新的 Salesforce 对象的数据。要处理数据映射器加载返回的结果,请将输出反序列化为映射。从生成的地图中,您可以提取有关创建的对象和发生的任何错误的数据。下面的数据映射器加载示例显示了如何从结果映射访问这些数据。
String objectList = '{"accountName":"Vlocity", "contractCode":"SKS9181"}';
List<String> jsonInputData = new List<String>();
jsonInputData.add(objectList);
ConnectApi.DataMapperExecuteInputRepresentation apexInput = new ConnectApi.DataMapperExecuteInputRepresentation();
apexInput.dataMapperInput = jsonInputData;
apexInput.inputType = 'JSON';
ConnectApi.DataMapperExecuteOptionsRepresentation options = new ConnectApi.DataMapperExecuteOptionsRepresentation();
options.ignoreCache = false;
options.shouldSendLegacyResponse = true;
apexInput.options = options;
ConnectApi.DataMapperExecuteOutputRepresentation output =ConnectApi.OmniDesignerConnect.executeDataMapper(bundleName, apexInput);
List<String> innerResponse = output.response;
String currentResponse = innerResponse[0];
System.debug(currentResponse);
Map<String, Object> outerMap = (Map<String, Object>) JSON.deserializeUntyped(currentResponse);
List<String> keys = new List<String>(outerMap.keySet());
System.debug(outerMap.get('drSObjectResults'));
/*
Process the results of the load: these methods return details about objects affected by the Data Mapper Load, in addition to any errors that occured
*/
Map<String, Object> createdObjectsByType = (Map<String, Object>)resultMap.get('createdObjectsByType');
Map<String, Object> createdObjectsByTypeForBundle = (Map<String, Object>)createdObjectsByType.get('bundleName');
Map<String, Object> createdObjectsByOrder = (Map<String, Object>)resultMap.get('createdObjectsByOrder');
Map<String, Object> errors = (Map<String, Object>)resultMap.get('errors');
Map<String, Object> errorsByField = (Map<String, Object>)resultMap.get('errorsByField');
List<Object> errorsAsJson = (List<Object>)outerMap.get('errorsAsJson'); // Returns input JSON plus per-node errors带有 bulkUpload 参数的数据映射器加载示例
此示例代码将 bulkUpload 参数传递给数据映射器加载。
String objectList = '[{"ProductCode__c": 11050665},{"ProductCode__c": 11070100}]'; // replace this with the input for your Data Mapper Load
String bundleName = 'DRLoadPrice'; // replace this with your Data Mapper name
Map<String,Object> bodyData = new Map<String,Object>();
bodyData.put('bundleName',bundleName);
bodyData.put('objectList',objectList);
List<String> jsonInputData = new List<String>();
jsonInputData.add(bodyData.get('objectList'));
ConnectApi.DataMapperExecuteInputRepresentation apexInput = new ConnectApi.DataMapperExecuteInputRepresentation();
apexInput.dataMapperInput = jsonInputData;
apexInput.inputType = 'JSON';
ConnectApi.DataMapperExecuteOptionsRepresentation options = new ConnectApi.DataMapperExecuteOptionsRepresentation();
options.ignoreCache = false;
apexInput.options = options;
options.shouldSendLegacyResponse = true;
ConnectApi.DataMapperExecuteOutputRepresentation output = ConnectApi.OmniDesignerConnect.executeDataMapper(bodyData.get('bundleName'), apexInput);
List<String> innerResponse = output.response;
Map<String, Object> outerMap = (Map<String, Object>) JSON.deserializeUntyped(innerResponse[0]);
List<String> keys = new List<String>(outerMap.keySet());
System.debug(outerMap.get('drSObjectResults'));
