[SalesForce] Pass JavaScript Variable To Apex Controller in LWC

Javascript

import createContactRecord from '@salesforce/apex/ContactController.createContactRecord';
import updateContactRecord from '@salesforce/apex/ContactController.updateContactRecord';
import myContact from "@salesforce/apex/ContactController.fetchContact";
....

@track recordId;
contactRecord;


// fetches a contact record from Apex
@wire (myContact)
    fetchedContact({error, data}){
        if(data){

            // this is where I save the fetched contact which will be updated later
            this.contactRecord = JSON.stringify(data);
            ...
            ...
        }
        ...
    }


// my create record JS function, where I construct a SObject and am able to 
// successfully create a record
createRecord() {

    // created a JSON representation of the Contact record, 
    // same as we would do in Lightning Aura Components

    let cont = { 'sobjectType': 'Contact' };
    cont.FirstName = 'Jayant';
    cont.LastName = 'From LWC';

    createContactRecord({newRecord: cont})
        .then(result => {
            this.recordId = result;
            console.log(result);
        })
        .catch(error => {
            console.log(error);
            this.error = error;
        });
}


// my update record JS function, where I manipulate the JSON 
// and set some values to be able to successfully update the record
updateRecord() {
    let cont = JSON.parse(this.contactRecord);

    // update the fields those are required to be updated
    cont.LastName = '-LWC1';

    updateContactRecord({recordForUpdate: cont})
        .then(result => {
            this.wiredContact = result;
            console.log(result);
        })
        .catch(error => {
            console.log(error);
            this.error = error;
        });
}

Apex Controller

@AuraEnabled(cacheable=true)
public static Contact fetchContact(){
    return [SELECT Id,Name, LastName FROM Contact where Id='xxxx' LIMIT 1];
}

@AuraEnabled
public static String createContactRecord(Contact newRecord){
    insert newRecord;
    return newRecord.Id;
}

@AuraEnabled
public static contact updateContactRecord(Contact recordForUpdate){
    update recordForUpdate;
    return recordForUpdate;
}

In place of 'XXXX' in apex controller i want to call list of contact from "recordId" which is in JS function and then fetch the list

Best Answer

You need to create a parameter in the Apex function as below

public static Contact fetchContact(Id[] contactIds){
    return [SELECT Id,Name, LastName FROM Contact where Id IN :contactIds];
}

From the wired Apex method pass the parameter. Please note that the API name of the parameter should be the same in both js and apex code.

@wire(fetchContact, { contactIds: '$contactIdList' })
contacts;

Where contactIdList is the list of contact ids.


If you need to get a single record then use Id type instead of Id[] and pass the record Id as below.

public static Contact fetchContact(Id contactId){
    return [SELECT Id,Name, LastName FROM Contact where Id = :contactId];
}


@wire(fetchContact, { contactId: '$recordId' })
contact;

See more examples here.

Note: If you want to get only a single record with Id, always prefer the getRecord wired adapter.

Related Topic