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
Baylor PeakBaylor Peak 

Trigger is not reflecting all tasks Deleted

Hello all,

I finally got my Trigger to do what I want, almost...

I ran into a snag that I can't figure out why it's occuring.

I've notcied that my Total Touchpoint fields are not reflecting correctly when there are no Activities under Activity History. So let's say Total Contacs is showing 10, I can add and delete Tasks and it updates correctly until I delete the last Task. Once I delete the last Task under Activity History, it leaves 1 for the Total Contacts field.

Beating my head around this....

Here is my whole code and I would love some input on whay it's not working like it should be unless I am missing something?:D

trigger TaskRollupSummaryTrigger on Task (after insert, after update, after delete) {
    Set<ID> contactIdSet = new Set<ID>();
    if(Trigger.isInsert || Trigger.isupdate){
        for (Task t : trigger.new) {
            if (t.whoId != NULL) {
                contactIdSet.add(t.whoId);
            }
        }
    }

    if(Trigger.isDelete){
        For(Task t : trigger.old){
            contactIdSet.add(t.whoId);
        }
    }
    
    if (contactIdSet.size() > 0) {
        AggregateResult[] groupedResults = [SELECT WhoId, Type, Count(ID) FROM Task WHERE WhoId IN :contactIdSet AND Type IN ('Call','Email','In-Person Meeting') GROUP BY WhoId, Type];
        
        Map<ID, Contact> contactMap = new Map<ID, Contact>([SELECT Id, Total_Call_Touchpoints__c FROM Contact WHERE Id IN :contactIdSet]);
    
        for (AggregateResult g : groupedResults) {    
            System.debug('####' + g.get('WhoId') + ' : ' + g.get('expr0'));
            Contact c = contactMap.get((String)g.get('WhoId'));
            
            if ((String)g.get('Type') == 'Call') {
                c.Total_Call_Touchpoints__c = (Decimal)g.get('expr0');
            } else if ((String)g.get('Type') == 'Email') {
                c.Total_Email_Touchpoints__c = (Decimal)g.get('expr0');
            } else if ((String)g.get('Type') == 'In-Person Meeting') {
                c.Total_Meeting_Touchpoints__c = (Decimal)g.get('expr0');
            }
        }
        update contactMap.values();
    }
}

Regards for all your help:)
@anilbathula@@anilbathula@
Hi Baylor,

try this code its because of aggregate query on deletion of last record it returns null.
 
trigger TaskRollupSummaryTrigger on Task (after insert, after update,after delete) {
    Set<ID> contactIdSet = new Set<ID>();
    if(Trigger.isInsert || Trigger.isupdate){
        for (Task t : trigger.new) {
            if (t.whoId != NULL) {
                contactIdSet.add(t.whoId);
            }
        }
    }

    if(Trigger.isDelete){
        For(task t : Trigger.old){
            contactIdSet.add(t.whoId);
        }
    }
    
    if (!contactIdSet.isempty()) {
    
        Map<Id,Decimal>Mapofcall=new Map<ID,Decimal>();
        Map<Id,Decimal>MapofEmail=new Map<ID,Decimal>();
        Map<Id,Decimal>MapofMeeting=new Map<ID,Decimal>();
        
        AggregateResult[] groupedResults = [SELECT WhoId, Type, Count(ID) FROM Task WHERE WhoId IN :contactIdSet AND Type IN ('Call','Email','In-Person Meeting') GROUP BY WhoId, Type];
       
    
        for (AggregateResult g : groupedResults) {  
            
            if ((String)g.get('Type') == 'Call') {
                Mapofcall.put((Id)g.get('Whoid'),(Decimal)g.get('expr0'));
            } else if ((String)g.get('Type') == 'Email') {
               MapofEmail.put((Id)g.get('Whoid'),(Decimal)g.get('expr0'));
            } else if ((String)g.get('Type') == 'In-Person Meeting') {
                MapofMeeting.put((Id)g.get('Whoid'),(Decimal)g.get('expr0'));
            }
        }
       
       list<contact>lstcon=[SELECT Id, Total_Call_Touchpoints__c,Total_Email_Touchpoints__c,Total_Meeting_Touchpoints__c  FROM Contact WHERE Id IN :contactIdSet];
       for(contact c:lstcon){
         c.Total_Call_Touchpoints__c=Mapofcall.get(c.id);
         c.Total_Email_Touchpoints__c=MapofEmail.get(c.id);
         c.Total_Meeting_Touchpoints__c =MapofMeeting.get(c.id);         
       }
       update lstcon;
    }
}

Thanks
Anil.B​​​​​​​
Baylor PeakBaylor Peak

Wow! Almost there. That seemed to be it, but it only works on manually created Tasks. If a user hits the Send an Email button, the Touchpoint field updates correctly when it's created, but not when deleted.

If you delete a Task gereated by the Send an Email button, it leaves the Touchpoint field total the same and does not refelct the deletion. However, if I then go create a manual Task, the Touchpoint field total updates with the new manual task as well reflects that the Send an Email generated Task was deleted and the Touchpoint field is updated appropriately...

Baylor PeakBaylor Peak
Sorry if I wasn't clear but I am still having trouble trying to get this to work. Do you have any more suggestions or is this a limitation of automated tasks resulting in this behavior?
@anilbathula@@anilbathula@
Hi Baylor,

Send email button creates a task but it wont have "Type"  filed to populate data ,so on insertion/deletion of these tasks the trigger wont fire .
As in your logic you are checking for Type IN ('Call','Email','In-Person Meeting');

Thanks
Anil.b

 
Baylor PeakBaylor Peak

Yes, that is my logic I am checking.

I found an object called EmailRelation object that has the emailmessage IDs that correlate to those Outbound Emails Messages but I am not sure

Baylor PeakBaylor Peak

Excuse me, EmailMessageRelation

So I guess what I need to figure out is Query on the Task Object to get my Type data for my Total count then query on the EmailMessageRelation object to get the count of EmailMessageId and then hagve it add that to my Total Email Touchpoints field.

I think?:)

@anilbathula@@anilbathula@
Hi Baylor,

Yes your are correct, you need to query on EmailMessageRelation object.
SELECT Id, EmailMessageId, RelationId, RelationType, RelationAddress, RelationObjectType, CreatedDate, CreatedById, SystemModstamp, IsDeleted FROM EmailMessageRelation

Thanks
Anil.B​​​​​​​
Baylor PeakBaylor Peak
That's what I thought - Let me see how it goes:)