3

DISCLAIMER : I know there are better ways to get the current User Id, I am just writing this up as example to illustrate the issue I am facing.

Let's say I have two apex methods that are @AuraEnabled (cacheable=true):

    @AuraEnabled (cacheable=true)
    public static id getUserId() {
        return UserInfo.getUserId();
    }

    @AuraEnabled (cacheable=true)
    public static List<Task> getTasksForUser(Id userId) {
    List<Task> returnValue = new List<Task>();
    for (Task task : [SELECT Id,  WhoId, WhatId, Subject, ActivityDate, Priority, Status FROM Task WHERE OwnerId = : userId]) {
         returnValue.add(task);
    }
    return returnValue;
    }

and I also have a Lightning Web Component which uses the @wire notation to wire the results of these methods to properties:

    @wire(getUserId, {}) myId;
    @wire(getTasksForUser, { userId: '$myFormattedId' }) myTasks;
    get myFormattedId () {
        if (this.myId && this.myId.data) {
            return this.myId.data;
        }
        return undefined;
    }

I am having a difficult time getting the myTasks variable to be populated correctly.

It seems as though the call to getTasksForUser gets cached as an empty list, then the myId variable gets populated, and getTasksForUser is called again, but it never makes it to the server, even though the input variable has changed since its' original call.

This did not seem to be the case as of a few weeks ago. The @wire documentation states "If a reactive variable changes, the wire service provisions new data" but I do not observe that being the case.

How can we use the output of one wired apex call as the input to another?

1
  • AFAIC get properties are not watched and so yeah, i am surprised it ever worked for you at all...
    – zaitsman
    Jan 28, 2020 at 5:35

1 Answer 1

4

I would change it a bit like so:

@track targetUserId;

@wire(getUserId, {})
getUserIdData({ error, data }) {
 if(data != null) {
   this.targetUserId = data;
 }
}

@wire(getTasksForUser, { userId: '$targetUserId' }) myTasks;

This should definitely work because we are @tracking the field on the component. In theory, there is a change coming in Spring '20 where everything is @tracked by default; however i saw nothing about that starting to apply to actual get properties.

1
  • I think that the other times I have used get, I did not feed the results back to Apex. Thank you for the help, worked like a charm! Jan 28, 2020 at 14:40

You must log in to answer this question.

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