0

I am not able to convert the JSON string into List of sObject.

Lightning Controller:

createOwner : function(component, event, helper) {
    console.log('$$$$$$Test: '+ component.get("v.ownerList"));
    var action = component.get("c.createAdditionalOwner");
    var ARLIlistAsString = JSON.stringify(component.get("v.ownerList"));
    action.setParams({
        "newOwnerList": ARLIlistAsString

    });
    action.setCallback(this, function(response){
        var state = response.getState();
        if (component.isValid() && state === "SUCCESS") {
            console.log('$$$$$Check: '+ response.getReturnValue());
        }
    });
    $A.enqueueAction(action);
}

Apex Class:

@AuraEnabled
public static String createAdditionalOwner(String newOwnerList){
    System.debug('$$$$$Check: '+ newOwnerList);
    List<Visit_Owner_Institution__c> newOwnerList2 = (List<Visit_Owner_Institution__c>)JSON.deserialize(newOwnerList, Visit_Owner_Institution__c.class);
    System.debug('$$$$$Check: '+ newOwnerList2);
    return 'Success';

}

Component:

<aura:attribute name="ownerList" type="Visit_Owner_Institution__c[]"/>
1
  • You want to deserialize it as a list, not an individual object, right? Make sure you are only using the List class, not the object class. Jul 13, 2017 at 11:54

1 Answer 1

2

You're trying to convert a list into a single record, then back in to a list:

List<Visit_Owner_Institution__c> newOwnerList2 = (List<Visit_Owner_Institution__c>)
    JSON.deserialize(newOwnerList, Visit_Owner_Institution__c.class);

Generally speaking, when using JSON.deserialize, you want to use the following format:

T result = (T)JSON.deserialize(jsonString, T.class);

Where T is a specific type of class. In your code, however, T is correct in the first two places, but not in the third. All three T values should be exactly List<Visit_Owner_Institution__c>.


List<Visit_Owner_Institution__c> newOwnerList2 = (List<Visit_Owner_Institution__c>)
    JSON.deserialize(newOwnerList, List<Visit_Owner_Institution__c>.class);

You must log in to answer this question.

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