You need to sign in to do that
Don't have an account?
Michael 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!
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; } }
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:
Controller: Helper :
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
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
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: Controller: (added to existing doInit)
Helper: Apex Class:
Am I missing something?
Thanks,
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.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
My debug log is showing the correct userId and the array is also correct, but the component list is empty :(
Here is the code
Apex Class: Component: Controller: Helper: Thanks,
Michael
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:
Controller: Helper :
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Thanks a million for your massive patience and help! :)