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
MattMet86MattMet86 

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:
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

 
Best Answer chosen by MattMet86
ManojjenaManojjena
Hi MattMet,

Is there any relationship with Event and Employees__c ,Are you creating event under employee . If like that 
trigger scheduleReviewDate on Event(after insert){
 Map<Id,Date> empIdWithDate=New Map<Id,Date>();
 for(Event evt :Trigger.New ){
  empIdWithDate.put(evt.Employee__c,evt.StartDateTime );
  }
  List<Employee__c> empList=new List<Employee__c>();
  For(Employee__c emp: [SELECT Id,Date__c FROM Employee__c WHERE Id IN: empIdWithDate.keySet()]){
	  emp.Date__c=empIdWithDate.get(emp.Id);
	  empList.add(emp);
  }
  try{
    Upadte empList;
  }catch(DMLException de){
  
  }
}
Replace Date__c with your Employee__c Date field .


 

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi,
 
List<Contact> lstContact = new List<Contact>();
for(Event e : [select WhoId from Event where Id IN : trigger.new])
{
    
   if(e.WhoId.getSobjectType() == Contact.sobjectType)
        lstContact.add(new Contact(Id = WhoId, Scheduled_Review_Date__c = 
date.newinstance(e.StartDateTime.year(), e.StartDateTime.month(), e.StartDateTime.day());
);
}

update lstContact;


 
MattMet86MattMet86
Thanks for the quick reply but I can't get that code to save in SF. Keeps telling me that a right paren is missing on line 8. 

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.
Pankaj_GanwaniPankaj_Ganwani
trigger scheduleReviewDate on Event(after insert)
{
     List<Contact> lstContact = new List<Contact>();

for(Event e : [select WhoId, StartDateTime from Event where Id IN : trigger.new])

{     

   if(e.WhoId.getSobjectType() == Contact.sobjectType)//checking whoId belongs to Contact

       lstContact.add(new Contact(Id = WhoId, 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

}

update lstContact;//updating contact list

}
Please try this code.
MattMet86MattMet86
Still no joy.

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. 

User-added image
Pankaj_GanwaniPankaj_Ganwani
HI,

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
MattMet86MattMet86
I'm sorry but I am now totaly lost. I cannot figure out where you are getting "Contact" from. The two objects I am interacting with are Employees__c and Event. The Employees__c object is a custom object that has NO relation to the native object of "Contacts". 
ManojjenaManojjena
Hi MattMet,

Is there any relationship with Event and Employees__c ,Are you creating event under employee . If like that 
trigger scheduleReviewDate on Event(after insert){
 Map<Id,Date> empIdWithDate=New Map<Id,Date>();
 for(Event evt :Trigger.New ){
  empIdWithDate.put(evt.Employee__c,evt.StartDateTime );
  }
  List<Employee__c> empList=new List<Employee__c>();
  For(Employee__c emp: [SELECT Id,Date__c FROM Employee__c WHERE Id IN: empIdWithDate.keySet()]){
	  emp.Date__c=empIdWithDate.get(emp.Id);
	  empList.add(emp);
  }
  try{
    Upadte empList;
  }catch(DMLException de){
  
  }
}
Replace Date__c with your Employee__c Date field .


 
This was selected as the best answer
MattMet86MattMet86
Hello Manoj Kumar jena,

I updated the code to this using my fields on Employees__c
 
trigger scheduleReviewDate on Event(after insert){
 Map<Id,Date> empIdWithDate=New Map<Id,Date>();
 for(Event evt :Trigger.New ){
  empIdWithDate.put(evt.Employees__c,evt.StartDateTime );
  }
  List<Employees__c> empList=new List<Employees__c>();
  For(Employees__c emp: [SELECT Id,Scheduled_Review_Date__c FROM Employees__c WHERE Id IN: empIdWithDate.keySet()]){
      emp.Scheduled_Review_Date__c=empIdWithDate.get(emp.Id);
      empList.add(emp);
  }
  try{
    Update empList;
  }catch(DMLException de){
  
  }
}

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". 
ManojjenaManojjena
Hey ,

Click on setup >Customise >ActivityCustomFields > Check the lookup field name in Event which is to Employee add that filed Name .
MattMet86MattMet86
The "Related To" field (API "What") is the lookup field. 

Here are some screenshots that might help. 

Event Fields
User-added image

Here is an actual Event for an Employees__c record. 
User-added image
MattMet86MattMet86
I got the code to save but nothing happens on the Employee object when I create the event. I did a debug log and the trigger is firing but something I noticed is that the Query Rows = 0. Any thoughts? 
 
trigger scheduleReviewDate on Event(after insert){
 Map<Id,Datetime> empIdWithDate=New Map<Id,Datetime>();
 for(Event evt :Trigger.New ){
  empIdWithDate.put(evt.What.Id,evt.StartDateTime );
  }
  List<Employees__c> empList=new List<Employees__c>();
  For(Employees__c emp: [SELECT Id,Scheduled_Review_Date__c FROM Employees__c WHERE Id IN: empIdWithDate.keySet()]){
      emp.Scheduled_Review_Date__c=empIdWithDate.get(emp.Id);
      empList.add(emp);
  }
  try{
    Update empList;
  }catch(DMLException de){
  
  }
}

 
MattMet86MattMet86
If I replace "evt.What.Id" with a string value of a specific record the trigger works. So the Id that is being collected is either the wrong one or the wrong type or something. 
 
MattMet86MattMet86
figured it out! 

It is evt.WhatId not evt.What.Id
 
trigger scheduleReviewDate on Event(after insert){
 Map<Id,Datetime> empIdWithDate=New Map<Id,Datetime>();
 for(Event evt :Trigger.New ){
  empIdWithDate.put(evt.WhatId,evt.ActivityDate );
  }
  List<Employees__c> empList=new List<Employees__c>();
  For(Employees__c emp: [SELECT Id,Scheduled_Review_Date__c FROM Employees__c WHERE Id IN: empIdWithDate.keySet()]){
      emp.Scheduled_Review_Date__c=empIdWithDate.get(emp.Id);
      empList.add(emp);
  }
  try{
    Update empList;
  }catch(DMLException de){
  
  }
}

 
ManojjenaManojjena
Hey Still it will  not work properly ,Can u do one thing just open one Employee record and paste one record Id here I will tell you some thing .
Pankaj_GanwaniPankaj_Ganwani
Hi Manoj and Matt,

I also would like to share some more optimized code:
 
trigger scheduleReviewDate on Event(after insert){
   Map<Id,Employee__c> mapIdToEmp = new Map<Id,Employee__c>();
   for(Event e: [select Employee__c, StartDateTime from Even where Id In : Trigger.new])
{
if(e.Employee__c!=null)        
   mapIdToEmp.put(e.Employee__c, new Employee__c(Id = e.Employee__c, Date__c = e.StartDateTime));
}

update mapIdToEmp.values();

 
MattMet86MattMet86
Pankaj, that code doesn't work. 

The code that Manoj made for me works after I updated a few items on it. 
 
Pankaj_GanwaniPankaj_Ganwani
Okay, I just optimized the code as shared by him. I am not aware about the latter changes made by you.
ManojjenaManojjena
Hey MattMet ,

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 .
MattMet86MattMet86
Final, well for now, code
 
trigger scheduleReviewDate on Event(after insert){
 Map<Id,Datetime>    event_EmpIdWithDate    =    New Map<Id,Datetime>();
 for(Event evt :Trigger.New ){
  event_EmpIdWithDate.put(evt.WhatId,evt.StartDateTime );
  }
  List<Employees__c>    empList    =    new List<Employees__c>();
  For(Employees__c     emp: [SELECT Id,Scheduled_Review_Date__c FROM Employees__c WHERE Id IN: event_EmpIdWithDate.keySet()]){
      emp.Scheduled_Review_Date__c    =    event_EmpIdWithDate.get(emp.Id);
      empList.add(emp);
  }
  try{
    Update empList;
  }catch(DMLException de){
  
  }
}

 
ParidhiBindalParidhiBindal

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'.

 

MattMet86MattMet86
Paridhi Bindal, I would recommend using process builder so something as simple as this. Very easy and visual to accomplish. 

Is there a lookup or master detail relationship between CourseOfferring__c and CourseConnection__c? 
ParidhiBindalParidhiBindal
Hi Matt, yah I am working with HEDA. It provides lookup relationship between course connection and course offerings by course offering id.
MattMet86MattMet86
Without being able to see the structure of the objects and relationships I can't do a lot to help. I would recommend that you check out SFDC99.com as it has a great training section for writing your first trigger. http://www.sfdc99.com/beginner-tutorials/ *Matt Metzinger* Systems Developer BCInsourcing| 6363 College Blvd | Suite 500| Overland Park, KS 66211 | 913.652.2087 | For urgent support please call our support line at (888) 739-1144.