0

I am trying to use recordId imperatively and do some logic based on that. But I can see recordId get printed in UI(for testing eg eg. 502i088888ABC23K) with {recordId}

But when I try to use the recordId which is Case.Id to call Apex class, recordId is not getting populated in Js file.. Any help is appreciated.

Update : added this.recordId but its coming as undefined This is a record page I am using LWC on top of that.

Update 2: REMOVING THE SET TIMEOUT SOLVED THE ISSUE.

import getRecord from '@salesforce/apex/CaseServiceController.getCaseWithRecordId';

const FIELDS = ['Case.First_Name__c','Case.Last_Name__c'];

export default class RedirectToRecordPage extends LightningElement {
  @api recordId; // its printing in HTML eg. 502i088888ABC23K
  @track record;
  @track error;

  connectedCallback(){
       setTimeout(function() {
          console.log('testing inside block');
          console.log('outside this.recordId :',this.recordId);
          getRecord({idx : this.recordId})
                .then(result => {
                    this.record = result;
                        // do logic with retrieved data
                    console.log('results,', result);
                    console.log('inside block this.recordId:',this.recordId);
                })
                .catch(e => {
                    console.log('error,', e);
                    this.error = e;
                })
        //code to be executed after 10 second
      }, 10000); // 10 Second
  }    

}
4
  • you can edit your question with new info (your console.log() statements/debugging). It's much easier to read than in comments. Also, where is this component on? A record page? A quick action? Feb 15 at 17:15
  • 1
    This is a Record page and Edited above Feb 15 at 17:21
  • 2
    any reason why the code is wrapped in setTimeout? Is that 10 seconds delay actually needed? Also, is that actually how you did the console logs? It shouldn't have commas to add the variables: console.log('outside this.recordId : ' + this.recordId). Feb 15 at 17:30
  • Removing the Set Time out Works !! thanks Kris Feb 15 at 17:38

3 Answers 3

2

Call Apex Methods Imperatively shows the format for doing this with parameters with this example

handleSearch() {
    findContacts({ searchKey: this.searchKey })
        .then((result) => {
            this.contacts = result;
            this.error = undefined;
        })
        .catch((error) => {
            this.error = error;
            this.contacts = undefined;
        });
}

You'll notice to pass in variables from your javascript file, you'll need to reference it as so this.yourVariableName. You're currently doing 'recordId'. Changing it to the following should work

getRecord({idx : this.recordId})
1
  • console.log('outside this.recordId : ', this.recordId); getRecord({idx : this.recordId}) .then(result => { this.record = result; console.log('results,', result); console.log('this.recordId : ', this.recordId); both are coming as "undefine" outside this.recordId : undefined Feb 15 at 17:13
3

In addition to what @Kris Goncalves has said, the setTimeout is the final problem. It has a function specified as function() {...} which means that the this binding changes from being the LWC component to be the window object (which is the this binding for the call from setTimeout). Change this to use an arrow function, which doesn't change the this binding (leaving it as the LWC component) and the problem will evaporate:

connectedCallback() {
    setTimeout(() => {
        ...
    }, 10000);

    ...
0

Try with @wire

import { LightningElement, api, track, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

export default class WireFunction extends LightningElement {
    @api recordId;
    @track record;
    @track error;

    @wire(getRecord, { recordId: '$recordId', fields: ['Account.Name'] })
    wiredAccount({ error, data }) {
        if (data) {
            this.record = data;
            this.error = undefined;
        } else if (error) {
            this.error = error;
            this.record = undefined;
        }
    }
    get name() {
        return this.record.fields.Name.value;
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.