2

I am trying to pass multiple parameters from the lightning component to the Apex Class. But the passed parameter from the lightning component to Apex shows as NULL. Below is the component Controller

onChange: function (component, event, helper) {

       var dropdownSelection = component.find('select').get('v.value');
       alert('Selection made ' +dropdownSelection);

       var action = component.get("c.transferCase");
       action.setParams({caseId: component.get('v.recordId')}, {dept: dropdownSelection});
       action.setCallback(this, result => {
       .......................

In the alert for 'Selection made ' +dropdownSelection I can see the selection

enter image description here

But when I pass this in to the apex Class like below

public class manageCase {
    @AuraEnabled
    public static void transferCase(String caseId, String dept){
        try
        {
            System.debug('~~~~~~~~Transfer to Dep~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ : ' +dept);
        List<Case> records = [select id,Status from Case WHERE Id = :caseId];
        for (Case record : records)
        {
            if(dept == 'JaxServices')
            {
              ....
            }      
             ....        

In the debug I see null

enter image description here

Am I missing something here why the passed parameter is null here.

  • Please use the {} tool in the editor or triple backticks (```) to format your code. Use only relevant tags, here apex and lightning-aura-components. – David Reed Apr 13 at 16:35
4

You formed the JSON for the parameters incorrectly.

The correct format is this:

action.setParams({caseId: component.get('v.recordId'), dept: dropdownSelection});

In short, you need one JSON object with one property for each parameter you want to pass, not one JSON object per parameter.

PS: This is not a problem in this case, since you did it correctly, but the order and case of the parameters is important.

| improve this answer | |
0

Thanks Sebastian this really helped me as not easily found in the documentation!

To add to your example, to dynamically build action.setParams() something like this can be done.

Multiple params to controller

splitSalesAndServiceDataByKey : function(component) {

        var action = component.get("c.getSalesAndServiceDataTM");
        let params = {}; 

            let accountEntries =this.removeNullEntries(component.get("v.accountIds"));
            if(accountEntries.length > 0){
                //action.setParams({accs : accountEntries});
                params.accs = accountEntries;
            }
            
            let urnEntries = this.removeNullEntries(component.get("v.URNs"));
            if(urnEntries.length > 0){
               // action.setParams({urns : urnEntries});
               params.urns = urnEntries;
            }
                    
            let corpGroupEntries = this.removeNullEntries(component.get("v.corporateGroups"));
            if(corpGroupEntries.length >0){
                //action.setParam({groups : corpGroupEntries});
                params.groups =corpGroupEntries;
            }
            
            let salesDistrictEntries = this.removeNullEntries(component.get("v.salesDistricts"));
            if(salesDistrictEntries.length >0){
                //action.setParam({districts : salesDistrictEntries});
                params.districts= salesDistrictEntries;
            }
            
            let empIdEntries = this.removeNullEntries(component.get("v.employeeIds"));
            if(empIdEntries.length >0){
                //action.setParam({employees : empIdEntries});
                params.employees = empIdEntries;
            }
        
        //params Object
        accs: ["13saa", "2aads"]
        urns: ["35335s"]
        groups:  ["1sra", "3s3"]
        districts: ["23sdg"]
        employees: ["24srs"]


        action.setParams(params); 

To call this controller method..

 @AuraEnabled
    public static SasService getSalesAndServiceDataTM(List<String> accs, List<String> urns, List<String> groups, List<String> districts, List<String> employees) {
| improve this answer | |

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.