You need to sign in to do that
Don't have an account?
Preeti Shetty
Update contact field from activity.
I have a field called "Call_Count__c" which is a number field in Contact. I want that when some one logs a call and if the Subject Category is "Call" or "Update" the Call Count field should increase by 1. for eg if value in call count field is 3 it should become 1. Please help me with a trigger for this.
This is your Trigger,
and this is your trigger handler,
All Answers
You need to write a trigger on Task after update/insert, that loops through trigger.new, checking to make sure that the task has a value for the WhoId and it is a contact. I'm not going to write the whole thing, but this should get you started
trigger UpdateContactOnTaskInsertUpdate on Task (after insert,after update){
List contact_ids = new List();
List update_contacts = new List();
for(Task t : trigger.new){
//if the whoid is a contact and the status is completed
if(t.WhoId instanceOf Contact && t.Status == 'Completed'){
contact_ids.add(t.WhoId);
}
}
//if the id list is not empty
if(!contact_ids.isEmpty()){
loop through associated contacts with completed tasks
for(Contact c : [SELECT fields_to_update FROM Contact WHERE ID IN :contact_ids){
//do updates here and add to list for dml
update_contacts.add(c)
}
}
//do dml
update update_contacts;
}
That will get you started, but you are going to have to fill in your own logic and I would recommend if you are having trouble getting a trigger like this started, to review the apex developer guides. Next time I won't write that much for you, everybody gets one ;)
Best Regards
Naga Kiran
You can try this:
This is your Trigger,
and this is your trigger handler,
Due to some changes in requirement i have updated the above trigger handler given by Prosenjit Sarkar as below
But while deploying in Production it is giving me the following error -
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ActivityTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Class.ActivityTriggerHandler.getContactIds: line 17, column 1 Class.ActivityTriggerHandler.updateCallCount: line 7, column 1 Trigger.ActivityTrigger: line 3, column 1: []
Stack Trace: Class.TestUpdateAccountEngScore.myUnitTest: line 61, column 1
Can you please help me with this.