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
Jessie WheelerJessie Wheeler 

Trigger on Contact Record (Cross Object from Task & Event)

Our contact records have 3 custom fields that we would like to update from activities being created/updated:
  1. Meeting Date (Date field)
  2. Email Date (Date field)
  3. Task Date (Date field) 
We already have a field on Activity called "Type" that identifies these 3 types of activities: Meeting (Event), Email and Task (Task)

 

The following is what I'd like to do:

  1. Criteria: When Contact record has an Event created/associated to it

Result: Update Meeting Date field on Contact to the Start Date of the Event (this updates each time there is a new event scheduled - with the latest start date always staying in this field)

  1. Criteria: When Contact record has an Email Task created

Result: Update Email Date field on Contact to the Created on date of the Email (this updates each time there is a new event scheduled - with the latest start date always staying in this field)

  1. Criteria: When Contact record has an Task that is NOT an email (ie Call, etc) created/associated to it

Result: Update Task Date field on Contact to the Due date of the Task (this updates each time there is a new event scheduled - with the latest start date always staying in this field)


 

 


Best Answer chosen by Jessie Wheeler
Hargobind_SinghHargobind_Singh
Hi Jessie, 

Here is some sample code for your requirement 1. You can refer to this and write other triggers. I haven't tested this, but should work with minor tweaks. 

trigger EventTest on Event (after insert) {
	Set<ID> whoIdSet = new Set<ID>(); 
	for(Event e: trigger.new){
		if(e.whoId != null){
			whoIdSet.add(e.WhoId); 
		}
	}
	//retreive contacts
	Map<ID, Contact> contactMap = new Map<ID, Contact>([select meeting_Date__c from contact where id in :whoIdSet]); 
	//update meeting date value 
	List<Contact> updContactList = new List<Contact>(); 
	for(Event e: trigger.new){
		if(e.whoId != null && contactMap.containsKey(e.whoId)){
			Contact c1 = contactMap.get(e.whoId); 
			//create date from datetime 
			Date d1 = Date.newInstance( e.StartDateTime.year(), e.StartDateTime.month(), e.StartDateTime.day());  
			c1.meeting_Date__c = d1; 
			updContactList.add(c1); 
		}
	}
	if(updContactList.size()>0){
		update updContactList; 
	}
	
}


All Answers

Andy BoettcherAndy Boettcher
Jessie,

Hello!  Welcome to the developer boards!

Normally - you would use Workflow to do what you're looking for, however you cannot workflow off of tasks and events specifically (boo!).  There is absolutely a solution using Apex Triggers for this.

If you are a developer, have a developer on-staff, or have access to a consultant - this is a fairly straightforward trigger to write.  You have laid out the logic pretty well in your post.  You can check out a blog post of mine to get familiarized with how to write your first Apex Trigger.

http://techman97.wordpress.com/2012/07/18/how-to-write-a-basic-apex-trigger/ (http://techman97.wordpress.com/2012/07/18/how-to-write-a-basic-apex-trigger/" target="_blank)

Good luck!


Hargobind_SinghHargobind_Singh
Hi Jessie, 

Here is some sample code for your requirement 1. You can refer to this and write other triggers. I haven't tested this, but should work with minor tweaks. 

trigger EventTest on Event (after insert) {
	Set<ID> whoIdSet = new Set<ID>(); 
	for(Event e: trigger.new){
		if(e.whoId != null){
			whoIdSet.add(e.WhoId); 
		}
	}
	//retreive contacts
	Map<ID, Contact> contactMap = new Map<ID, Contact>([select meeting_Date__c from contact where id in :whoIdSet]); 
	//update meeting date value 
	List<Contact> updContactList = new List<Contact>(); 
	for(Event e: trigger.new){
		if(e.whoId != null && contactMap.containsKey(e.whoId)){
			Contact c1 = contactMap.get(e.whoId); 
			//create date from datetime 
			Date d1 = Date.newInstance( e.StartDateTime.year(), e.StartDateTime.month(), e.StartDateTime.day());  
			c1.meeting_Date__c = d1; 
			updContactList.add(c1); 
		}
	}
	if(updContactList.size()>0){
		update updContactList; 
	}
	
}


This was selected as the best answer
Jessie WheelerJessie Wheeler
Thanks hs1, I will try now.
Jessie WheelerJessie Wheeler
Hi Hs1 - First of all, thank you - the meeting date works.

I am stuck right now on the task and email. It is returning an error on line 16 - saying that t.activitydate does not exist . What am I doing wrong.

Also - we have "task detail" as a field and i want to be able to say if its a task and the take type says email, update the email date field, otherwise, enter date into task date field.


 trigger TaskTest on Task (after insert) { 
    Set<ID> whoIdSet = new Set<ID>(); 
    for(Task e: trigger.new){ 
        if(e.whoId != null){
            whoIdSet.add(e.WhoId); 
 } 
    }
//retreive contacts
  Map<ID, Contact> contactMap = new Map<ID, Contact>([select task_Date__c from contact where id in :whoIdSet]); 
    //update task date value  
    List<Contact> updContactList = new List<Contact>(); 
    for(Task e: trigger.new){
        if(e.whoId != null && contactMap.containsKey(e.whoId)){
            Contact c1 = contactMap.get(e.whoId); 
            //create date from datetime 
            Date d1 = Date.newInstance( T.ActivityDate.year(), t.activitydate.month(), t.activitydate.day());  
            c1.Task_Date__c = d1; 
            updContactList.add(c1); 
        } 
    }
    if(updContactList.size()>0){
        update updContactList; 
    }
    } 
Hargobind_SinghHargobind_Singh
Hi Jessie, the object variable on the for  loop is "e", replace the line with this :

Date d1 = Date.newInstance( e.ActivityDate.year(), e.activitydate.month(), e.activitydate.day());

Replacing "t" with "e" shoudl take care of your compile error. 



Jessie WheelerJessie Wheeler
Excellent. It works now. I have one last piece to this that I am sure you can help me.

I have the TASK DATE field and the EMAIL DATE field. Right now the trigger just says "when task is created push into task date field on contact record" - I need it to say "when task is created and the type does not = EMAIL, push into task date field, otherwise push into EMAIL DATE field"

How can I modify this to accomplish that?

 trigger TaskTest on Task (after insert) { 
    Set<ID> whoIdSet = new Set<ID>(); 
    for(Task e: trigger.new){ 
        if(e.whoId != null){
            whoIdSet.add(e.WhoId); 
 } 
    }
//retreive contacts
  Map<ID, Contact> contactMap = new Map<ID, Contact>([select task_Date__c from contact where id in :whoIdSet]); 
    //update task date value  
    List<Contact> updContactList = new List<Contact>(); 
    for(Task e: trigger.new){
        if(e.whoId != null && contactMap.containsKey(e.whoId)){
            Contact c1 = contactMap.get(e.whoId); 
            //create date from datetime 
            Date d1 = Date.newInstance( e.ActivityDate.year(), e.activitydate.month(), e.activitydate.day());   
            c1.Task_Date__c = d1; 
            updContactList.add(c1); 
        } 
    }
    if(updContactList.size()>0){
        update updContactList; 
    }
    }
Jessie WheelerJessie Wheeler
Any help would be greatly appreciated!