Sunday 18 December 2016

Custom roll up summary on lookup _ count number of attachments attached on the tasks.

 Count number of attachments attached on the tasks.

 the field (NumberOfAttachments__c)updates when you edit the attachment and save the attachment. Also works if deleting the attachment updates the field. 

trigger CountAttachment on Attachment (after insert, after update, after delete, after undelete) {
    // Contains the IDs of all the parent tasks
    Set<Id> parentTaskIdSet = new Set<id>();

    if (trigger.new != null)
    {
        for (Attachment a: trigger.new)
        {
            parentTaskIdSet.add(a.parentId);
        }
    }
    
    if (trigger.old != null)
    {
        for (Attachment a: trigger.old)
        {
            parentTaskIdSet.add(a.parentId);
        }
    }    
    
    // List of tasks that needs to be updated
    List<Task> parentTaskList = [SELECT id, (SELECT id FROM Attachments) FROM Task WHERE id in: parentTaskIdSet];
    
    for (Task t: parentTaskList)
    {
        t.NumberOfAttachments__c = t.Attachments.size();
    }
    
    update parentTaskList;
}
-----------------------------------------------------------------------------
when you create attachments on a task, you need to edit the task, create the attachments, then save the task. When you are saving the task, we are probably writing over the trigger's update on task. So to fix this we will actually need to write another trigger, this time on Task. And since we will be updating task in the task trigger, we need a class to prevent recursion. Here's what the trigger on Task would look like:

-------------------------------------------------------------------------------
trigger UpdateAttachmentCount on Task (after insert, after update) {
    
    if (checkRecursive.runOnce())
    {
        List<Task> taskList = new List<Task>();
        Set<ID> taskIDSet = new Set<ID>();
        
        if (trigger.old != null)
        {
            for (Task t: trigger.old)
            {
                taskIDSet.add(t.ID);
            }
        }
        
        if (trigger.new != null)
        {
            for (Task t: trigger.new)
            {
                taskIDSet.add(t.ID);
            }
        }
        
        // Query for the attachment children of the tasks
        taskList = [SELECT id, (SELECT id FROM attachments) FROM Task WHERE ID in: taskIDSet];
    
        for (Task t: taskList)
        {
            t.NumberOfAttachments__c = t.Attachments.size();
        }
        
        update taskList;
    }
-----------------------------------------------------------------------------

the class that prevents recursion looks like:
------------------------------------------------------------------------------
public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
        if (run){
            run = false;
            return true;
        }
        else
        {
            return run;
        }
    }
}

2 comments:

  1. Excellent information with unique content and it is very useful to know about the information based on blogs Salesforce Training and Placement | Salesforce Developer Training

    ReplyDelete
  2. Why we need check Trigger.old value in Task Trigger (after update, after insert).
    Trigger.new value not enought?

    ReplyDelete

customize omni channel logic to distribute cases based on Case Creation Date

Omni Channel queues distributes cases, based on Date/Time the case is assigned to the queue. we can customize this logic to look for some ...