function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Som_11Som_11 

How to call imperative method in connected call back

Hi all, I want to call an imperative method in connectedCallback() because at the time of loading I want to prepopulate some data into lwc.

I am returning wrapper class data from the apex. Trying the below snippet of code but in error also not able to get anything. Can anyone help me through this?

APEX : @AuraEnabled public static personDataWrapper
      getPersonTimeZone(Id loanReqId) {
    Loan_Request__c lr =
        [ select Id, Name, Leads__c, Account__c, Account__r.Name, Leads__r.Timezone__c, Leads__r.FirstName,
          Leads__r.LastName,
          Status__c from Loan_Request__c where Id =:loanReqId ];

    personDataWrapper pdw = new personDataWrapper();
    
    if(lr.Leads__c != NULL){
      pdw.personName = lr.Leads__r.FirstName + ' ' + lr.Leads__r.LastName;
    }else{
      pdw.personName = lr.Account__r.Name;
    }
    pdw.timezoneValue = lr.Leads__r.Timezone__c;
    pdw.loanRequestName = lr.Name;
    pdw.currentDt = system.now().adddays(1);
    if (lr.Status__c.contains('Closed')) {
      pdw.isLRClosed = true;
    } else {
      pdw.isLRClosed = false;
    }
    return pdw;
  }
public
  class personDataWrapper {
    @AuraEnabled public String personName {
      get;
      set;
    }
    @AuraEnabled public String timezoneValue {
      get;
      set;
    }
    @AuraEnabled public String loanRequestName {
      get;
      set;
    }
    @AuraEnabled public Boolean isLRClosed {
      get;
      set;
    }
    @AuraEnabled public DateTime currentDt {
      get;
      set;
    }
  }

LWC :   connectedCallback() {
    console.log('In connected call back function....');
    getPersonZone({loanReqId: this.recordId})
        .then(result => {
          console.log(result.records[0].personName);
          /* console.log('In connected call back result....');
           this.loanrequestdata = result;
           console.log('The loanrequestdata ' + this.loanrequestdata);
           console.log(
               '==result.Leads__r.FirstName===' + loanrequestdata.personName);
           this.timeZone = this.loanrequestdata.timezoneValue;
           if (this.loanrequestdata.personName != null &&
               this.loanrequestdata.personName != '') {
             this.title =
                 'Callback for Loan Inquiry ' + this.loanrequestdata.personName;
           }
           this.dateTimeFieldValue = this.loanrequestdata.currentDt;*/
        })
        .catch(error => {
          console.log('In connected call back error....');
          this.error = error;
          console.log('Error is ' + this.error);
        });
}

Template :     <lightning-input type="String"  onchange={handleTitleChange} label='Title' value={title} required>
    </lightning-input>
    <lightning-textarea name="input1" onchange={handleNotesChange} label="Notes"></lightning-textarea>
    <lightning-input type="String"  label='Timezone' value={value} readonly>
    </lightning-input>
Suraj Tripathi 47Suraj Tripathi 47

Hi Som_11,

You can take reference from here:

 

LWC:


import { api, LightningElement, track, wire } from 'lwc';
import passrecordId from '@salesforce/apex/CaseSectionApex.methodName';
export default class CaseCreation extends LightningElement {
    @track caseResult;
    @api recordId='5002y00000BA6juAAD';
 
    connectedCallback(){
        getAllCaseFields({accId:this.recordId})
         .then(result=>{
             this.caseResult=result;
             console.log(JSON.stringify(this.caseResult));
         })
         .catch(error=>{
             this.caseResult=undefined;
         })
    }
    
   
 
}





Apex:

public with sharing class CaseSectionApex {
     @AuraEnabled
     public static void methodName(String accId){
         system.debug(accId);
         
     }
}


Please Mark it as Best Answer, if it helps.

Thanks