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 on Opportunity - trying to reference Opp Product details

Hi there,

I have a task trigger running for Opportunities, that updates the Opp stage based on Task completion.  There is some logic to ensure that certain fields have been entered on the Opportunity, before being allowed proceed to the next stage.  The issue i'm facing is trying to check that certain fields have been input on the Opportunity Products, before proceeding to the next stage of the Opportunity.

Can somebody please tell me how to go about this.  Below is my attempt so far, however i dont know if im on the right track.  
 

Thanks in advance!

trigger TaskTrigger on Task (before insert, before update) 
{
    List<Id> OppIds=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
            {
                OppIds.add(t.whatId);
                //System.debug('In here:' + t.whatid);
            }//if 2
        }//if 1
    }//for
        
    map<id,Opportunity> OpptaskMap = new map<id,Opportunity>([SELECT Id, Situation__c, Problem__c, Implication__c, Need_Payoff__c , Involves_Active_Partners__c, StageName, (Select ID, Software_Deal_Type__c, Software_GPP_Code__c From OpportunityLineItems) FROM Opportunity WHERE Id IN :OppIds]);
    
    List<OpportunityContactRole> OCR = [select id from OpportunityContactRole where OpportunityId IN :oppIds];
    List<OpportunityPartner> OPartner = [select id from OpportunityPartner where OpportunityId in :oppIds];
    
       
    
    for(Task tsk:trigger.new)
    {   
      if(OpptaskMap.keyset().contains(tsk.whatid))
      {
            if(tsk.Status == 'Completed' && tsk.Subject == 'Communicate with Client')
            {
                OpptaskMap.get(tsk.whatid).StageName = '2-Identified';
                //System.debug('Communicate with Client:' + OpptaskMap.get(tsk.whatid).StageName);
            }
            
            Else if( tsk.Status == 'Completed' && tsk.Subject == 'Validate Opportunity')
            {
                If (OpptaskMap.get(tsk.whatid).Situation__c == Null ||  OpptaskMap.get(tsk.whatid).Problem__c == Null || OpptaskMap.get(tsk.whatid).Implication__c == Null || OpptaskMap.get(tsk.whatid).Need_Payoff__c == Null || OpptaskMap.get(tsk.whatid).Involves_Active_Partners__c == Null)
                {
                    tsk.adderror('Task cannot be completed until the following Opportunity fields have been populated: Situation, Problem, Implication, Need & PayOff, Involves Active Partners.');
                }
                Else
                {
                    OpptaskMap.get(tsk.whatid).StageName = '3-Validated';
                }
                    
            }
            
          //  System.debug('1 condition:' + LeadtaskMap.get(tsk.whatid).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(tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportunity and Identify Contact Roles')
            {
                
                If(OpptaskMap.get(tsk.whatid).Involves_Active_Partners__c == 'Yes') 
                { 
                    If (OpptaskMap.get(tsk.whatid).OpportunityLineItems.isEmpty() || OCR.size()==0 || OPartner.size()==0) 
                    {
                        tsk.adderror('Task cannot be completed until Contact Roles, Products AND Partners have been added to the Opportunity');
                    
                    }
                    Else
                    {
                        OpptaskMap.get(tsk.whatid).StageName = '4-Qualified';
                    }
                }
                Else If(OpptaskMap.get(tsk.whatid).Involves_Active_Partners__c == 'No')
                {
                    If (OpptaskMap.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
                    {
                        OpptaskMap.get(tsk.whatid).StageName = '4-Qualified';
                    }
                
                }
                Else
                {
                    //OpptaskMap.get(tsk.whatid).StageName = '4-Qualified';
                }
            }
            
            Else if(tsk.Status == 'Completed' && tsk.Subject == 'Create quote and submit to Client')
            {
                OpptaskMap.get(tsk.whatid).StageName = '5-Proposal';
            }
            
            Else if(tsk.Status == 'Completed' && tsk.Subject == 'Proposal Outcome')
            {
                If (tsk.Proposal_Outcome__c == 'Commercial Agreement')
                {
                    List<OpportunityLineItem>OLI = OpptaskMap.get(tsk.whatid).OpportunityLineItems;
                    for (OpportunityLineItem OppLI : OLI) 
                    {
                        //Am i correct in doing this?
                    
                    }
      {

                    
                    OpptaskMap.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
                    {
                        OpptaskMap.get(tsk.whatid).StageName = '9-Lost';
                        OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
                        OpptaskMap.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
                    {
                        OpptaskMap.get(tsk.whatid).StageName = '8-Abandoned';
                        OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
                        OpptaskMap.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')
                {
                    OpptaskMap.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
                    {
                        OpptaskMap.get(tsk.whatid).StageName = '9-Lost';
                        OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
                        OpptaskMap.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
                    {
                        OpptaskMap.get(tsk.whatid).StageName = '8-Abandoned';
                        OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
                        OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
                    }
                }
            }
            
            Else 
            {
                //OpptaskMap.get(tsk.whatid).StageName = '7-Won';
            }
      }
    } 
    List<Opportunity> OppList = OpptaskMap.values(); 
      update OppList;
      }