I have been asked to update a Controller used for a custom 'Recent Activities' LWC on the Account layout. Right now the Controller is querying Tasks related to the Account and displaying them in the component. I need to update this to also include Tasks that are related to any related Opportunities for the Accounts.
This is the method that gets Tasks from the Controller class.
//method to get tasks
public static TaskWrapper getTasks(Date fromDate,Date toDate,List<string> createdBy){
try{
// Intialize the TaskWrapper class
TaskWrapper wc = new TaskWrapper();
list<Task> tasks = new list<Task>();
//fetch records based on created by
if(createdBy.size()==0 ){
wc.listTasks = [SELECT Id, WhoId, WhatId, Who.Name,ActivityDate,What.Name, Subject, Status, Priority,Owner.Name,Description, Type ,CreatedById ,CreatedDate, AccountId FROM Task WHERE RecordType.Name !='Log a Call'];
}
else{
wc.listTasks = [SELECT Id, WhoId, WhatId, Who.Name,ActivityDate,What.Name, Subject, Status, Priority,Owner.Name,Description, Type ,CreatedById ,CreatedDate, AccountId FROM Task WHERE RecordType.Name !='Log a Call'];
}
//return wrapper
return wc;
} catch (Exception ex) {
//Catch and throw Aura Handled Exception
WM_HandleCustomException.LogException(ex);
throw new AuraHandledException(system.label.custom_exception);
}
}
What I have tried:
I tried adding an additional SOQL query that includes a subquery for related Opportunities. However this did not work:
wc.listTasks = [
select Id, Subject
from Task
where WhatId in (select Id from Opportunity)
and Type = 'Call'
order by Subject
];
Additionally I have tried adding the following:
list<Opportunity> opportunities = new list<Opportunity>();
list<Id> listOfOpps = new list<Id>();
Set<Id> accountIds = new Set<Id>();
for (Opportunity op : opportunities) {
accountIds.add(op.AccountId);
}
for(Account a : [select id, (select id from opportunities) from account where id in: accountIds]) {
for(Opportunity opp : a.opportunities) {
listOfOpps.add(opp.id);
}
}
My intentions was to query the related opportunities for the account and then update the Where clause to:
wc.listTasks = [SELECT Id, WhoId, WhatId, Who.Name,ActivityDate,What.Name, Subject, Status, Priority,Owner.Name,Description, Type ,CreatedById ,CreatedDate, AccountId FROM Task WHERE (WhatId == AccountId Or IN: listOfOpps) AND RecordType.Name !='Log a Call'];