function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Francesco Carmagnola 10Francesco Carmagnola 10 

Pass a list of objects from Lightning Component to Apex Controller

I am passing a list of Object from my Lightning Component to the Apex Controller. The variable is called "fieldList" and is declared as parameter. If I run this code, just for debug pourpose:
 
@AuraEnabled
public static SObject[] getExistingRecords(SObject item, Object[] fieldList, String[] parameterList){
    system.debug('@@fieldList: '+fieldList);
    for(Object field: fieldList){
         system.debug('@@field: '+field);
    }
}

I obtain this debug LOG:

11:50:58:000 VARIABLE_ASSIGNMENT [56]|fieldList|{"s":1,"v":[{"name":"FirstName","type":"inputField","required":true},{"name":"LastName","type":"inputField","required":true},{"name":"Phone","type":"inputField","required":true},{"name":"Email","type":"inputField"}]}|0x5c6aaa7e

11:50:58:001 USER_DEBUG [57]|DEBUG|@@fieldList: ({name=FirstName, type=inputField, required=true}, {name=LastName, type=inputField, required=true}, {name=Phone, type=inputField, required=true}, {name=Email, type=inputField})

And then, entering the loop

11:50:58:019 FATAL_ERROR System.UnexpectedException: Salesforce System Error: 1668576522-38148 (152924272) (152924272)

As you can see in the first debug, the List of Objects doesn't seem a list at all...
Bug?
Best Answer chosen by Francesco Carmagnola 10
Francesco Carmagnola 10Francesco Carmagnola 10
Hi Eldon,

yes, in the end that's what I did:

Apex Controller:
https://github.com/fracarma/Lightning-Object-Creator-Component/blob/master/src/classes/ChildCreatorController.cls#L60

But I had no fortune with the JSON.stringify method: the attribute was a List of SObjects, so the stringify not worked as I expected really. I changed the attribute type to String and it worked:

Component:
https://github.com/fracarma/Lightning-Object-Creator-Component/blob/master/src/aura/ChildCreatorComponent/ChildCreatorComponent.cmp#L16

JS Component Helper:
https://github.com/fracarma/Lightning-Object-Creator-Component/blob/master/src/aura/ChildCreatorComponent/ChildCreatorComponentHelper.js#L18

Actually, I think this is a very unelegant solution. SalesForce should resolve this bug!

All Answers

sfdcMonkey.comsfdcMonkey.com
hi Francesco Carmagnola
can you Please share your component code and js controller also
thanks
Francesco Carmagnola 10Francesco Carmagnola 10
Hi Piyush Soni,
my controller code is already there. My controller JS code is quite complicated, but basically I am setting as a parameter this object:

<aura:attribute name="fieldList" type="Object[]"  access="global"/>

fieldList="[
                                    {name : 'FirstName' ,       type : 'inputField', required : true},
                                    {name : 'LastName' ,       type : 'inputField', required : true},
                                    {name : 'Phone' ,        type : 'inputField', required : true},
                                    {name : 'Email' ,        type : 'inputField'}
                                ]"

 
EldonEldon
Hi Francesco,

In your Lightning component, use JSON.stringify to convert your object to a String:

 action.setParams({ strFilters : JSON.stringify(component.get("v.lstFilters")) });

In your Apex method, convert it back:

 MyClass[] lstFilters = (List<MyClass>)System.JSON.deserializeStrict(strFilters, List<MyClass>.Class);


Regards
Francesco Carmagnola 10Francesco Carmagnola 10
Hi Eldon,

yes, in the end that's what I did:

Apex Controller:
https://github.com/fracarma/Lightning-Object-Creator-Component/blob/master/src/classes/ChildCreatorController.cls#L60

But I had no fortune with the JSON.stringify method: the attribute was a List of SObjects, so the stringify not worked as I expected really. I changed the attribute type to String and it worked:

Component:
https://github.com/fracarma/Lightning-Object-Creator-Component/blob/master/src/aura/ChildCreatorComponent/ChildCreatorComponent.cmp#L16

JS Component Helper:
https://github.com/fracarma/Lightning-Object-Creator-Component/blob/master/src/aura/ChildCreatorComponent/ChildCreatorComponentHelper.js#L18

Actually, I think this is a very unelegant solution. SalesForce should resolve this bug!
This was selected as the best answer
Gabrielle Olivera 2Gabrielle Olivera 2
Anyone know if there is a known issue opened up for this? Or at least has someone posted something to the idea exchange?
Safouane BEN MANSOURSafouane BEN MANSOUR

Thank you for the question and also the good answer.

In fact, when I followed the 3 links in the good answer, I corrected my error.

Thank you :)