usuresh | 2021-10-07 13:59:50 UTC | #1
Hi, Could you please help to parse to get my data action return array of objects: Below are my input and output contracts: Input:
{ "title": "Phone Number Request", "type": "object", "properties": { "PHONE_NUMBER": { "type": "string" } }, "additionalProperties": true }
Output : { "type": "object", "required": [ "searchRecords" ], "properties": { "searchRecords": { "type": "array", "items": { "title": "Customer", "type": "object", "properties": { "Id": { "type": "string" }, "CLSPreferredLanguage__c": { "type": "string" } }, "additionalProperties": true } } }, "additionalProperties": true }
My Sample response will be:
{ "searchRecords": [ { "attributes": { "type": "Contact" }, "Id": "1213456789", "WWID_c": "1234", "CLSPreferredLanguage_c": "English", "EmployeeStatus_c": "Active" }, { "attributes": { "type": "Contact" }, "Id": "1112233445", "WWID_c": "12457", "CLSPreferredLanguage_c": "English", "EmployeeStatus_c": "Active" } ] }
jacobshaw | 2021-10-07 19:03:33 UTC | #2
Hi usuresh Could you provide the API endpoint that this action is hitting? Also is it the searchRecords that you want an array for? I see that the highest level of the response is of type object. If you want an array at the top level, it would need to be of type array at the top level.
usuresh | 2021-10-07 19:52:31 UTC | #3
Hi Jacob,
Thanks for checking this. This is my end point salesforce API:
/services/data/v37.0/search/?q=$esc.url("FIND {$salesforce.escReserved(${input.PHONENUMBER}))} IN PHONE FIELDS RETURNING Contact(Id, WWID_C, CLSPreferredLanguage_c, EmployeeStatus_c, CLSStatus_c,Account.CLSClassofTrade__c)")
Yes I want searchrecords an array. I am okay if you can move on to top level. I am unable to parse the same as well. Could you please help?
Sample response below: { "searchRecords": [ { "attributes": { "type": "Contact" }, "Id": "1213456789", "WWID_c": "1234", "CLSPreferredLanguage_c": "English", "EmployeeStatus_c": "Active", "Account": { "attributes": { "type": "Account" }, "CLSClassofTrade_c": "1000" } }, { "attributes": { "type": "Contact" }, "Id": "1112233445", "WWID_c": "12457", "CLSPreferredLanguage_c": "English", "EmployeeStatus_c": "Active", "Account": { "attributes": { "type": "Account" }, "CLSClassofTrade_c": "1100" } } ] } Thanks..!!
Regards, Suresh U
Jerome.Saint-Marc | 2021-10-08 15:23:59 UTC | #4
Hello,
A first comment on your request url. I don't have a Salesforce environment to test your exact query with (via a Salesforce Data Action), but I think you have a typo/error in the request url. One ")" that you have to remove. /services/data/v37.0/search/?q=$esc.url("FIND {$salesforce.escReserved(${input.PHONE_NUMBER})} IN PHONE FIELDS RETURNING Contact(Id, WWID__C, CLS_Preferred_Language__c, Employee_Status__c, CLS_Status__c,Account.CLS_Class_of_Trade__c)")
Regarding your Data Action, you can use the following Output Contract:
{ "type": "object", "properties": { "searchRecords": { "type": "array", "items": { "title": "Customer", "type": "object", "properties": { "Id": { "type": "string" }, "CLSPreferredLanguage__c": { "type": "string" } }, "additionalProperties": true } } }, "additionalProperties": true }
with the following Response configuration:
{ "translationMap": {}, "translationMapDefaults": {}, "successTemplate": "${rawResult}" }
When you invoke a Data Action from an Architect flow or from a Script, it is not possible to have an output variable which is an array of objects. Architect only supports array/collection/list of strings, integers/numbers, ... But it does not support array of objects (with a complex structure).
So what is done automatically, in this case is that the result of the Data Action is "flattened".
What it means, with the Output Contract I described above, is that in your Architect flow, you will have 2 output variables. searchRecords.Id will be a collection of strings with the different/extracted Id values. searchRecords.CLSPreferredLanguage_c will be a collection of strings with different/extracted CLSPreferredLanguage_c values.
So this depends on what data/attribute you are trying to extract from the response. I assumed Id and CLSPreferredLanguage__c as this is what you had in your original post (output contract you've included).
Regards,
Jerome.Saint-Marc | 2021-10-08 15:36:58 UTC | #5
If you want to also extract the CLSClassofTrade_c values, the output contract could look like this (same Response configuration than above):
{ "type": "object", "properties": { "searchRecords": { "type": "array", "items": { "title": "Record Item", "type": "object", "properties": { "Id": { "type": "string" }, "WWID_c": { "type": "string" }, "CLSPreferredLanguage_c": { "type": "string" }, "EmployeeStatus_c": { "type": "string" }, "Account": { "type": "object", "properties": { "CLSClassofTrade_c": { "type": "string" } }, "additionalProperties": true } }, "additionalProperties": true } } }, "additionalProperties": true }
Each of the attribute will be mapped with a collection of string (as output parameter of the Data Action).
Regards,
usuresh | 2021-10-08 15:38:51 UTC | #6
Thank you Jerome. Really appreciate your help.
Regards, Suresh U
system | 2021-11-08 15:39:18 UTC | #7
This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.
This post was migrated from the old Developer Forum.
ref: 12289