1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Sending Wrapper object to Apex

Last updated at Posted at 2022-07-05

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/

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?