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
Sachin K 13Sachin K 13 

Recursive Trigger on Task and Oppty

Dear Friends, 

Need your help, My Question is...

There is a Trigger Code on Task object, Which is going to update a custom field on Opporunity (There is DML Update operation on Task Object for Opportunity). And there is also a trigger code on Opportunity (after Update). So Whenever i create a task, and i am comparing (Task.WhatId==Opp.WhatID), In the Console it is saying that ' Attempt to Derefernce a null Object ',. How to over come on this. How to use Recursive trigger here. Please help me on this. Thank you.  
ManidManid
hai sachin,
                 i know sol but it is right or wrong i dont know but the problem is solved i think. Create a check box in opportunity and and write condition if checkbox is true then only fire the opportunity trigger otherwise don't. white updating field in opportunity checkbox also and make this check box as false . so update trigger is don't fire.
EldonEldon
Hi Sachin please share your code so that we can have a look.
Sachin K 13Sachin K 13
Hi Eldon, here is the code 

//Task Trigger
if(trigger.isAfter)
    {
        if(trigger.isInsert || trigger.isUpdate)
        { 
             RelatedTask.method1(trigger.new);
         }
}
//Task Class: RelatedTask
public static void method1(List<Task> taskRecords){   
try{
      
        List<Task> listTsk=new List<Task>();
        List<Contact> listCont=new List<Contact>();
        List<String> tskGrpName=new List<String>();    
        List<Id> taskWhatId=new List<Id>();
        List<Task> accountTasks=new List<Task>();
       
        for(Task tsk:taskRecords)
        {
            String taskgr=tsk.Account_Group_Task__c;
            Id taskleadId=tsk.WhoId;
                                   
            // if(wId!=null && wId.startsWith('001') && !listTask.contains(tsk.WhatId))
            if(taskgr!=null)
            {   
                tskGrpName.add(tsk.Account_Group_Task__c);
            }             
           
             if(tsk.WhatId!=null)
             {
                 taskWhatId.add(tsk.WhatId);
             }
           
        }                                                   
		
      
listTsk= [SELECT Id, ActivityDate,WhatId,WhoId,Primary_Contact__c,Status,Meeting_Purpose__c,Type,Account_Name__c,
RecordTypeId,Account_Group_Task__c,Last_meeting_90_days__c,Next_meeting_90_days__c FROM Task Where Account_Group_Task__c IN : tskGrpName AND (RecordTypeId=:Account_Activity_RECORD_TYPE_ID OR RecordTypeId=:Opportunity_Activity_RECORD_TYPE_ID) AND (Meeting_Purpose__c='Executive' OR Meeting_Purpose__c='Operational')  AND Status='Completed' AND (Type='F2F' OR Type='Call') Order by ActivityDate];
              
        Map<Id, Account> accMaps=new Map<Id, Account>([Select Id,Last_Onsite_Activity_A_T__c,Last_Remote_Activity_A_T__c,Name from Account Where Id IN:taskWhatId]);
            
     
        Map<Id, Opportunity> oppMaps=new Map<Id, Opportunity>([Select Id,Last_Onsite_Activity_O_T__c,Last_Remote_Activity_O_T__c from Opportunity where Id IN:taskWhatId]);    
                
        Map<Id, Account> actn = new Map<Id, Account>();
        Map<Id, Opportunity> opps = new Map<Id, Opportunity>();
     
        for(Task tk:listTsk)
        {   
            if(tk.Type=='F2F')
            {
               if(accId!=null)
                {  System.debug('Account Name is ' + accId.Name);
                        System.debug('Task Type ' +tk.Type);
                   		 if(tk.WhatId==accId.Id)
                			{
                   				accId.Last_Onsite_Activity_A_T__c=tk.ActivityDate;
                			}
                 }
                else if(oppId!=null)
                 {
                     if(tk.WhatId==oppId.Id){
                     oppId.Last_Onsite_Activity_O_T__c=tk.ActivityDate;
                 } 
					}
            }
            if(tk.Type=='Call')
            {
             if(accId!=null)
                {  System.debug('Account Name is ' + accId.Name);
                      System.debug('Task Type ' +tk.Type);
                   		 if(tk.WhatId==accId.Id)
                			{
                                accId.Last_Remote_Activity_A_T__c=tk.ActivityDate;
                			}                            
                    }
                else if(oppId!=null)
                {
                    if(tk.WhatId==oppId.Id){
                     oppId.Last_Remote_Activity_O_T__c=tk.ActivityDate;
                                System.debug('Opp Last Remote Activity ' +  oppId.Last_Remote_Activity_O_T__c);
                 }
                }
            }
             }
        update oppMaps.values();
        update accMaps.values();
        
   
    } catch(DmlException de)
    {
        System.debug('Error While Executing '+ de.getLineNumber());
    }
}
----------------------------------------------------------------------------------------------------------------
//Opportunity Trigger
if(trigger.isAfter)
    {    
        if(trigger.isUpdate)
        {
          CustomOpportunity.findRecentActivityDate(trigger.new);  
        }
    }


//Opportunity Class : CustomOpportunity
public static void findRecentActivityDate(List<Opportunity> opptys)
    { 
  try{
        List<Opportunity> listOpp=new List<Opportunity>();
        Set<Id> oppIds=new Set<Id>();
        Set<Id> accIds=new Set<Id>();
        List<String> accountGroupId=new List<String>();
        for(Opportunity ops:opptys)
        {
            oppIds.add(ops.Id);
            accIds.add(ops.AccountId);
            accountGroupId.add(ops.Account_Group_Id_Opp__c);
        }

        Map<Id, Opportunity> oppsOnsite=new Map<Id, Opportunity>([select Id,AccountId,Last_Onsite_Activity_O__c,Last_Remote_Activity_O_T__c from Opportunity where Account_Group_Id_Opp__c IN:accountGroupId Order by Last_Onsite_Activity_O__c Desc Limit 1]);

        Map<Id, Opportunity> oppsRemote=new Map<Id, Opportunity>([select Id,AccountId,Last_Onsite_Activity_O__c,Last_Remote_Activity_O_T__c from Opportunity where Account_Group_Id_Opp__c IN:accountGroupId Order By Last_Remote_Activity_O_T__c Desc Limit 1]);
       
 Map<Id, Account> accnts= new Map<Id, Account>([Select Id, Last_Onsite_Activity_A_O__c,Last_Remote_Activity_A_O__c from Account where Id IN:accIds]);
     
        for(Opportunity opp : oppsOnsite.values())
        {
	        Account accId=accnts.get(opp.AccountId);
            accId.Last_Onsite_Activity_A_O__c=opp.Last_Onsite_Activity_O__c;
        }

    	for(Opportunity opp1:oppsRemote.values())
        {
            Account accId1=accnts.get(opp1.AccountId);
            accId1.Last_Remote_Activity_A_O__c=opp1.Last_Remote_Activity_O_T__c;
        }
       update accnts.values();
     }
Catch(DMLException de)
        {
            System.debug('Error While Executing ' + de.getLineNumber());
        }
    }