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
Wes McCarthyWes McCarthy 

Help with Task Trigger across > 1 parent object

Hi there,

Im trying to write a Task trigger which checks the parent object type of the task.  Once the type is known, there are steps of logic e.g.  If Opportunity Task - do this.  If Lead Task - do that.

Im trying to save my code, but keep getting the following error:  
Error: Compile Error: Variable does not exist: ObjectTaskMap at line 48 column 17

I dont know why its telling me that the variable doesnt exist.  Can anyone help, or point me in the direction of similar code?  My code is below.  

Thanks in advance!!

trigger TaskTrigger on Task (before insert, before update) 
{
    string str = '';
    
    List<Id> ObjectIds=new List<Id>();
    for(Task t:trigger.new)
    {
        if(t.Status=='Completed')
        {
            if(t.whatId != null && String.valueOf(t.whatId).startsWith('006')==TRUE) //check if the task is associated with an Opp
            {
                ObjectIds.add(t.whatId);
                str = 'Opportunity';
                //System.debug('In here:' + t.whatid);
            }//if 2
            
            
            Else if(t.whoId != null && String.valueOf(t.whoId).startsWith('00Q')==TRUE) //check if the task is associated with an Opp
            {
                ObjectIds.add(t.whoId);
                str = 'Lead';
                //System.debug('In here:' + t.whoid);
            }//if 2
                
        }//if 1
    }//for
     

    if(str == 'Opportunity')
    {
        map<id,Opportunity> ObjectTaskMap = new map<id,Opportunity>([SELECT Id, Situation__c, Problem__c, Implication__c, Need_Payoff__c , StageName, (Select ID From OpportunityLineItems) FROM Opportunity WHERE Id IN :ObjectIds]);
    
        List<OpportunityContactRole> OCR = [select id from OpportunityContactRole where OpportunityId IN :ObjectIds];
    }
    
    Else If(str == 'Lead')
    {
        map<id,Lead> ObjectTaskMap = new map<id,Lead>([SELECT Id, Description, Status FROM Lead WHERE Id IN :ObjectIds AND IsConverted=FALSE]);
    }
       
    
    for(Task tsk:trigger.new)
    {   
      if(ObjectTaskMap.keyset().contains(tsk.whatid))
      {
            if(tsk.Status == 'Completed' && tsk.Subject == 'Communicate with Client')
            {
                ObjectTaskMap.get(tsk.whatid).StageName = '2-Identified';
                //System.debug('Communicate with Client:' + ObjectTaskMap.get(tsk.whatid).StageName);
            }
            
            Else if( tsk.Status == 'Completed' && tsk.Subject == 'Validate Opportunity')
            {
                If (ObjectTaskMap.get(tsk.whatid).Situation__c == Null ||  ObjectTaskMap.get(tsk.whatid).Problem__c == Null || ObjectTaskMap.get(tsk.whatid).Implication__c == Null || ObjectTaskMap.get(tsk.whatid).Need_Payoff__c == Null)
                {
                    tsk.adderror('Task cannot be completed until the following Opportunity fields have been populated: Situation, Problem, Implication, Need & PayOff');
                }
                Else
                {
                    ObjectTaskMap.get(tsk.whatid).StageName = '3-Validated';
                }
                    
            }
            
                    
            Else if(tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportunity and Identify Contact Roles')
            {
                If (ObjectTaskMap.get(tsk.whatid).OpportunityLineItems.isEmpty() || OCR.size()==0) 
                {
                    tsk.adderror('Task cannot be completed until Contact Roles AND Products have been added to the Opportunity');
                }
                Else
                {
                    ObjectTaskMap.get(tsk.whatid).StageName = '4-Qualified';
                }
            }
            
            Else if(tsk.Status == 'Completed' && tsk.Subject == 'Create quote and submit to Client')
            {
                ObjectTaskMap.get(tsk.whatid).StageName = '5-Proposal';
            }
            
            Else if(tsk.Status == 'Completed' && tsk.Subject == 'Proposal Outcome')
            {
                If (tsk.Proposal_Outcome__c == 'Commercial Agreement')
                {
                    ObjectTaskMap.get(tsk.whatid).StageName = '6-Commercial Agreement';
                }
                Else If (tsk.Proposal_Outcome__c == 'Lost')
                {
                    If (tsk.Loss_Abandonment_Reason__c == Null)
                    {
                        tsk.adderror('Please enter a reason for Loss / Abandonment');
                    }
                    Else
                    {
                        ObjectTaskMap.get(tsk.whatid).StageName = '9-Lost';
                        ObjectTaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
                        ObjectTaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
                    }
                }
                Else
                {
                    If (tsk.Loss_Abandonment_Reason__c == Null)
                    {
                        tsk.adderror('Please enter a reason for Loss / Abandonment');
                    }
                    Else
                    {
                        ObjectTaskMap.get(tsk.whatid).StageName = '8-Abandoned';
                        ObjectTaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
                        ObjectTaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
                    }
                }
            }
            
            Else if(tsk.Status == 'Completed' && tsk.Subject == 'Opportunity Outcome')
            {
                If (tsk.Opportunity_Outcome__c == 'Won')
                {
                    ObjectTaskMap.get(tsk.whatid).StageName = '7-Won';
                }
                Else If (tsk.Opportunity_Outcome__c == 'Lost')
                {
                    If (tsk.Loss_Abandonment_Reason__c == Null)
                    {
                        tsk.adderror('Please enter a reason for Loss / Abandonment');
                    }
                    Else
                    {
                        ObjectTaskMap.get(tsk.whatid).StageName = '9-Lost';
                        ObjectTaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
                        ObjectTaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
                    }
                }
                Else
                {
                    If (tsk.Loss_Abandonment_Reason__c == Null)
                    {
                        tsk.adderror('Please enter a reason for Loss / Abandonment');
                    }
                    Else
                    {
                        ObjectTaskMap.get(tsk.whatid).StageName = '8-Abandoned';
                        ObjectTaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
                        ObjectTaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
                    }
                }
            }
            
            Else 
            {
                //ObjectTaskMap.get(tsk.whatid).StageName = '7-Won';
            }
      }
        Else If(ObjectTaskMap.keyset().contains(tsk.whoid))
        {
            if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead')
            {
                ObjectTaskMap.get(tsk.whoid).Status = 'Contacted';
            }
            
            //  System.debug('1 condition:' + ObjectTaskMap.get(tsk.whoid).Description == NULL);
            //  System.debug('2 condition:' + tsk.Status == 'Completed');
            //  System.debug('3 condition:' + tsk.Subject == 'Make contact with Lead');
            //  System.debug('task:' + tsk);
            
            Else if(ObjectTaskMap.get(tsk.whoid).Description == Null && tsk.Status == 'Completed' && tsk.Subject == 'Qualify Lead')
            {
                tsk.adderror('Task cannot be completed until the following LEAD fields have been populated: Description');
            }
            Else 
            {
                ObjectTaskMap.get(tsk.whoid).Status = 'Qualified';
            }
        }
    
    
    }
    
    if(str == 'Opportunity')
    {
        List<Opportunity> ObjectList = ObjectTaskMap.values();
        update ObjectList;
    }
    Else if(str == 'Lead')
    {
        List<Lead> ObjectList = ObjectTaskMap.values(); 
        update ObjectList;
    }  
}
meerameera
It seems ObjectTaskMap is defined as a local variable inside if loop. You might need to move it outside and declare as a class variable.