Skip to main content
Hello,

I'm trying to configure Salesforce to add activities (specifically events) that are posted on the account to automatically be posted on the account's open opportunities.

I'm thinking it should be a trigger but not exactly sure beyond that.

Any help would be appreciated.

Thanks,
4 answers
  1. Sep 10, 2021, 9:03 AM
    Hi Wayne,

    Can you please try this piece of code.  Change the opportunity stage as per your requirement.

    Trigger:

     

    trigger EventTrigger on Event (before insert) {

    private static boolean run = true;

    EventTriggerHandler ev= new EventTriggerHandler();

    if(Trigger.isinsert && Trigger.isbefore && run){

    run=false;

    ev.insertevent(Trigger.new);

    }

    }

    Handler:

    public class EventTriggerHandler {

    public void insertevent(List<Event> eventli){

    Set<id> accountids= new set<id>();

    for (Event e : eventli){

    system.debug('into for loop');

    if(e.whatid!=null ){

    system.debug('accountid'+e.whatid);

    accountids.add(e.whatid);

    }

    }

    List<opportunity> opplist=[select id,name,accountid,stageNaME from opportunity where stageNaME!='closed-won' and accountid in :accountids];

    List<Event>eventlist= new List<Event>();

    For(Event e : eventli){

    for(opportunity opp:opplist){

    event ev=new event(OwnerId=e.OwnerId,Subject=e.Subject,WhatId=opp.id,StartDateTime=e.StartDateTime,EndDateTime=e.EndDateTime);

    eventlist.add(ev);

    }

    }

    insert eventlist;

    }

    }

    If this solution helps, Please mark it as best answer.

    Thanks,

     
0/9000