You need to sign in to do that
Don't have an account?
Abhishek Chopra
How can i optimize my below code using a Map instead of a List
/*The Below Code Takes List of Account from Process builder and checks if the Annual Revenue field. If Annual Revenue < 25000 then Delete all of the related task of that Account, else if Annual Revenue > 25000 and Mandatory_Task_Completed__c checkbox set to true then delete the task who's name starts with 'Reminder'. */
public class DeleteUnwantedTaskonAccount
{
@InvocableMethod
public static void deletetasks(List<Account> accList)
{
set<Id> accId = new Set<Id>();
List<Task> tskToDelete = new List<Task>();
for(Account acc : accList)
{
accId.add(acc.Id);
}
List<task> taskList = [select id,Account.AnnualRevenue , Account.Mandatory_Task_Completed__c , subject from task Where WhatId In :accId];
for(Task tsk : taskList)
{
if(tsk.Account.AnnualRevenue > 25000 && tsk.Account.Mandatory_Task_Completed__c == true)
{
if(tsk.subject.startswithignorecase('Reminder'))
{
tskToDelete.add(tsk);
}
}
else if(tsk.Account.AnnualRevenue < 25000)
{
tskToDelete.add(tsk);
}
}
if(tskToDelete.size() > 0)
{
delete tskToDelete;
}
}
}
public class DeleteUnwantedTaskonAccount
{
@InvocableMethod
public static void deletetasks(List<Account> accList)
{
set<Id> accId = new Set<Id>();
List<Task> tskToDelete = new List<Task>();
for(Account acc : accList)
{
accId.add(acc.Id);
}
List<task> taskList = [select id,Account.AnnualRevenue , Account.Mandatory_Task_Completed__c , subject from task Where WhatId In :accId];
for(Task tsk : taskList)
{
if(tsk.Account.AnnualRevenue > 25000 && tsk.Account.Mandatory_Task_Completed__c == true)
{
if(tsk.subject.startswithignorecase('Reminder'))
{
tskToDelete.add(tsk);
}
}
else if(tsk.Account.AnnualRevenue < 25000)
{
tskToDelete.add(tsk);
}
}
if(tskToDelete.size() > 0)
{
delete tskToDelete;
}
}
}
As per your requirement, your question is going to be very complicated using Map and your code is already optimized, but I am providing you the basic idea of using Map in your question.
First of all, you have to make two maps as given below:
map<Id,Account> mapOfAccount = new map<Id,Account>(accList); //This map is used to contain Id of Accounts
map<Id,List<Task>> mapofTask = new map<Id,List<Task>>();
List<task> taskList = [select id,Account.AnnualRevenue , Account.Mandatory_Task_Completed__c , subject from task Where WhatId In :mapOfAccount.Keyset()];
for(Task tsk : taskList){
if(mapofTask.containskey(tsk.Id)){
List<Task> lstTsk = mapofTask.get(tsk.Id);
lstTsk.add(tsk);
mapofTask.put(tsk.Id,lstTsk); //In this way you have to fill your Map
}
else{
mapofTask.put(tsk.Id,new List<Task>{tsk});
}
}
List<Task> tskToDelete = new List<Task>();
for (List <Task> listTsk : mapofTask.values()){
for (Task objTask : listTsk){
if(objTask.Account.AnnualRevenue > 25000 && objTask.Account.Mandatory_Task_Completed__c == true)
{
if(objTask.subject.startswithignorecase('Reminder'))
{
tskToDelete.add(objTask);
}
}
else if(objTask.Account.AnnualRevenue < 25000)
{
tskToDelete.add(objTask);
}
if(tskToDelete.size() > 0)
{
delete tskToDelete;
}
}
}
I hope it will help you.
Please select this as Best Answer so that others also get help from it.
Thank You
Ajay Dubedi