You need to sign in to do that
Don't have an account?
Francesco 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:
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?
@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?
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
can you Please share your component code and js controller also
thanks
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'}
]"
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
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!
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 :)