0

Following is the JSON structure. This is the JSON structure

I want to deserialize this data structure in APEX to Update the Records, sobject is dynamic not every time 'Contact', I am confused how deserialize this data structure to Update the record Just for more info here is the Js function.

   addRelatedRecord:function(cmp,evt,help){
    let arrofRecs = [],Ids = cmp.get('v.selectedIds'),objName = cmp.get('v.objectName'),
    relatedApiName = cmp.get('v.RelatedListFieldAPI'),recID = cmp.get('v.recordId');
    Ids.map(val =>{
        let obj = {};
        obj['SobjectType'] = objName;
        obj[relatedApiName] = recID;
        obj['Id'] = val;
        arrofRecs.push(obj);
    })
    console.log(arrofRecs)
    //------------------------------------
    let methodName = 'c.updateRecord',
        params = {
            'json':JSON.stringify(arrofRecs)
        },
        callbackRess = (response) => {
            if (response) {
                let data = JSON.parse(response)[0];
                cmp.set('v.NonRelateddata',data);
                console.log('data', data);
            }
        }
    console.log(params)
    help.callApexMethod(cmp, methodName, params, callbackRess);
}
1
  • What could be other SObjectType here? You can pass this information along with in your apex method and utilize switch/if-else to de-serialize the JSON based on that information. – Jayant Das Apr 14 '19 at 16:10
1

This is easy. Just pass attributes as list of maps, and in Apex just do

(List<Contact>) JSON.deserialize(JSON.serialize(records), List<Contact>.class)

or

(List<SObject>) JSON.deserialize(JSON.serialize(records), List<SObject>.class)

The method signature would be the following.

@AuraEnabled
public static void method(List<Map<String, Object>> records) {
5
  • getting error :Method does not exist or incorrect signature: void serialize(List<Map<String,Object>>) from the type List<Map<String,Object>> – Sarvesh Apr 14 '19 at 12:42
  • You missed JSON name of the class, you shouldn't call serialize(records) but JSON.serialize(records) – Patlatus Apr 14 '19 at 12:43
  • Here is the method i am try to using.... @AuraEnabled public static string updateRecord(List<Map<String, Object>> json){ System.debug('json'+json); List<SObject> lst = (List<SObject>) JSON.deserialize(JSON.serialize(json), List<SObject>.class); System.debug('lst'+lst); return 'Done'; } – Sarvesh Apr 14 '19 at 12:45
  • rename json to records or to something else, don't use standard class names like JSON, Test etc as the name for variable or class. – Patlatus Apr 14 '19 at 12:53
  • Sarvesh kumar, please avoid extended discussions, either update your post with the problem or create a new one – glls Apr 14 '19 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.