1

Is it possible to pass a list or Set to apex class function (@AuraEnabled) from lightning component. I am getting Error Message "Internal Server Error"

//Initialize the Apex Controller Method to be called
var saveAction = component.get("c.testfunction");
var fruits = ["Apple", "Banana"];

//Pass the parameters to the Apex controller method
saveAction.setParams ({
    "pListfruits": fruits
});

saveAction.setCallback(this, function(response) {
   alert('@@ response @@ ' + saveAction.getError()[0].message);
});
$A.enqueueAction(saveAction);

Apex Function:
@AuraEnabled
public static void testfunction(list<String> lstfruits) {
    System.debug('@@ lstfruits@@' + lstfruits);
}
2

You may be running into issues with the differences between a Javascript List/Array and a Apex/Java List. The lightning error messages are extremely unhelpful, so you may want to try a different approach entirely. One approach that I have used consistently (and reliably) is to pass data as a JSON string. so you would do something like this:

//Initialize the Apex Controller Method to be called
var saveAction = component.get("c.testfunction");
var fruits = ["Apple", "Banana"];

//Pass the parameters to the Apex controller method
saveAction.setParams ({
    "pListfruits": JSON.stringify(fruits)
});

saveAction.setCallback(this, function(response) {
   alert('@@ response @@ ' + saveAction.getError()[0].message);
});
$A.enqueueAction(saveAction);

Apex Function:
@AuraEnabled
public static void testfunction(String lstfruits) {
    Map<String, Object> jsonObj = (Map<String, Object>)JSON.deserializeUntyped(lstfruits);
    //then do stuff with the map...
}

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm#apex_System_Json_deserializeUntyped

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.