public class AccountWrapper
{
@auraenabled
public string Name{get;set;}
@auraenabled
public integer NumberOfEmployees{get;set;}
@AuraEnabled
public List<Contact> Contacts { get; set; }
}
public class AccountService {
@auraenabled
public static void createAccountContact(AccountWrapper wrapper)
{
system.debug('wrapper:'+wrapper);
if(wrapper!=null)
{
Account act=new Account();
act.Name=wrapper.Name;
act.NumberOfEmployees=wrapper.NumberOfEmployees;
insert act;
if(wrapper.Contacts!=null)
{
for(Contact ct:wrapper.Contacts)
{
ct.AccountId=act.id;
}
insert wrapper.Contacts;
}
}
}
}
import { LightningElement } from 'lwc';
import createAccountContact from '@salesforce/apex/AccountService.createAccountContact';
export default class ApexWrapperCall extends LightningElement {
contacts=[];
error;
handleClick(e)
{
var contact=
{
LastName:'Sahni',
Email:'salesforcecodex@gmail.com',
Phone:'9871506648'
};
this.contacts.push(contact);
var pass=
{
Name:'Dhanik',
NumberOfEmployees:2,
Contacts:this.contacts
};
createAccountContact({wrapper:pass})
.then(result => {
console.log('Data:'+ JSON.stringify(result));
}) .catch(error => {
console.log(error);
this.error = error;
});
}
}
Link:
https://salesforcecodex.com/salesforce/sending-wrapper-object-to-apex-from-lwc/