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
sekhar 131sekhar 131 

Display the count of Emails, Attachments, Tasks with Upcoming and Overdue based on the User. Need sorting functionality as well using visualforceg

ShivankurShivankur (Salesforce Developers) 
Hi Sekhar,

You can make use of following type of piece of code to get the count of attachments over a task:
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;
}

You may need to modify the code a little bit to fit in the requirement to add emails count as well as getting differentiated values as Upcoming and Overdue based on user.

Hope above information helps. Please mark as Best Answer so that it can help others in future.

​​​​​​​Thanks.
Samar JhaSamar Jha
3. Visualforce Page:
    Display the count of Emails, Attachments, Tasks with Upcoming and Overdue based on the User. Need sorting functionality as well.

Is this answer is ok for this question sir