1

I have one LWC component. In my JS file, I'm trying to call one apex method imperatively as it is not cache enabled, based on the result of wired method.

I'm not receiving the result of my wired method, as it is not executed at that time. I tried to call my imperative method in connectedCallback(), even at that time also my wired method data is not available.

What other options I have to call my imperative method after getting data from the wired method?

JS:

import ACCOUNT_ID from "@salesforce/schema/User.AccountId";

import checkActives from '@salesforce/apex/AcitveService.checkActives';

// this gets you the logged in user
import USER_ID from "@salesforce/user/Id";

@wire(getRecord, { recordId: USER_ID, fields: [ACCOUNT_ID] })
    wiredRecord({ error, data }) {
        if (error) {
        } else if (data) {
            
            let accId = data.fields.AccountId.value;

            checkActive({accountId: accId})
            .then(result => {
                if(result.hasActive){
                    this.displayActiveMessage = false;
                }else{
                    this.displayActiveMessage = true;
                    this.displayCaseCreation = false;
                }
            })
            .catch(error => {
                console.log('>>> error... '+JSON.stringify(error));
            });
        }
    } 

Edit: USER_ID is the logged in USER ID

3
  • What you have here is the correct structure - in the wire's function you check if you've got data (a wire is always called during component initialization with undefined error and data, then called with either data or an error) and only then imperatively call your other apex method. Have you debugged to check that the wire is being invoked? Recall that a wire is only called when all its reactive parameters have a value. How is USER_ID being defined? Please edit to add some detail.
    – Phil W
    Jan 10, 2023 at 18:11
  • FYI, you do not need to pass the running user Id to an Apex method, you can write it such that it consumes UserInfo.getUserId().
    – Adrian Larson
    Jan 10, 2023 at 18:58
  • 1
    @AdrianLarson this is calling the standard getRecord wire service by the look of it, so needs the ID to be passed in.
    – Phil W
    Jan 10, 2023 at 20:23

1 Answer 1

2

EDIT - Changed based on comment by Phil - Looking at the documentation, it appears that the salesforce/user/Id module must always execute before the main lwc controller class is constructed.


Assign USER_ID to a reactive variable - when it changes, it'll invoke the getter and then the wire:

import USER_ID from "@salesforce/user/Id";

export default class YourClass extends LightningElement {

  userId = USER_ID;

  @wire(getRecord, { recordId: "$userId", fields: [ACCOUNT_ID] }) 
  //etc etc

Also, you have called checkActive but have defined checkActives as the method - but this was probably a typo created when you defined this question.

6
  • 1
    I am pretty sure I have written wires with no reactive parameters before. However, what could be troublesome here is the user ID import; I think this is async and that might be part of the problem...?
    – Phil W
    Jan 10, 2023 at 20:22
  • Hmm maybe the best thing is to use a getter that looks at the imported USER_ID constant and make that reactive. (edited my answer) Jan 11, 2023 at 1:53
  • Answer upvoted; I think this is likely the right sort of way. I wonder if the getter needs @track, or did you test this for yourself?
    – Phil W
    Jan 11, 2023 at 7:24
  • I think it won't... but haven't tested. Have been doing quite a lot of LWC's recently though and fairly sure I don't need to add @track to a getter. Maybe it's implicit? Jan 11, 2023 at 9:21
  • 1
    It might be better to recommend the approach taken in the module documentation; i.e. store the value in a property of the LWC, which definitely will auto-track, and use that as a reactive parameter for the wire...
    – Phil W
    Jan 11, 2023 at 9:38

You must log in to answer this question.

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