You need to sign in to do that
Don't have an account?

Create a trigger to update a field in contact/account record when a field in task record is changed
Source Object - Activity
Source Field - FT_Emp__c
Destination Object - Contact
Destination Field - Employees__c
I want Employees__c to be equal to FT_Emp__c when FT_Emp__c is updated.
Can anybody help me complete the above the code snippet?
Source Field - FT_Emp__c
Destination Object - Contact
Destination Field - Employees__c
trigger Update_employee_count_in_contact on Task (after insert, after update) { // set up lists you will need List<Contact> consToUpdate = new List<Contact>(); Map<Id, Task> taskMap = new Map<Id, Task>(); // go through the list of tasks that were inserted for (Task t: Trigger.New) { taskMap.put(t.WhoId, t); } if (taskMap.size() > 0) { // get all of the contacts related to the tasks // Code logic to update Employees__c field of Contact field . . . if (consToUpdate.size() > 0) { update consToUpdate; } } }
I want Employees__c to be equal to FT_Emp__c when FT_Emp__c is updated.
Can anybody help me complete the above the code snippet?
try this
All Answers
List<Contact contsToUpdate = new List<Contact>();
Schema.DescribeSObjectResult r = CustomObject__c.sObjectType.getDescribe();
String keyPrefix = r.getKeyPrefix();
for(Task t:trigger.new){
String myIdPrefix = String.valueOf( t.WhoId).substring(0,3);
if(myIdPrefix == keyPrefix){
Contact cont = new Contact();
cont.LastName = '' ; // populate this field, its required
cont.Id = t.WhoId;
cont.Employees__c = t.FT_Emp__c;
contsToUpdate.add(cont);
}
}
update contsToUpdate;
Thanks for the code snippet.
I'm wondering how to populate the LastName. Can it be a contant value? It changes every the trigger is fired. I want only the "contact" which is tagged to that particular "task" is updated.
Please let me know can I access the lastname using id dynamically.
Thanks,
Rakshith
Schema.DescribeSObjectResult r = Contact.sObjectType.getDescribe();
with Contact
If I want to do the same and update Account record. Can I just replace Contact by Account?
Cheers
Thanks for all the help
ERROR:
UpdateEmployeeCountInContact: execution of AfterInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
()
MY CODE:
Any pointers?
Also please let me know if there are any other pit falls like this in the code coz I dont want it to fail again in the future.
Rakshith
I got the following error in the email. Please help.
Apex script unhandled trigger exception by user/organization: 00537000001MTQS/00D00000000hcNe
UpdateEmployeeCountInContact: execution of AfterUpdate
caused by: System.ListException: Duplicate id in list: 0030000000EalYiAAJ
Trigger.UpdateEmployeeCountInContact: line 20, column 1
Rakshith
try this