+ Start a Discussion
vineet kumarvineet kumar 

Pass JS array to Apex wrapper class list variable in LWC

Hi All,

I have an Arrey in lwc JS and have Wrapper class in apex and on click button event mapping array to List<WrapperClass> but in apex all the array variable shows as NULL.

e.g.
in JS
===========
var accData=[{Name:"Sanjiv",Lastname:"Kumar"}];

=============
In Apex:
Class WrapperClass{
public string firstName;
public string lastName;
}

@AuraEnabled
public static void callMe(List<WrapperClass> wrpList){
system.debug('FirstName :'+wrpList[0].firstName);
system.debug('LastName :'+wrpList[0].lastName);
}


********when I call apex method callMe  through JS and pass accdata (JS array) FirstName and LastName appeares as NULL but values are being passed from JS*******************




Thanks

 
mukesh guptamukesh gupta
Hi Vineet,

Don't understand about your mapping, why mapping is need here .

Please mentioned in detail
 
vineet kumarvineet kumar
@mukesh gupta

Thanks for the reply!
mapping, I mean passing array variable to wrapper class variable (as a parameter).

Anyways fixed the issue.
Actually I forgot to mention { get; set; } to wrapper class variables thats why data was not getting passed to apex.
i.e.

BEFORE
Class WrapperClass{
public string firstName;
public string lastName;
}

AFTER
Class WrapperClass{
public string firstName  { get; set; }
public string lastName  { get; set; }
}


and once the change is done, now I can see the passed data into apex.

Thanks.
veer soniveer soni
hi ,
try below code.
Class WrapperClass{
        public string firstName;
        public string lastName;
    }
    @AuraEnabled
    public static void callMe( List<Map<String, String>>  wrpList){
        system.debug('wrpList :'+wrpList);
        for(Map<String, String> sMap : wrpList){
            system.debug('FirstName :'+sMap.get('firstName'));
        system.debug('LastName :'+sMap.get('lastName'));

        }
            }
from your Js class you was sending wrong data below is your data instead of Name it should have been firstName and lastName.
var accData=[{Name:"Sanjiv",Lastname:"Kumar"}];

any way use my code.
//Here is my js code.

from Js class you can send like this data =>   var accData=[{firstName:"Sanjiv",lastName:"Kumar"},
                    {firstName:"Kiran",lastName:"Jain"},
                    {firstName:"test",lastName:"12"}];

I am sure you are expacting like this. don't forget to mark it as best answer. 
Thank you
veer soniveer soni
hi vineet,
you should try my code once i believe it will work as your expectations and let me know if it helps you by marking it as best answer.
Thank you