Thursday 7 August 2014

Trigger to count the Number of Open Task in Opportunity

Below is one of the example code which helps to get the count of open Task in related Opportunity



Explanation of the Trigger

Code Block 1:-

trigger OpenTaskCount on Task (after insert, after update, after delete, after undelete) 

This Trigger fires on after insert, update, delete and undelete events on Task object
similarly, this trigger can be used on any child object:-

trigger <TriggerName> on ChildObject (after insert, after update, after delete, after undelete)

Code Block 2:-

List<Task> taskList = (Trigger.isInsert|| Trigger.isUnDelete) ? Trigger.new : Trigger.old;
 

If the event is "after Insert" or "after undelete", then the list takes the value Trigger.new or else it takes Trigger.Old
similarly,

List<ChildObject> lstChild = (Trigger.isInsert|| Trigger.isUnDelete) ? Trigger.new : Trigger.old;

Code Block 3:-

List<Id> taskIds = new List<Id>();
    for (Task tsk : taskList) {
        taskIds.add(tsk.WhatID);
    }

This above code helps to get the RelatedTo Id i.e The Opportunity Id to which this task belongs.
similarly, get the parent id by using the following

List<Id> ChildIds = new List();
    for (ChildObject chd : ChildList) {
        ChildIds.add(chd.ParentFieldname);
    }

Code Block 4:-

 List<Opportunity> oppList = [
            select
                id,
                (select id, WhatID, IsClosed from Tasks),
                 Open_Tasks__c,Total_Tasks__c
            from
                Opportunity
            where
                id in :taskIds];

The above code is the SubQuery which gives us the Number of task records present in that opportunity.
(select id, WhatID, IsClosed from Tasks) is the query which gives the task record and important to note here that "Tasks" is not the Object name, it is the Child Relationship Name.
To get that name, click on Relatedto field in object Task and check the ChildRelationship name.
similarly,

 
List<ParentObject> ParentList = [
            select
                id,
                (select FieldNames from ChildRelationshipName),
                
            from
                ParentObject
            where
                id in :ChildIds];
 
For Custom Object, append the ChildRelationshipName with "__r" i.e ChildRelationshipName__r

Code Block 5:-

 for (Opportunity opp : oppList) {
        Integer count = 0;
        for(Task tsk : opp.Tasks)
         {
           if(tsk.WhatId != null  && !tsk.IsClosed) 
           count += 1;
         }  
              
        opp.Total_Tasks__c = opp.Tasks.size();
        opp.Open_Tasks__c = count;
    }
    update oppList;  

The above code gives us the logic to rollup and count the Open Task.
similarly,

 for(Parentobject parent : ParentList) {
        Integer count = 0;
        for(Integer i =0;i < opp.ChildRelationshipName.size(); i++)
        {
         if(opp.ChildRelationshipName[i].FieldName != null  && !opp.ChildRelationshipName[i].IsClosed) 
          count = count + 1;
          
        }
        parent.FieldName = count;
    }
    update ParentList; 
 

Note:- So in any Case if you want to write a Trigger which updates the Parent object field, the above code can do the trick.

No comments:

Post a Comment