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
Tobias GareisTobias Gareis 

How to fill an Opportunity-Field when Creating an Event on that opportunity

Hi,

everytime someone creates an Event for an Opportunity I want to update the Next Steps-Field with the Event-name+Date.
I guess I have to code a Trigger, but I never coded one. Help anyone?

Greets
Best Answer chosen by Tobias Gareis
MithunPMithunP
Hi Tobias Gareis,

Create following trigger on Event, it will update opportunity Next step (Event Subject + Event Activity Date) when ever Event Created/Updated.

Let me know if you have any issues on this.
trigger updateOpportunty on Event (before insert, before update) {
    
    List<Id> OpportuntyIds=new List<Id>();
    List<Opportunity> OpportunitysToUpdate =new List<Opportunity>();
    for(Event t:trigger.new){
            if(String.valueOf(t.whatId).startsWith('006') == TRUE){//check if the task is associated with a Opportunty
                OpportuntyIds.add(t.whatId);
            }
    }
    Map<Id,Opportunity> OpportunityMap = new Map<Id,Opportunity>([SELECT Id,NextStep FROM Opportunity WHERE Id IN :OpportuntyIds]);
    
    For (Event t:trigger.new){
        Opportunity opp = OpportunityMap.get(t.whatId);
        opp.NextStep = t.subject+ ' ' +t.ActivityDate;
        OpportunitysToUpdate.add(opp);
    }
    
    try{
        update OpportunitysToUpdate;
    }catch(DMLException e){
        system.debug('Opportunitys were not all properly updated.  Error: '+e);
    }
}
Best Regards,
Mithun.
 

All Answers

SonamSonam (Salesforce Developers) 
You are right, you will need a  trigger on Event object. the thread on link below has sample code for better understanding - you would need to replace the field names with the ones you wish to update :Kindly check the code 

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AdBxIAK
MithunPMithunP
Hi Tobias Gareis,

Create following trigger on Event, it will update opportunity Next step (Event Subject + Event Activity Date) when ever Event Created/Updated.

Let me know if you have any issues on this.
trigger updateOpportunty on Event (before insert, before update) {
    
    List<Id> OpportuntyIds=new List<Id>();
    List<Opportunity> OpportunitysToUpdate =new List<Opportunity>();
    for(Event t:trigger.new){
            if(String.valueOf(t.whatId).startsWith('006') == TRUE){//check if the task is associated with a Opportunty
                OpportuntyIds.add(t.whatId);
            }
    }
    Map<Id,Opportunity> OpportunityMap = new Map<Id,Opportunity>([SELECT Id,NextStep FROM Opportunity WHERE Id IN :OpportuntyIds]);
    
    For (Event t:trigger.new){
        Opportunity opp = OpportunityMap.get(t.whatId);
        opp.NextStep = t.subject+ ' ' +t.ActivityDate;
        OpportunitysToUpdate.add(opp);
    }
    
    try{
        update OpportunitysToUpdate;
    }catch(DMLException e){
        system.debug('Opportunitys were not all properly updated.  Error: '+e);
    }
}
Best Regards,
Mithun.
 
This was selected as the best answer
Tobias GareisTobias Gareis
Awesome, exactly what I needed. Thanks man
MithunPMithunP
I'm Glad It helped.

Best Regards,
Mithun.
Tobias GareisTobias Gareis
I used your code in the sandbox and everything seemed to work fine. Now I'm trying to deploy it to production, but it gives me a lot of errors. All like "Can not insert because of NullPointer Exception". Help :(
Tess Sta RomanaTess Sta Romana
This code compiles but it does not aupdate NextStep. Any idea why?