I am very new to Apex and trying to write a trigger on Task. I appreciate if you can help..
I have a picklist field on Task(name: Result__c), let's say with values A,B,C. If I choose A or B when I create a new task, I want a custom field (Result Count__c) to be updated on the related contact record.
I think;
| Object : Task | | Operation : Insert | | Event : After |
so we need to create the trigger on Task and it needs to be after the insert.
I wrote this but have no idea what else to do or how to continue.
trigger TaskTrigger on Task (after insert) {
If (Trigger.isAfter && Trigger.isInsert) {
for (Task taskRec : Trigger.New) {
List<Contact> conListToUpdate = [SELECT CountCalls__c FROM Contact WHERE Contact.Name = taskRec.WhoId];
if (taskRec.Activity_Call_Result__c = 'Not Home' OR taskRec.Activity_Call_Result__c = 'Left Message') {
conListToUpdate.CountCalls__c = conListToUpdate.CountCalls__c + 1;
}
}
}
}
I tried to select the contact records where the name of the contact is same in the task we are creating so we'd know which contacts to update but just stuck here.
Thanks in advance.