You need to sign in to do that
Don't have an account?
Baylor 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:)
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:)
try this code its because of aggregate query on deletion of last record it returns null.
Thanks
Anil.B
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...
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
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
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?:)
Yes your are correct, you need to query on EmailMessageRelation object.
Thanks
Anil.B