1

I am sending object data ({FirstName=Test, Id=0030m00000WyQ6ZAAV}, {FirstName=Arya, Id=0030m00000YF2SBAA1}) to the Apex method like below

@AuraEnabled
public static List<MockResponse> updateContacts(Object data,String objName) {
    // {FirstName=Lalith, Id=0030m00000WyQ6ZAAV}, {FirstName=Arya, Id=0030m00000YF2SBAA1}
    List<Contact> contactsForUpdate = (List<Contact>) JSON.deserialize(
            JSON.serialize(data),
            List<Contact>.class
    );
    system.debug('contactsForUpdate'+contactsForUpdate);
    try {
        Database.SaveResult[] srList = Database.update(contactsForUpdate, false);
        List<MockResponse> errorResult = new List<MockResponse>();
        Map<String,String> errorData = new Map<String,String>();
        errorData.put('Id','Message');
        for(Integer i=0;i<srList.size();i++){
            if (srList.get(i).isSuccess()){
                srList.get(i).getId();
                system.debug('I came here !!');

            }else if (!srList.get(i).isSuccess()){
                // DML operation failed
                Database.Error error = srList.get(i).getErrors().get(0);
                String failedDML = error.getMessage();
                contactsForUpdate.get(i);
                system.debug('Failed ID'+contactsForUpdate.get(i).Id);
                MockResponse mr = new MockResponse();
                mr.Id = String.valueOf(contactsForUpdate.get(i).Id);
                mr.errorMessage = failedDML;
                errorResult.add(mr);
            }
        }
        System.debug('errorData'+errorResult);
        return errorResult;

    }
    catch (Exception e) {
        system.debug('exception occ'+e.getMessage());
        return null;
    }
}

Right now, I am deserializling the object like below

List<Contact> contactsForUpdate = (List<Contact>) JSON.deserialize(
    JSON.serialize(data),List<Contact>.class
);

but,I want to do it for any object data passed like contact,Account etc. Please help me out here I am passing object name as other parameter but unable to deserialize.

6
  • 1
    Did you try List<SObject>.class?
    – Phil W
    Commented Nov 25, 2020 at 10:48
  • tried below 1. SObject customObject = (SObject)JSON.deserialize(JSON.serialize(data), Sobject.class); System.debug('customObject '+ customObject); ERROR : System.JSONException: Json Deserialization failed on token 'null' and has left off in the middle of parsing a row. Will go to end of row to begin parsing the next row
    – Bibbi
    Commented Nov 25, 2020 at 11:06
  • 2. List<Object> results = (List<Object>) JSON.deserializeUntyped(JSON.serialize(data)); system.debug('results'+results); response : results({FirstName=Lalith30, Id=0030m00000WyQ6ZAAV})
    – Bibbi
    Commented Nov 25, 2020 at 11:10
  • I said List<SObject> not List<Object>.
    – Phil W
    Commented Nov 25, 2020 at 12:06
  • yeah tried with sobject one also below is the code List<SObject> results = (List<SObject>)JSON.deserialize(JSON.serialize(data), List<SObject>.class); System.debug('results'+results); Error: System.JSONException: Nested object for polymorphic foreign key must have an attributes field before any other fields.
    – Bibbi
    Commented Nov 25, 2020 at 13:29

1 Answer 1

1

Aura/LWC supports direct serialization/deserialization of objects. Just use:

@AuraEnabled
public static MockResponse updateRecords(sObject[] records) {

As for the client side, make sure there's an attributes property:

[ { "attributes": { "type": "Contact" }, "FirstName": "John", ... }, ... ]

Coincidentally, if you want to pass in the type, you could use Type.forName, but the syntax is a bit annoying to get just right sometimes.

@AuraEnabled
public static List<MockResponse> updateContacts(Object data,String objName) {
    // {{attributes={type=Contact}, FirstName=Lalith, Id=0030m00000WyQ6ZAAV}, {{attributes={type=Contact}, FirstName=Arya, Id=0030m00000YF2SBAA1}
    List<sObject> contactsForUpdate = (List<sObject>) JSON.deserialize(
            JSON.serialize(data),
            Type.forName('List<'+objName+'>')
    );

But seriously, there's no need to serialize/deserialize data yourself, just let the platform do what it was designed to do.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .