0

I have a event listener that is firing to handle the creation for new leads.

Within the handler, I am calling a method to determine the source of the lead and identify the record type. However, one value is getting set but the other is not. See code snippet

import { LightningElement, api, track, wire } from 'lwc';
import getCallInfo from '@salesforce/apex/myclass.getCallInfo';
import getCustomSettings from '@salesforce/apex/myClass.getCustomSettings';
import getConfirmSource from '@salesforce/apex/myClass.getConfirmSource'; 
import { 
    createRecord,
    generateRecordInputForCreate,
    getRecordCreateDefaults
} from 'lightning/uiRecordApi';
import LEAD_OBJECT from '@salesforce/schema/Lead';
import { NavigationMixin } from  'lightning/navigation';

export default class CallStateTracker extends NavigationMixin(LightningElement) {



    /* Declaring getters and setters. */



    @wire(getCustomSettings) //Wires automatically invoke the APEX method within the paranetheses. 
    customSetting; 

    @wire(getRecordCreateDefaults, { objectApiName : LEAD_OBJECT})
    leadCreateDefaults;


    @api
    get lastCaller() {
        return this._lastCaller;
    }

    set lastCaller(value) {
        this.setAttribute('lastCaller', value);
        this._lastCaller = value;
        this.searchCaller();
    }

    @api
    get lastCallerName() {
        return this._lastCallerName;
    }

    set lastCallerName(value) {
        this.setAttribute('lastCallerName', value);
        this._lastCallerName = value;
    }

    @api
    get lastCallerSource() {
        return this._lastCallerSource;
    }

    set lastCallerSource(value) {
        this.setAttribute('lastCallerSource', value);
        this._lastCallerSource = value;
    }

    _lastCallerSource;

    _lastCaller;

    _lastCallerName;


    @track persons;

    @track shouldDisable = false;



/* Methods and handlers */ 



    handleNewLead() {
        this.shouldDisable = true;
        let badge;  // sets the business badge 
        let myRecordTypeId; 
        console.log('_lastCallerSource ==> ' + this._lastCallerSource); 
        getConfirmSource({lastCallSource: this._lastCallerSource})
            .then(response => {
                if(response == 'Lead2'){
                    // this.badge = 'Value2'
                    this.myRecordTypeId = this.customSetting.data.Lead_Id_2__c;
                    console.log('RECORDTYPE --> ' + this.myRecordTypeId ); 


                } 
            })
            .catch(error => {
                this.error = error; 
                console.log('ERROR SETTING RECORD TYPE ==> ' + this.error); 
            }) 

        if(!this.leadCreateDefaults.data) {
            return false;
        }

        let name = this._lastCallerName;

        if(name === undefined || name.trim() === '') {
            name = 'Not Provided';
        }

        if(name.match(',')) {
            name = name.split(',');
        } else {
            name = name.split(' ');
        }

        const leadObjectInfo = this.leadCreateDefaults.data.objectInfos[
            LEAD_OBJECT.objectApiName
        ];

        const leadDefaults = this.leadCreateDefaults.data.record;
        const leadInput = generateRecordInputForCreate(
            leadDefaults,
            leadObjectInfo
        );

        leadInput.fields.Company = name.join(' ');
        leadInput.fields.FirstName = name[0].trim();
        leadInput.fields.LastName = name.length > 1 ? name[1].trim() : name[0].trim();
        leadInput.fields.Phone = this.lastCaller;
        leadInput.fields.LeadSource = this._lastCallerSource;
        leadInput.fields.RecordTypeId = this.myRecordTypeId; 

        let _parent = this;

        let personName = (leadInput.fields.FirstName + ' ' + leadInput.fields.LastName).trim();
        if(personName == "") {
            personName = "No Name";
        }
        console.log('RecordId passed ==> ' + leadInput.fields.RecordTypeId);
        createRecord(leadInput)
            .then(lead => {
                this[NavigationMixin.Navigate]({
                    type: 'standard__recordPage',
                    attributes: {
                        actionName: 'view',
                        recordId: lead.id
                    }
                });

                _parent.persons = [
                    {
                        recordId: lead.id,
                        name: personName,
                        recordType: badge, 
                        icon: 'standard:lead'
                    }
                ];
            })
            .catch(error => {
                console.error(error);
            });
    }
}
  • There is not enough code in the above. if you're doing this in a .forEach(), for example, then 'this' context won't be captured automatically, you have to pass it in. – zaitsman Sep 19 '19 at 23:57
  • I can post more but the issue is primarily with the fact that "let myRecordTypeId" is not getting assigned the value once the getConfirmSource is fired. Salesforce doesn't even recognize the value. It's almost as if, my method is firing after CreateRecord. – thinker Sep 20 '19 at 0:07
  • 1
    Ahm, so is not getting assigned the value prior to getConfirmSource() -> this is in line with the code you posted? you didn't assign the value, it is assigned in the promise callback. – zaitsman Sep 20 '19 at 0:08
  • 1
    It's almost as if, my method is firing after CreateRecord this is very possible, especially if you're using @wire. You do need to include more JS as i mentioned in my first comment as atm it is not clear what is going on – zaitsman Sep 20 '19 at 0:10
  • 1
    Ok my dude, this is not even an LWC or Salesforce specific problem. Read up on promises. You set the value in the promise call back on line 86 then you attempt to use it outside the promise on line 127. Of course it is not working, because line 127 is reached BEFORE 86. – zaitsman Sep 20 '19 at 0:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.