You need to sign in to do that
Don't have an account?
Niraj Kumar 9
trigger on task to count the activity history and uodate the contact field Count of Activity
Trigger saves but not updating the contact field, Its Urgent, HELP ME.....
Thanks In advance....
Here is my trigger :
trigger ContactCountActivity on task (after insert, after update) {
map<id,integer> mcontactCount = new map<id,integer>();
integer Count_of_Activity = 0;
if(Trigger.isinsert){
for(task t :trigger.new) {
if(t.whatId != null) {
if(string.valueOf(t.whatId).left(3) == '006') {
if(mcontactCount.containsKey(t.whatId)) {
Count_of_Activity = mcontactCount.get(t.whatId);
}
Count_of_Activity = Count_of_Activity + 1;
mcontactCount.put(t.whatid,Count_of_Activity);
}
}
}
}
if(Trigger.isdelete){
for(task t1 :trigger.old) {
if(t1.whatId != null) {
if(string.valueOf(t1.whatId).left(3) == '006') {
if(mcontactCount.containsKey(t1.whatId)) {
Count_of_Activity = mcontactCount.get(t1.whatId);
}
Count_of_Activity = Count_of_Activity - 1;
mcontactCount.put(t1.whatid,Count_of_Activity);
}
}
}
}
if(mcontactCount.keyset().size()>0) {
list<contact> contactToUpdate = new list<contact>([SELECT id,Count_of_Activity__c FROM contact WHERE Id IN :mcontactCount.keyset()]);
for(contact o :contactToUpdate) {
o.Count_of_Activity__c = mcontactCount.get(o.id);
}
if(contactToUpdate.size()>0) {
update contactToUpdate;
}
}
}
Thanks In advance....
Here is my trigger :
trigger ContactCountActivity on task (after insert, after update) {
map<id,integer> mcontactCount = new map<id,integer>();
integer Count_of_Activity = 0;
if(Trigger.isinsert){
for(task t :trigger.new) {
if(t.whatId != null) {
if(string.valueOf(t.whatId).left(3) == '006') {
if(mcontactCount.containsKey(t.whatId)) {
Count_of_Activity = mcontactCount.get(t.whatId);
}
Count_of_Activity = Count_of_Activity + 1;
mcontactCount.put(t.whatid,Count_of_Activity);
}
}
}
}
if(Trigger.isdelete){
for(task t1 :trigger.old) {
if(t1.whatId != null) {
if(string.valueOf(t1.whatId).left(3) == '006') {
if(mcontactCount.containsKey(t1.whatId)) {
Count_of_Activity = mcontactCount.get(t1.whatId);
}
Count_of_Activity = Count_of_Activity - 1;
mcontactCount.put(t1.whatid,Count_of_Activity);
}
}
}
}
if(mcontactCount.keyset().size()>0) {
list<contact> contactToUpdate = new list<contact>([SELECT id,Count_of_Activity__c FROM contact WHERE Id IN :mcontactCount.keyset()]);
for(contact o :contactToUpdate) {
o.Count_of_Activity__c = mcontactCount.get(o.id);
}
if(contactToUpdate.size()>0) {
update contactToUpdate;
}
}
}
can you please add some debug message and post here. Set debug log and post the log here for below code
please give me this log message where i have added debug.log line.
All Answers
In this trigger you have used trigger.isDelete context varibale but you are not executing this trigger in delete event.
first make the change in first line as :
trigger ContactCountActivity on task (after insert, after update,after delete) {
secondly, here 006 is a key prefix for Opportunity recrds, as you are working on contact records, please use 003 as key prefix in comparision.
change this line
if(string.valueOf(t.whatId).left(3) == '006'){ as belowif(string.valueOf(t.whatId).left(3) == '003') {
please check it and let me know if it solves your issue.
Thanks,
Debasis
Thanks for reply.
I made changes in code, but still not updating the field Count_of_Activity__c (Number data type) in contact.
please use whoId instead of WhatId, because
WhoID refers to people things. So that would be typically a Lead ID or a Contact ID
WhatID refers to object type things. That would typically be an Account ID or an Opportunity ID
i have modified your code as below. please let me know it helps you.
Still not updating.
can you please add some debug message and post here. Set debug log and post the log here for below code
please give me this log message where i have added debug.log line.
Thanks for the help.
After two changes, Its work.
1) Assign County of activity (on contact object) field value to 0.
2) o.Count_of_Activity__c = o.Count_of_Activity__c + mopportunityCount.get(o.id);
here is the trigger. can u please provide me the test classes.
map<id,integer> mopportunityCount = new map<id,integer>();
integer Countevent =0;
if (Trigger.isafter){
if(Trigger.isinsert){
for(event e :trigger.new) {
if(e.whatId != null) {
if(string.valueOf(e.WhatId).left(3) == '006') {
if(mopportunityCount.containsKey(e.WhatId)) {
Countevent = mopportunityCount.get(e.WhatId);
}
Countevent = Countevent + 1;
mopportunityCount.put(e.whatid,Countevent);
}
}
}
}
if(Trigger.isdelete){
for(event e1 :trigger.old) {
if(e1.WhatId != null) {
if(string.valueOf(e1.WhatId).left(3) == '006') {
if(mopportunityCount.containsKey(e1.WhatId)) {
Countevent = mopportunityCount.get(e1.WhatId);
}
Countevent = Countevent - 1;
mopportunityCount.put(e1.WhatId,Countevent);
}
}
}
}
if(mopportunityCount.keyset().size()>0) {
list<opportunity> opportunityToUpdate = new list<opportunity>([SELECT id,Count_of_Activity__c FROM opportunity WHERE Id IN :mopportunityCount.keyset()]);
for(opportunity o :opportunityToUpdate ) {
o.Count_of_Activity__c = o.Count_of_Activity__c + mopportunityCount.get(o.id);
}
if(opportunityToUpdate.size()>0) {
update opportunityToUpdate;
}
}
}
}
Here is the code its working.
trigger opportunitycountevent on Event (after insert, after delete) {
map<id,integer> mopportunityCount = new map<id,integer>();
integer Countevent =0;
if (Trigger.isafter){
if(Trigger.isinsert){
for(event e :trigger.new) {
if(e.whatId != null) {
if(string.valueOf(e.WhatId).left(3) == '006') {
if(mopportunityCount.containsKey(e.WhatId)) {
Countevent = mopportunityCount.get(e.WhatId);
}
Countevent = Countevent + 1;
mopportunityCount.put(e.whatid,Countevent);
}
}
}
}
if(Trigger.isdelete){
for(event e1 :trigger.old) {
if(e1.WhatId != null) {
if(string.valueOf(e1.WhatId).left(3) == '006') {
if(mopportunityCount.containsKey(e1.WhatId)) {
Countevent = mopportunityCount.get(e1.WhatId);
}
Countevent = Countevent - 1;
mopportunityCount.put(e1.WhatId,Countevent);
}
}
}
}
if(mopportunityCount.keyset().size()>0) {
list<opportunity> opportunityToUpdate = new list<opportunity>([SELECT id,Count_of_Activity__c FROM opportunity WHERE Id IN :mopportunityCount.keyset()]);
for(opportunity o :opportunityToUpdate ) {
o.Count_of_Activity__c = o.Count_of_Activity__c + mopportunityCount.get(o.id);
}
if(opportunityToUpdate.size()>0) {
update opportunityToUpdate;
}
}
}
}
Thanks for the help.
regards,
Niraj Kumar