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
Merry SMerry S 

Trigger to update a field on the contact based on the last person to log an activity

Hello, I am looking for some help with creating a trigger that will update a field named "Last Person Worked" on the contact based on the assigned user of the newest activity related to the contact. I am new to developing in general and though I wanted to tough this out and figure out for myself the need has become more critical. Any help would be greatly appreciated.
Best Answer chosen by Admin (Salesforce Developers) 
SLockardSLockard

Here is a trigger on Task that I wrote, that will update the Contacts last person worked field every time a new task is inserted. You probably want to write a similar trigger for the Event obejct too, but I believe you can edit this code to do that.

trigger lastPersonWorkedFromTask on Task(after insert)
{
	// 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)
	{
		// if they are related to a contact, add the contact id (whoID) and their values to a map
		if (t.WhoId  != null)
		{
			taskMap.put(t.WhoId, t);
		}
	}
	
	// if the map isnt empty
	if (taskMap.size() > 0)
	{
		// get all of the contacts related to the tasks
		consToUpdate = [SELECT Id, Last_Person_Worked__c FROM Contact WHERE Id IN: taskMap.keySet()];
		// go through the list for each contact
		for (Contact c: consToUpdate)
		{
			// set the last person worked field to the owner of the task
			c.Last_Person_Worked__c = taskMap.get(c.Id).OwnerId;
		}
		
		// if the list of contacts isnt empty, update them
		if (consToUpdate.size() > 0)
		{
			update consToUpdate;
		}
	}
}

 I hope this helps!

All Answers

SLockardSLockard

Here is a trigger on Task that I wrote, that will update the Contacts last person worked field every time a new task is inserted. You probably want to write a similar trigger for the Event obejct too, but I believe you can edit this code to do that.

trigger lastPersonWorkedFromTask on Task(after insert)
{
	// 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)
	{
		// if they are related to a contact, add the contact id (whoID) and their values to a map
		if (t.WhoId  != null)
		{
			taskMap.put(t.WhoId, t);
		}
	}
	
	// if the map isnt empty
	if (taskMap.size() > 0)
	{
		// get all of the contacts related to the tasks
		consToUpdate = [SELECT Id, Last_Person_Worked__c FROM Contact WHERE Id IN: taskMap.keySet()];
		// go through the list for each contact
		for (Contact c: consToUpdate)
		{
			// set the last person worked field to the owner of the task
			c.Last_Person_Worked__c = taskMap.get(c.Id).OwnerId;
		}
		
		// if the list of contacts isnt empty, update them
		if (consToUpdate.size() > 0)
		{
			update consToUpdate;
		}
	}
}

 I hope this helps!

This was selected as the best answer
SFAdmin5SFAdmin5

nice work

Merry SMerry S

This was exactly what I needed. Thank you so much. :)