You need to sign in to do that
Don't have an account?
MattMet86
First Trigger Help - Update field on another object
Hi, I am trying to write my first trigger and need some help.
We have a custom object called Employee that has a field named "Scheduled Review Date".
I need to create a trigger that upon creation of an Event record this field is updated with the date from Event in field "StartDateTime" < API name. The Related to (who) field on event is my employee.
I have come up with this so far:
I'm not sure where to go from here. I think I need to query employee and then put the field update but I don't really know how to do this.
I think this is pretty simple so hopefully someone can point me in the right direction.
Thanks!
Matt
We have a custom object called Employee that has a field named "Scheduled Review Date".
I need to create a trigger that upon creation of an Event record this field is updated with the date from Event in field "StartDateTime" < API name. The Related to (who) field on event is my employee.
I have come up with this so far:
trigger ScheduledReviewDate on Event (after insert) { for (Event e: trigger.new){ } }
I'm not sure where to go from here. I think I need to query employee and then put the field update but I don't really know how to do this.
I think this is pretty simple so hopefully someone can point me in the right direction.
Thanks!
Matt
Is there any relationship with Event and Employees__c ,Are you creating event under employee . If like that Replace Date__c with your Employee__c Date field .
All Answers
Can you put some notes in this code telling me what each step is doing? I am looking for more of a training so I can understand what each step is doing.
Another item: Is this referencing the SF native object Contact or is that just the name you used for the list? The record I am writting to is stored on a custom object named Employee.
Use e.WhoId instead of WhoId. I have a question. Is your Contact object is same as Employee object? If this is not, then use : if(e.WhatId.getSobjectType() == Employee__c.sobjectType))
lstContact.add(new Contact(Id = e.WhatId, Scheduled_Review_Date__c =date.newinstance(e.StartDateTime.year(), e.StartDateTime.month(), e.StartDateTime.day())));//storing the Contact in a list with assigning the date value of StartDateTime field to Schedule review date
Is there any relationship with Event and Employees__c ,Are you creating event under employee . If like that Replace Date__c with your Employee__c Date field .
I updated the code to this using my fields on Employees__c
I am getting an error however:
Error: Compile Error: Invalid field Employees__c for SObject Event at line 4 column 21
P.S. you had a small mistype of the word "update".
Click on setup >Customise >ActivityCustomFields > Check the lookup field name in Event which is to Employee add that filed Name .
Here are some screenshots that might help.
Event Fields
Here is an actual Event for an Employees__c record.
It is evt.WhatId not evt.What.Id
I also would like to share some more optimized code:
The code that Manoj made for me works after I updated a few items on it.
You need to add on line more in that trigger to fire only for the event which will create under Employee__c ,Else it will fire for event creation from all object and chances are there to throw error .
Hi I am writing my first trigger. Please help.
I have two objects - CourseOffering and CourseConnection, there is a field in CourseOffering named 'Faculty. I want to write a trigger by which on updating Faculty, Course Connection's field named 'Status' changes to 'Former' from 'Current'.
Is there a lookup or master detail relationship between CourseOfferring__c and CourseConnection__c?