+ Start a Discussion
Gopal Kallepu 3Gopal Kallepu 3 

Unable to pass an array of strings from helper to apex controller method

Hi,
I am trying get the details of list of accounts whose Ids are present in an array (of Strings)
Following are my code details :
component code :
<aura:component controller="ApexController" >
    <aura:attribute name="selectedAccountIdsArray" type="String[]"/>
   <aura:attribute name="selectedAccounts" type="Account[]"/>
   Code to iterate on accounte...
</aura:component>
Helper code :
var action = component.get("c.getSelectedAccounts");
        action.setParams({
          "fAccntIds" : component.get("v.accountsToRebal") 
        });
         alert(component.get("v.accountsToRebal") ;);
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (component.isValid() && state === "SUCCESS") {
                component.set("v.selectedAccounts", response.getReturnValue());
            }
Apex code :
@AuraEnabled
    public static List<Account> getSelectedAccounts(List<String> selAccountIds) {
        system.debug('##########################'+ selAccountIds);
        return [SELECT Id,Name FROM Account WHERE Id in : selAccountIds ] ;
    }

But on executing this I am getting following error in debug logs, 
03:53:52.214 (214837802)|EXECUTION_STARTED
03:53:52.214 (214870409)|CODE_UNIT_STARTED|[EXTERNAL]|01p37000002JJaj|ApexController.getSelectedAccounts
03:53:52.215 (215446408)|METHOD_ENTRY|[1]|01p37000002JJaj|ApexController.ApexController()
03:53:52.215 (215459217)|METHOD_EXIT|[1]|ApexController
03:53:52.215 (215616551)|FATAL_ERROR|Internal Salesforce.com Error
03:53:52.215 (215634182)|CUMULATIVE_LIMIT_USAGE
03:53:52.215 (215634182)|LIMIT_USAGE_FOR_NS|(default)|
As the debug statement (######## ) is not being printed, clearly there is some issue with assigning the array of string to List of strings..

How can I solve this issue... ?
Note : We are passing the values of 'selectedAccountIdsArray' from parent component 



 
Best Answer chosen by Gopal Kallepu 3
Gopal Kallepu 3Gopal Kallepu 3
Hi guys,

It looks like this is a limitation/bug in lightning framework, which needs to be fixed..you can find some useful infromation about this at this link
http://salesforce.stackexchange.com/questions/55464/sobject-array-parameter-in-lightning-causes-internal-salesforce-com-error-in-ape

However, for time being I am going with this work around :
  • convert the list into a json string before passing it as an argument using  (ofcourse, change the parameter of apex method to String)
    JSON.stringify(component.get("v.accountsToRebal"));
  • Then convert this string to a List in the apex method using this
            List<String> selectedIdsList = new List<String>();
            Object[] values = (Object[])System.JSON.deserializeUntyped(selAccountIds);
            if(values.size()>0){         
                 for (Object id : values) {
                     selectedIdsList.add(string.valueof(id) );
                 }
             }


     

 

All Answers

William TranWilliam Tran
I don't see selectedAccountIdsArray being used anywhere?

where is accountsToRebal being defined?

Make sure all your variables are in the correct places.

Thx
Shalagha GumberShalagha Gumber
By looking at your code, it seems that 'selectedAccountIdsArray' contains the Ids of the Accounts that need to be queried in getSelectedAccounts method of Apex Controller.

So, instead of passing the value of component.get("v.accountsToRebal") ('accountsToRebal' does not seem to be an attribute of the component as per the above code), try passing the value of component.get("v.selectedAccountIdsArray") as parameter to the Apex Controller method.
Gopal Kallepu 3Gopal Kallepu 3
Hi guys,

It looks like this is a limitation/bug in lightning framework, which needs to be fixed..you can find some useful infromation about this at this link
http://salesforce.stackexchange.com/questions/55464/sobject-array-parameter-in-lightning-causes-internal-salesforce-com-error-in-ape

However, for time being I am going with this work around :
  • convert the list into a json string before passing it as an argument using  (ofcourse, change the parameter of apex method to String)
    JSON.stringify(component.get("v.accountsToRebal"));
  • Then convert this string to a List in the apex method using this
            List<String> selectedIdsList = new List<String>();
            Object[] values = (Object[])System.JSON.deserializeUntyped(selAccountIds);
            if(values.size()>0){         
                 for (Object id : values) {
                     selectedIdsList.add(string.valueof(id) );
                 }
             }


     

 
This was selected as the best answer
Raj R.Raj R.
is there a reason why you didn't create a string that is comma separated using a js controller and then pass that string into the apex method to be split using the String class (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm). Here is some example code. I understand the whole JSON portion, but curious if there was a particular reason why string.split(character) was not used.
 
//lightning component (I am excluding some of code and getting straight to the lines that are needed) generateString : function(cmp, event, helper) { 
//code to generate string using some character to separate goes here (i.e. ",", ";", "|")

 } 

action.setParams{ 

"strToSplit" : "str" ,
"characterUsedForSpliting" : ","

} 

----- //apex class 
@AuraEnabled 
public static void performSomeAction(String strToSplit, String characterUsedForSpliting) { 
                    List<String> splitStr = strToSplit.split(characterUsedForSplitting); //perform necessary actions to convert to list<id> or use list to query the records you need and then iterate through that returned set 

}