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 

Task Trigger across < 1 Parent Object

Hi there,

Im trying to write a Task Trigger that identifies the parent object type of the task, and then executes some logic depending on the type e.g. If type = Opportunity, do this.  If Type = Lead, do that.

When i try to save my code, i get the following error:
Error: Compile Error: Variable does not exist: ObjectTaskMap at line 48 column 17

I dont know why it is saying this.  Can anybody help me, 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;
    }  
}
bob_buzzardbob_buzzard
You declare ObjectTaskMap inside an if and else block, which means that once the else completes the variable goes out of scope:
 
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]);
    }

You need to declare the variable outside the if statement and just initialise it in the if/else block. As you are storing different types of data, you'll need to declare the map values as generic sObject rather than Lead/Opportunity. You might also need to change the initialisation to new Map<Id, SObject>, but I haven't verified that:
 
    map<id,sObject> ObjectTaskMap;

    if(str == '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')
    {
         ObjectTaskMap new map<id,Lead>([SELECT Id, Description, Status FROM Lead WHERE Id IN :ObjectIds AND IsConverted=FALSE]);
    }

It looks like the OCR list will be similar too, as you are initialising it in the if statement but then not using it.