0

I need to build a json for external rest service(non salesforce platform) dynamically according to mapping stored in a custom metadata object (named SobjectToJSONFieldsMapping__mdt) records(i.e. including custom fields as :sobject_field__c, Json_field__c, sobject_type__c, XXX_type__c(external platform), etc.). i have done it before with JSON object having only one level of key-value pairs, in this case i have more for example:

   {
    "entity": { "externalId": "{{customerExternalId}}" },
    "item": {
        "items": [{
            "item": { "externalId": "{{itemExternalId}}" },
            "rate": 60
        }]
    }
}

it has to be totaly dynamic(not hard coded). In the example above I would have a record that has

record1:

sobject_field__c = 'Opportunity', 
Json_field__c = 'item'(outer one),  

record2:

sobject_field__c = 'AccountId', 
Json_field__c = 'externalId'  

In order to distinguish between the outer 'item' and 'inner', I have added another custom field in SobjectToJSONFieldsMapping__mdt named parents_scheme__c which contains a string of comma separated parents(from inner to outer in order to treat them with apex and build JSON respectively) e.g. for inner 'item' key (Json_field__c = 'item') parents_scheme__c would have 'items,item'.

Anyone have a better idea?

3
  • You could have your sobject_field__c be a string and allow this to be a path, like "Account.Name". Similarly allow your json_field__c to be a JSON path, like "account.name". You need to make sure you're smart about translating bundles of field paths that actually identify related lists into arrays in the output JSON, but that just needs some smarts in your code to detect and handle.
    – Phil W
    Jun 25, 2021 at 10:52
  • For identifying that i have a picklist field called type_in_json__c which holds one of the following values: field,object,array.
    – DavidS
    Jun 25, 2021 at 13:42
  • That's an option, but I don't think it fits with making it fully dynamic IMHO, since you need to always map to the SObject structure which itself would tell you it is a field or (related list) array.
    – Phil W
    Jun 25, 2021 at 13:44

0

You must log in to answer this question.

Browse other questions tagged .