0

enter image description hereI want to call an imperative method in connectedCallback() because at the time of lwc loading I want to prepopulate some data into lwc.

I am returning wrapper class data from the apex. Trying the below snippet of code to get that in js but in error also not able to get anything.its not going in result at all. 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>
<lightning-input disabled={isNoTimeSelected} type="datetime" class="slds-col validateCheck" value={dateTimeFieldValue} onchange={handleDateTimeChange}>
            </lightning-input>
2
  • Welcome to SFSE! Please use the tools in the editor to format your code - I've fixed it here.
    – David Reed
    Commented Jul 19, 2021 at 19:49
  • if you do console.log('Error is ' + this.error.body.message);, what do you see? Commented Jul 19, 2021 at 19:53

2 Answers 2

4

Here's the culprit: console.log(result.records[0].personName);
getPersonTimeZone returns a single instance of personDataWrapper class, which doesn't have a records property, so doing result.records[0] you're trying to access to an undefined parameter, thus the exception.

By the way, console.log() takes a list of objects to output, so there is no need to concatenate them as string, you could and should pass them as different parameters so you'll never see again [object Object]. The same goes for console.info(), console.warn(), console.error() and so on.

connectedCallback() {
    console.log('In connected call back function....');
    getPersonZone({loanReqId: this.recordId})
        .then((result) => {
            console.log(result.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 && this.loanrequestdata.personName.length > 0) {
                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;
            // This way you are not to going to see [object Object]
            console.log('Error is', this.error); 
        });
}
2
  • Anyways I am getting record id undefined when I try to print that as I am adding that in connected call back. I want to fetch data without cacheable=true using imperative method when the LWC component loads. How to acheive that.
    – Som
    Commented Jul 20, 2021 at 9:40
  • 1
    @Somesh is recordId decorated with @api annotation?
    – RubenDG
    Commented Jul 20, 2021 at 11:13
0

I had a similar scenario with no result being returned.

The issue for me was that I was missing the @AuraEnabled annotations against the properties of the wrapper class, which I could see in the example above.

You must log in to answer this question.

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