+1 vote
32 views
by (163k points)

I'* creating a custom lead conversion page, I'* trying to pass the lead to an apex controller, I think the problem I guess is when the 'handleConvert' function in js is called, the variable varLeadRecord is not recognized by the apex.

Here is my js controller:

import { LightningElement, api, wire, track } from 'lwc';
import getLeadRecord from '@salesforce/apex/apexController.getLeadRecord';
import convertLead from '@salesforce/apex/apexController.convertLead';    

export default class CustomLeadConversion extends LightningElement {
    @api recordId; 
    @track isNewAccount = true;
    @track isNewContact = true;
    @track isNewOpp = true;
    @track varLeadRecord = {};

    @wire(getLeadRecord, { leadId : '$recordId' })
    leadRecordDetails({ data, error }){
        if(data){
            this.varLeadRecord = data;
            console.log('this.varLeadRecord please');
            console.log(this.varLeadRecord);
            console.log(this.varLeadRecord.Name);
            console.log(this.varLeadRecord.Id);
        }else if(error){

        }
    }  
        
    handleConvert(){
        console.log('convert button is clicked'); //this is OK
        console.log(this.varLeadRecord); //
        convertLead({leadRec: '$this.varLeadRecord'})
        .then(result => {
            console.log(result);
        })
        .catch(error => {
            this.error = error;
        });
    }
}

Here is my APEX controller

public class apexController{
    @AuraEnabled(cacheable=true)
    public static Lead getLeadRecord(String leadId) {   
        System.debug('leadId ' + leadId);
        
        Lead leadRec;
        List<Lead> leadList = [SELECT Id, Name, Salutation, FirstName, LastName FROM Lead WHERE Id =: leadId];
        System.debug('leadList ' + leadList);
        if(leadList !=null && !leadList.isEmpty()){
            return leadList[0];
        }
        return leadRec;       
    }   
    
   @AuraEnabled(cacheable=true)
    public static String convertLead(Lead leadRec){
        System.debug('convert lead');
        return 'converted';
        
    }
}
 

1 Answer

+1 vote
by (163k points)
 
Best answer

In the imperative apex call, you don't need to use the "$varLeadRecord"(This format is used only for wire calls). format. Simply use this.varLeadRecord without any quotes (This does not work with wired calls).

handleConvert(){
    console.log('convert button is clicked'); //this is OK
    console.log(this.varLeadRecord); //
    convertLead({leadRec: this.varLeadRecord})
    .then(result => {
        console.log(result);
    })
    .catch(error => {
        this.error = error;
    });
}
Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
You can donate any amount for Orphans village using following QR Code.
...