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

Trigger Assistance Requested

Good day to those reading this.  I am an administrator by design with very little code experience.  Recently, we lost our developer and I am now responsible for taking this challenge on......okay, I will pause here and allow those playing the violin to finish.  :)


I am wanting to create a trigger, based off the information below:


When an Opportunity is created, it creates/converts to Stage=Intake. 


At this point, we wait for one of our Associates to schedule an Appointment for this Opportunity.  Appointment is a custom object (Appointment__c). 


The trigger I am needing assistance with is:


Opportunity stage=Intake and Opportunity field Initial_Appointment_Date__c is NULL. 


When a first appointment record is created and seen associated to the opportunity where the criteria above is true, then populate Initial_Appointment_Date__c with the date that first Appointment record was added and change the Opportunity stage to Scheduled.


Can anyone be so kind as to help me with this trigger?  Thank you in advance....




Is  Appointment linked to the opportunity by Master-Detail or lookup relationship?


Then you can create a trigger on Appointment__c that queries the linked opportunity, and if the criteria are met, updates the opportunity. Could be an after create trigger


Just a lookup relationship.  I originally thought, because of my lack of code knowledge, that I could simply export out all my appointment data, then delete.  Create the Master-Child, re-import my data, and create a SUM roll-up summary field and then build a workflow that bases the above off the greater than 0 criteria to achieve the same result.  But, I wanted to come here and see if it would be easier to get help with a trigger versus do all that work.  :)




You don't need a trigger for this action.  Presuming that Appointments objects are related to Opportunity objects in a 'Master-Detail' relationship, you can use a workflow rule to update the Initial_Appointment_Date__c and set the Stage value on the Opportunity related to an Appointment. The steps for this are below.


1.  Create the workflow rule on the 'Appointment__c' object.


2.  In the workflow rule, use the 'Formula' option to chose when it evaluates, and set your formula to the following:


AND(Appointment__c.Opportunity.StageName = "Intake", ISBLANK(Appointment__c.Opportunity.Initial_Appointment_Date__c))


3.  n the workflow actions, create two new field updates.


The first is to set the Opportunity Stage to 'Scheduled'.  The second is to set the 'Initial_Appointment_Date__c' to a value of 'TODAY()'.


However, if you are using a 'Lookup' relationship between the two records, then you will need a trigger.  The one below should meet your needs.  You will need to create a test method for this trigger before you can install it in your environment.  I leave that as an exercise for you.



trigger updateRelatedOpportunity on Appointment__c (after insert)


// Create an array to store all the updated Opportunities

List<Opportunity> arrUpdatedOpps = new List<Opportunity> ();

// Create a Map to store the Ids for all Opportunities related to Appointments being processed by this trigger

Map<Id,Id> arrOppIds = new Map<Id,Id> ();

// Loop through all the Appointments being processed by the trigger

for (Appointment__c currentApp :


    //If the arrOppIds map does not contain the Opportunity ID,

   if (!arrOppIds.containsKey(currentApp.Opportunity__c))


                // add it




// Fetch all the Opportunities related to Appointments being processed when the trigger fired.

Map<Id,Opportunity> arrOpps = [Select Id, StageName, Initial_Appointment_Date__c from Opportunity where Id in :arrOppIds.keySet()];

// Loop through all the Appointments being processed by the trigger

for (Appointment__c currentApp :


         // check if the current Appointment is related to an Opportunity

         if (currentApp.Opportunity__c == null)


        // if it is, fetch the Opportunity
Opportunity currentOpp = arrOpps.get(currentApp.Opportunity__c); // Check the conditions if (currentOpp.StageName == 'Intake' && currentOpp.Intial_Appointment_Date__c == null) { //If the conditions apply, set the appropriate field values. currentOpp.StageName = 'Scheduled'; currentOpp.Intial_Appointment_Date__c =; // and add it to the updated opportunities array arrUpdatedOpps.add(currentOpp); } } // if the arrUpdatedOpps has any items added, we need to update the Opportunities. if (arrUpdatedOpps.size() > 0) update arrUpdatedOpps; }