0

I'm new in LWC and javascript. Can you help me declare a variable account in Salesforce LWC js file. TIA.

1 Answer 1

1

JavaScript does not really have a concept of an Account object. You just need an object with an sobjectType property to tell the server how to cast the data into the appropriate object when it reaches the server.

If you wanted to pass an Account to an AuraEnabled method in an Apex class you would do so like below:

Apex class

public with sharing class AccountService {

    @AuraEnabled
    public static Account insertAccount(Account toInsert) {
        insert toInsert;
        return toInsert;
    }

}

LWC JS

import { LightningElement, wire } from 'lwc';
import createAccountRecord from '@salesforce/apex/AccountService.insertAccount';

export default class MyComponent extends LightningElement {

    // ...properties and other methods 

    createAccount() {
        // declare an object with a property `sobjectType` to help the middleware parse 
        // this object into an `Account` on the server
        let myAccount = { 'sobjectType': 'Account' };

        // set any additional fields
        myAccount.Name = 'Example';

        createAccountRecord({toInsert: myAccount})
            .then(result => {
                console.log(result);
            })
            .catch(error => {
                console.log(error);
            });
    }
}

Not the answer you're looking for? Browse other questions tagged .