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
Michael Degn JensenMichael Degn Jensen 

Apex Class: Passing parameter from one query to another not working

Hi,

I have the below Apex Class with 2 queries. First I get the current user details (this one is working as I can display the details in the component).
I then want to pass the current user id to the getTasks query and only get the tasks assigned to the current user.
Howevet I'm not getting any results :(
If I remove the "WHERE OwnerId=:oUser" from the 2. query I get all tasks, so the query is working - just not the "WHERE OwnerId=:oUser" part.
What am I doing wrong? Thank!
 
public class ALTaskController {
    @AuraEnabled 
    public static user fetchUser(){
     // query current user information  
      User oUser = [select id,Name,TimeZoneSidKey,Username,Alias,Country,Email,FirstName,LastName,IsActive 
                 FROM User Where id =: userInfo.getUserId()];
        return oUser;
    }
    
    @AuraEnabled
    public static List<Task> getTasks(Id oUser){
        List<Task> taskList = [SELECT Id,Subject,Description,OwnerId,ActivityDate,WhoId,Priority,Status,Type FROM Task WHERE OwnerId=:oUser AND ActivityDate <= NEXT_N_DAYS:3 AND Status != 'Completed' ORDER BY ActivityDate ASC];
        return taskList;
    }
}

 
Best Answer chosen by Michael Degn Jensen
Ajay K DubediAjay K Dubedi
 
Hi Michael,
You should set return list value to any attribute.Please Try the below code and let me know if this works for you. If still need modifications do let me know.
Apex Controller will remain same.

Component:
 
<aura:component controller="ALTaskController" implements="flexipage:availableForAllPageTypes">
   <lightning:workspaceAPI aura:id="workspace"/>
    <aura:attribute name="taskList" type="List" />
    <aura:attribute name="user_id" type="String" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    <lightning:card class="accountCard" variant="Narrow" iconName="standard:task" footer="Card Footer">
        <aura:set attribute="title">
            Mine opgaver (test)
        </aura:set>
        <aura:set attribute="body">
            <p class="slds-p-horizontal_medium">
                <!-- Use the Apex model and controller to fetch server side data -->
                <aura:iteration items="{!v.taskList}" var="tasks">
                    Id :{!tasks.Id}   <br /> Status : {!tasks.Status}
                </aura:iteration>
            </p>  
        </aura:set>
    </lightning:card>
</aura:component>

Controller:
({
    doInit: function(component, event, helper) {
        // Fetch the account list from the Apex controller
        helper.getUserId(component,event,helper);
    }
})
Helper :
({
    getUserId: function(component,event,helper) {
        var action = component.get('c.fetchUser');
        // Set up the callback
        action.setCallback(this, function(actionResult) {
            component.set('v.user_id', actionResult.getReturnValue().Id);
            console.log(actionResult.getReturnValue().Id);
            helper.getTaskList(component,event,helper);
        });
        $A.enqueueAction(action);
    },
     getTaskList: function(component,event,helper) {
        var action = component.get('c.getTasks');
         console.log(component.get('v.user_id'));
         var user_id = component.get('v.user_id')
         action.setParams({
             oUser : user_id
        });
        action.setCallback(this, function(actionResult) {
            component.set('v.taskList', actionResult.getReturnValue());
            console.log(actionResult.getReturnValue())
        });
        $A.enqueueAction(action);
    }
})

User-added image
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi

All Answers

Ajay K DubediAjay K Dubedi
Hi Michael,
Please try the below code it works for me and let me know if this works for you. If still need modifications do let me know.

Apex class :-

public class ALTaskController {
    @AuraEnabled 
    public static user fetchUser(){
     // query current user information  
      User oUser = [select id,Name,TimeZoneSidKey,Username,Alias,Country,Email,FirstName,LastName,IsActive 
                 FROM User Where id =: userInfo.getUserId()];
        return oUser;
    }
    
    @AuraEnabled
    public static List<Task> getTasks(Id oUser){
        System.debug('oUser'+oUser);
       List<Task> taskList = [SELECT Id,Subject,Description,OwnerId,ActivityDate,WhoId,Priority,Status,Type FROM Task WHERE OwnerId=:oUser AND ActivityDate <= NEXT_N_DAYS:3 AND Status != 'Completed' ORDER BY ActivityDate ASC];
       System.debug('taskList'+taskList);
        return taskList;
    }
}
aura handler:     

({
    getUser: function(component,event,helper) {
        var action = component.get('c.fetchUser');
        // Set up the callback
        action.setCallback(this, function(actionResult) {
            component.set('v.user_id', actionResult.getReturnValue().Id);
            console.log(actionResult.getReturnValue().Id);
            helper.setAcc(component,event,helper);
        });
        $A.enqueueAction(action);
    },
     setAcc: function(component,event,helper) {
        var action = component.get('c.getTasks');
         console.log(component.get('v.user_id'));
         var set_user_id = component.get('v.user_id')
         action.setParams({
             oUser : set_user_id 
        });
        action.setCallback(this, function(actionResult) {
            console.log(actionResult.getReturnValue())
        });
        $A.enqueueAction(action);
    }
})


Controller :
({
    doInit: function(component, event, helper) {
        helper.getUser(component,event,helper);
    },
})

Component:
 <aura:component controller="ALTaskController" implements="flexipage:availableForAllPageTypes">
    <aura:attribute name="user_id" type="String" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
</aura:component>

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Michael Degn JensenMichael Degn Jensen
Hi Ajay
Thanks for your prompt reply.
I tried implementing your code, but I am not getting any results.
Here is what I added to my code according to your instructions

Component:
<aura:attribute name="user_id" type="String" />
Controller:
helper.getUser(component,event,helper);
(added to existing doInit)

Helper:
getUser: function(component,event,helper) {
        var action = component.get('c.fetchUser');
        // Set up the callback
        action.setCallback(this, function(actionResult) {
            component.set('v.user_id', actionResult.getReturnValue().Id);
            console.log(actionResult.getReturnValue().Id);
            helper.setAcc(component,event,helper);
        });
        $A.enqueueAction(action);
    },
     setAcc: function(component,event,helper) {
        var action = component.get('c.getTasks');
         console.log(component.get('v.user_id'));
         var set_user_id = component.get('v.user_id')
         action.setParams({
             oUser : set_user_id 
        });
        action.setCallback(this, function(actionResult) {
            console.log(actionResult.getReturnValue())
        });
        $A.enqueueAction(action);
    }
Apex Class:
System.debug('taskList'+taskList);


Am I missing something?
​​​​​Thanks,
Michael

Michael Degn JensenMichael Degn Jensen
I also tried creating a completly new Apex Class and Component and no results either.
Ajay K DubediAjay K Dubedi
Hi Michael,
Please check your debug log if userId return by the helper. after that make sure your data where your query in tasklist to satisfy the condition. I created the same data which you query in the tasklist it works for me. If still need modifications do let me know.
User-added image
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Michael Degn JensenMichael Degn Jensen
Hi Ajay,
My debug log is showing the correct userId and the array is also correct, but the component list is empty :(
Developer Log

Component view

Here is the code

Apex Class:
public with sharing class AL_Test {
    @AuraEnabled 
    public static user fetchUser(){
     // query current user information  
      User oUser = [select id,Name,TimeZoneSidKey,Username,Alias,Country,Email,FirstName,LastName,IsActive 
                 FROM User Where id =: userInfo.getUserId()];
        return oUser;
    }
    
    @AuraEnabled
    public static List<Task> getTasks(Id oUser){
        System.debug('oUser'+oUser);
       List<Task> taskList = [SELECT Id,Subject,Description,OwnerId,ActivityDate,WhoId,Priority,Status,Type FROM Task WHERE OwnerId=:oUser AND ActivityDate <= NEXT_N_DAYS:3 AND Status != 'Completed' ORDER BY ActivityDate ASC];
       System.debug('taskList'+taskList);
        return taskList;
    }

}
Component:
<aura:component controller="AL_Test" implements="flexipage:availableForAllPageTypes">
  	<lightning:workspaceAPI aura:id="workspace"/>
    <aura:attribute name="tasks" type="List" />
    <aura:attribute name="user_id" type="String" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    <lightning:card class="accountCard" variant="Narrow" iconName="standard:task" footer="Card Footer">
        <aura:set attribute="title">
            Mine opgaver (test)
        </aura:set>
        <aura:set attribute="body">
            <p class="slds-p-horizontal_medium">
                <!-- Use the Apex model and controller to fetch server side data -->
                <aura:iteration items="{!v.tasks}" var="task">
                    {!task.Id}
                </aura:iteration>
            </p>
        </aura:set>
    </lightning:card>
</aura:component>
Controller:
({
    doInit: function(component, event, helper) {
        helper.getUser(component,event,helper);
    }
})
Helper:
({
    getUser: function(component,event,helper) {
        var action = component.get('c.fetchUser');
        // Set up the callback
        action.setCallback(this, function(actionResult) {
            component.set('v.user_id', actionResult.getReturnValue().Id);
            console.log(actionResult.getReturnValue().Id);
            helper.setAcc(component,event,helper);
        });
        $A.enqueueAction(action);
    },
     setAcc: function(component,event,helper) {
        var action = component.get('c.getTasks');
         console.log(component.get('v.user_id'));
         var set_user_id = component.get('v.user_id')
         action.setParams({
             oUser : set_user_id 
        });
        action.setCallback(this, function(actionResult) {
            console.log(actionResult.getReturnValue())
        });
        $A.enqueueAction(action);
    }
})
Thanks,
Michael
Ajay K DubediAjay K Dubedi
 
Hi Michael,
You should set return list value to any attribute.Please Try the below code and let me know if this works for you. If still need modifications do let me know.
Apex Controller will remain same.

Component:
 
<aura:component controller="ALTaskController" implements="flexipage:availableForAllPageTypes">
   <lightning:workspaceAPI aura:id="workspace"/>
    <aura:attribute name="taskList" type="List" />
    <aura:attribute name="user_id" type="String" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    <lightning:card class="accountCard" variant="Narrow" iconName="standard:task" footer="Card Footer">
        <aura:set attribute="title">
            Mine opgaver (test)
        </aura:set>
        <aura:set attribute="body">
            <p class="slds-p-horizontal_medium">
                <!-- Use the Apex model and controller to fetch server side data -->
                <aura:iteration items="{!v.taskList}" var="tasks">
                    Id :{!tasks.Id}   <br /> Status : {!tasks.Status}
                </aura:iteration>
            </p>  
        </aura:set>
    </lightning:card>
</aura:component>

Controller:
({
    doInit: function(component, event, helper) {
        // Fetch the account list from the Apex controller
        helper.getUserId(component,event,helper);
    }
})
Helper :
({
    getUserId: function(component,event,helper) {
        var action = component.get('c.fetchUser');
        // Set up the callback
        action.setCallback(this, function(actionResult) {
            component.set('v.user_id', actionResult.getReturnValue().Id);
            console.log(actionResult.getReturnValue().Id);
            helper.getTaskList(component,event,helper);
        });
        $A.enqueueAction(action);
    },
     getTaskList: function(component,event,helper) {
        var action = component.get('c.getTasks');
         console.log(component.get('v.user_id'));
         var user_id = component.get('v.user_id')
         action.setParams({
             oUser : user_id
        });
        action.setCallback(this, function(actionResult) {
            component.set('v.taskList', actionResult.getReturnValue());
            console.log(actionResult.getReturnValue())
        });
        $A.enqueueAction(action);
    }
})

User-added image
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
This was selected as the best answer
Michael Degn JensenMichael Degn Jensen
Ajay you are my hero!! That did the trick! :)
Thanks a million for your massive patience and help! :)