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
Iswarya Sekar 9Iswarya Sekar 9 

How to map open task's due date to Next date and open task's status to opportunity's next step field.

public class countOpenTaskOnOpportunites{
public static  boolean firstRun = true;
public static date nextDate;
List<opportunity> updateOpps=new List<opportunity>();

public static void method1(List<Task> taskListNew,List<Task> taskListOld){
Set<Id> oppIds=new Set<id>();
Set<Id> taskNewIDS=new Set<Id>();
List<opportunity> oppUpdate=new List<opportunity>();
List<Task> updateTaskList=new List<Task>();
for(Task t:taskListNew){
if(((string)t.whatId).startsWith('006')){
oppIds.add(t.whatId);
taskNewIDS.add(t.id);
}
}

system.debug('the opportunity list is'+oppIds);
List<Task> allTasksList=[select id,whatid,status from task where id NOT in:taskNewIDS AND whatid in:oppIds ];
system.debug('the task new list is'+allTasksList);
Map<Id,List<Task>> mapOppAndItsRelatedTask=new Map<Id,List<Task>>();
for(Task tsk:allTasksList){
if(!taskNewIDS.contains(tsk.id)){
        if(!mapOppAndItsRelatedTask.containsKey(tsk.whatid))
            mapOppAndItsRelatedTask.put(tsk.whatid, new list<task>{tsk});
            else
          mapOppAndItsRelatedTask.get(tsk.whatId).add(tsk);
          }

}

system.debug('the opp and task map is'+mapOppAndItsRelatedTask);
for(ID optyId:mapOppAndItsRelatedTask.keySet()){
for(Task eachTask:mapOppAndItsRelatedTask.get(optyId)){
system.debug('inside for');
if(eachTask.status!='Completed'){
system.debug('inside for2');
eachTask.status='Completed';
nextDate=eachTask.ActivityDate;
updateTaskList.add(eachTask);
}

}
}
update updateTaskList;

}


}