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
Nicholas MelonasNicholas Melonas 

How can I trigger an event date to auto populate an oppportunity custom field?

I am new to Apex Coding / Triggers and I am stuck. I'm trying to create a trigger where when an event is created with a specific subject line, it automatically populates an "Appointment Date" custom field on the Opportunity object. Where do I begin? Thank you!
Sumitkumar_ShingaviSumitkumar_Shingavi
You can simply do like below:
Opp.Date__c = System.today() + 10; //Opp is instance of Opportunity
PS: if this answers your question then hit Like and mark it as solution!
Vishnu_SFDCVishnu_SFDC
Try the below trigger.
trigger updateOpp on event(after insert){
List<String> subject = new list<string>();
map<string,date> dates = new map<string,date>();
for(event e : trigger.new)
{
subject.add(e.subject);
dates.put(e.subject,e.appointment_date__c);
}
//get the list of opps based on the subject field
list<opportunity> opp = [SELECT Id,Name From Opportunity Where Name in : subject];
for(opportunity o : opp)
{
 o.Appointment_date__c = dates.get(o.name);
}
update opp;
}
 please like and mark this as solution if this solves you problem.
 

Sumitkumar_ShingaviSumitkumar_Shingavi
You can do this using Workflows simply where your Subject line will be specific Subject and Appointment_date__c will be "Today()+10" or something.

PS: if this answers your question then hit Like and mark it as solution!
Nicholas MelonasNicholas Melonas
@Sumitkumar_Shingavi I would prefer doing a workflow! However I'm having trouble because the event date is on the Event object while my custom field is on the Opportunity object. The workflow isn't allowing me to do a field update on the Opportunity object. Does that make sense? What am I doing wrong?

Sumitkumar_ShingaviSumitkumar_Shingavi
You will need a trigger for this. I thought you have that field in opp record only
Nicholas MelonasNicholas Melonas
@Vishnu_SFDC, I plugged in the trigger and this is the error that appeared: Error: Compile Error: Invalid field appointment_date__c for SObject Event at line 7 column 21. What should I do? Thanks for your help guys. I'm just starting with triggers so I don't really know what I am doing!
Vishnu_SFDCVishnu_SFDC
trigger updateOpp on event(after insert){
List<String> subject = new list<string>();
map<string,date> dates = new map<string,date>();
for(event e : trigger.new)
{
subject.add(e.subject);
dates.put(e.subject,e.appointment_date__c);---------->replace appointment_date__c with the date you want to copy in event object
}
//get the list of opps based on the subject field
list<opportunity> opp = [SELECT Id,Name From Opportunity Where Name in : subject];
for(opportunity o : opp)
{
o.Appointment_date__c = dates.get(o.name);----------------------->replace appointment_date__c with the field in opportunity object.
}
update opp;
}
Nicholas MelonasNicholas Melonas
@Vishnu_SFDC that's great! It saved that time! Just one final question before I test run in my Sandbox. Where do I put what I want for the subject line? I want it to only work if the Subject line contains KT, DC, Join.Me, and/or Proposal. Thank you!!
Vishnu_SFDCVishnu_SFDC
@Nicholas Melonas do you know which opportuity to update if event subject line contains KT, DC, Join.Me, and/or Proposal?
Can you describe this to me?

Nicholas MelonasNicholas Melonas
@Vishnu_SFDC thanks for your help and quick replies! When our appointment schedulers create an appointment for a sales person, they create an event and select the specific opportunity under the "related to" section. In the subject line, they put KT, DC, or Join.Me with the client's last name to indicate that it is an appointment. For example, "KT - Smith" or "DC - Bob Smith." Does that make sense? I want to make sure the event date that transfers over to my opportunity field is the actual appointment date and this is how we identify our appointments in our calendar. Let me know if that helps!
Vishnu_SFDCVishnu_SFDC
Try This.
trigger updateOpp on event(after insert){
List<String> Id = new list<string>();
map<string,date> dates = new map<string,date>();
for(event e : trigger.new)
{
Id.add(e.WhatId);
if((e.subject).comtains('KT') || (e.subject).comtains('DC') || (e.subject).comtains('Join.Me'))
dates.put(e.id,e.appointment_date__c);
}
//get the list of opps based on the subject field
list<opportunity> opp = [SELECT Id,Name From Opportunity Where Name in : id];
for(opportunity o : opp)
{
 o.Appointment_date__c = dates.get(o.Id);
}
update opp;
}


Nicholas MelonasNicholas Melonas
@Vishnu_SFDC thank you so much! It saved but it actually didn't work :-/ Thanks for your help with this. I will try and do what I can to work on it too! I'm not sure what fell through but it didn't work!
CARBCARB
Hi @Nicholas Melonas I am in the same situation than you. Did you find a solution in the end?