1

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'];
      

1 Answer 1

0

First you need to get all the opportunities related to that account and store there Id in the list(listOfOppId).

Then you can query Task and in Where condition put (WhatId = AccountId Or WhatId IN: listOfOppId)

By this you will get both of them in a single go.

2
  • Hey @Prajjwal agrawal, I have been following the advice in your answer for a few days but am still a bit confused. If possible, could you please clarify your first point- of how to store the related opps, as well as your second point of the updated condition? I have updated my question based off of your comments. Thanks
    – sp2
    Oct 24, 2022 at 0:59
  • You are having accountId for which you want task to update. So, query on Opportunity where AccountId =: accountId and add all opportunities into a set . Oct 24, 2022 at 5:16

You must log in to answer this question.

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