function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rakshith RamachandraRakshith Ramachandra 

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
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?
Best Answer chosen by Rakshith Ramachandra
R Z KhanR Z Khan
trigger UpdateEmployeeCountInContact on Task (after insert, after update) {
	Map<Id,Contact> contsToUpdate = new Map<Id, Contact>();
	Schema.DescribeSObjectResult r = Contact.sObjectType.getDescribe();
	String keyPrefix = r.getKeyPrefix();
	for(Task t:trigger.new){
 	    If(	 t.WhoId != null){
                String myIdPrefix = String.valueOf( t.WhoId).substring(0,3);
		if(myIdPrefix == keyPrefix){
			Contact cont = new Contact();
			cont.Id = t.WhoId;
			cont.Employees__c = t.Employees_del__c;
			cont.Total_FTE__c = t.FT_Emp__c;
			cont.Total_PTE__c = t.PT_Emp__c;
			contsToUpdate.put(cont.Id, cont);
		}
           }
	}
	if (contsToUpdate.size() > 0)
    {
        update contsToUpdate.values();
    }

try this

All Answers

R Z KhanR Z Khan
inside trigger wirte
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;
Rakshith RamachandraRakshith Ramachandra
Hi R Z Khan,

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
R Z KhanR Z Khan
ignore lastname field. when i wrote it i was assuming u r creating a record. jsut populate the id the employee__c
Rakshith RamachandraRakshith Ramachandra
Understood. So made I the changes and opened the activity made changes to the employee count. But the employee field under contact record did not get updated. Here's my current code
trigger Update_employee_count_in_contact on Task (after insert, after update) {
	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.Id = t.WhoId;
			cont.Employees__c = t.FT_Emp__c;
			contsToUpdate.add(cont);
		}
	}
	if (contsToUpdate.size() > 0)
    {
        update contsToUpdate;
    }
}
Please let me know whats wrong
 
Deepak GulianDeepak Gulian
At line 3 Replace by this
Schema.DescribeSObjectResult r = Contact.sObjectType.getDescribe();
R Z KhanR Z Khan
Oops sorry replace CustomObejct in CustomObject__c.sObjectType.getDescribe()
with Contact
R Z KhanR Z Khan
trigger Update_employee_count_in_contact on Task (after insert, after update) {
	List<Contact> contsToUpdate = new List<Contact>();
	Schema.DescribeSObjectResult r = Contact.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.Id = t.WhoId;
			cont.Employees__c = t.FT_Emp__c;
			contsToUpdate.add(cont);
		}
	}
	if (contsToUpdate.size() > 0)
    {
        update contsToUpdate;
    }
}

 
Rakshith RamachandraRakshith Ramachandra
Thanks Deepak and RZ Khan. It worked perfectly. I wish I can gove both the best answer. 

If I want to do the same and update Account record. Can I just replace Contact by Account?

Cheers
R Z KhanR Z Khan
Yes in describe write Account.sObjectTupe.GetDescrbie() and inside the loop create new account isntances. dont forgt to change your lsit type to List<Account> too
 
Rakshith RamachandraRakshith Ramachandra
Also if you don't mind providing me with the test class for the same. It'd be wonderful. I created a separate thread so I'd be able provide another thumbs up.  

Thanks for all the help
Rakshith RamachandraRakshith Ramachandra
Hi guys, I got an automated email from info@salesforce.com after deploying the above code. Considering you guys have a background on what I did, can you help fix the error?

ERROR:
UpdateEmployeeCountInContact: execution of AfterInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

()

MY CODE:
trigger UpdateEmployeeCountInContact on Task (after insert, after update) {
	List<Contact> contsToUpdate = new List<Contact>();
	Schema.DescribeSObjectResult r = Contact.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.Id = t.WhoId;
			cont.Employees__c = t.Employees_del__c;
			cont.Total_FTE__c = t.FT_Emp__c;
			cont.Total_PTE__c = t.PT_Emp__c;
			contsToUpdate.add(cont);
		}
	}
	if (contsToUpdate.size() > 0)
    {
        update contsToUpdate;
    }
}

Any pointers?
Deepak GulianDeepak Gulian
trigger UpdateEmployeeCountInContact on Task (after insert, after update) {
	List<Contact> contsToUpdate = new List<Contact>();
	Schema.DescribeSObjectResult r = Contact.sObjectType.getDescribe();
	String keyPrefix = r.getKeyPrefix();
	for(Task t:trigger.new){
 	    If(	 t.WhoId != null){
                String myIdPrefix = String.valueOf( t.WhoId).substring(0,3);
		if(myIdPrefix == keyPrefix){
			Contact cont = new Contact();
			cont.Id = t.WhoId;
			cont.Employees__c = t.Employees_del__c;
			cont.Total_FTE__c = t.FT_Emp__c;
			cont.Total_PTE__c = t.PT_Emp__c;
			contsToUpdate.add(cont);
		}
           }
	}
	if (contsToUpdate.size() > 0)
    {
        update contsToUpdate;
    }
Try this!
Rakshith RamachandraRakshith Ramachandra
It worked. Thanks Deepak. You are a life saver.
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
Rakshith RamachandraRakshith Ramachandra
Hi Deepak,

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
R Z KhanR Z Khan
trigger UpdateEmployeeCountInContact on Task (after insert, after update) {
	Map<Id,Contact> contsToUpdate = new Map<Id, Contact>();
	Schema.DescribeSObjectResult r = Contact.sObjectType.getDescribe();
	String keyPrefix = r.getKeyPrefix();
	for(Task t:trigger.new){
 	    If(	 t.WhoId != null){
                String myIdPrefix = String.valueOf( t.WhoId).substring(0,3);
		if(myIdPrefix == keyPrefix){
			Contact cont = new Contact();
			cont.Id = t.WhoId;
			cont.Employees__c = t.Employees_del__c;
			cont.Total_FTE__c = t.FT_Emp__c;
			cont.Total_PTE__c = t.PT_Emp__c;
			contsToUpdate.put(cont.Id, cont);
		}
           }
	}
	if (contsToUpdate.size() > 0)
    {
        update contsToUpdate.values();
    }

try this
This was selected as the best answer
Rakshith RamachandraRakshith Ramachandra
Got the error - Save error: Method does not exist or incorrect signature: [List&lt;Contact&gt;].values()
 
R Z KhanR Z Khan
did you change the contsToUpdate to a map as in the code above?
Rakshith RamachandraRakshith Ramachandra
I made the changes. Everything looks good. Hope it holds. Thanks RZ
R Z KhanR Z Khan
Your welcome Rakshith, glad i could help :)