You need to sign in to do that
Don't have an account?
Trigger on Contact Record (Cross Object from Task & Event)
- Meeting Date (Date field)
- Email Date (Date field)
- Task Date (Date field)
The following is what I'd like to do:
-
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)
-
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)
-
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)
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.
All Answers
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!
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.
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;
}
}
Replacing "t" with "e" shoudl take care of your compile error.
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;
}
}