Skip to main content
Conga Support

Creating an Apex Trigger for a Custom Object as a Process Object and a Step

A trigger is Apex code that executes before or after specific data manipulation language (DML) events occur, such as before object records are inserted into the database, or after records have been deleted. A list of all triggers in your organization is located in Setup under Apex Triggers. You can invoke Apex through the use of triggers. Apex triggers are stored as metadata in the application under the object with which they are associated.

A trigger is Apex code that executes before or after the following types of operations:

  • Insert.
  • Update.
  • Delete.
  • Merge.
  • Upsert.
  • Undelete.

In order for Conga Orchestrate to use a custom object you've created, you need to create a trigger and test class in a sandbox environment and deploy them to your production environment. Salesforce does not allow you to create an apex trigger or apex class directly in a production org. Refer to the Deploy from Sandbox with Change Sets module in Trailhead for instructions on the deployment process.

Follow these steps to create the trigger in a sandbox environment:

  1. In your sandbox environment, select the Settings Cog icon (in Lightning) or your name (in Classic) in the upper right corner of Salesforce.
  2. Select Developer Console.
  3. Select File.
  4. Hover over New and choose Apex Trigger.
  5. Give your trigger a descriptive name. Something like CustomObjectAsStepProcessTrigger.
  6. Choose the custom object from the sObject dropdown.
  7. Select Submit.
  8. Copy/Paste the following code into the new trigger window. Replace the code that was automatically entered when you created the trigger.

    trigger TRIGGERNAME on YOUR_CUSTOM_OBJECT_API_NAME (after insert, before insert, before update, after update, before delete, after delete) {
       
        // gather list of process objects used in steps
        List<sObject> oldTrg = FSTR.ProcessComposerExecution.StepProcessObjects(Trigger.old);
        Map<Id,sObject> oldTrgMap = FSTR.ProcessComposerExecution.StepProcessObjects(Trigger.oldMap);       
        List<sObject> newTrg = FSTR.ProcessComposerExecution.StepProcessObjects(Trigger.new);
        Map<Id,sObject> newTrgMap = FSTR.ProcessComposerExecution.StepProcessObjects(Trigger.newMap);
       
        //after triggers   
        if (Trigger.isAfter) {
            /*
                This part creates first group tasks when new object is created
                In addition, if new criteria are met this will start the new process
            */
            if(Trigger.isInsert) {
                FSTR.ProcessComposerExecution.CreateSteps(Trigger.new); 
                if (newTrg != null && !newTrg.isEmpty()) FSTR.ProcessComposerExecution.UpdateCompletedProcessObjects(newTrg);                                     
            }
            if(Trigger.isUpdate) {
                FSTR.RBPExecution.AfterObjectUpdate(Trigger.old, Trigger.new);
                FSTR.ProcessComposerExecution.CreateStepsOnUpdate(Trigger.old, Trigger.newMap);
                if (newTrg != null && !newTrg.isEmpty()) FSTR.ProcessComposerExecution.UpdateStep(oldTrg, newTrg);
            }
            if(Trigger.isDelete) {
                if (oldTrg != null && !oldTrg.isEmpty()) FSTR.ProcessComposerExecution.UpdateCompletedProcessObjects(oldTrg);                                       
            }
        }
       
        //before triggers
        if (Trigger.isBefore) {
            /*
                The trigger is fired before creation/update of new Business Process
            */
            if (Trigger.isInsert || Trigger.isUpdate){
                if (newTrg != null && !newTrg.isEmpty()) {
                    FSTR.ProcessComposerExecution.PopulateParentObjectName(newTrg);
                    if (oldTrgMap != null && !oldTrgMap.isEmpty())
                        FSTR.ProcessComposerExecution.UpdateCustomStepCompletedDate(newTrg,oldTrgMap);
                }
                if(Trigger.isInsert){
                    FSTR.ProcessComposerExecution.DetermineBusiness(Trigger.new);
                    for(YOUR_CUSTOM_OBJECT_API_NAME__c newRec : Trigger.new) {
                        //set status to open
                        newRec.Status__c = 'Open';
                    }
                }
                FSTR.ProcessComposerExecution.DetermineDefinition(Trigger.new);
            }
            else if(Trigger.isDelete){
                if (oldTrg != null && !oldTrg.isEmpty()) FSTR.ProcessComposerExecution.CheckObjectBeforeDelete(oldTrg); 
                for(YOUR_CUSTOM_OBJECT_API_NAME__c delRec : Trigger.old) {
                    if(delRec.Recurring_Business_Process__c != null)
                        delRec.addError('You cannot delete a YOUR CUSTOM OBJECT LABEL that was generated by a Recurring Business Process.');
                }
            }
        }
    }

     
  9. Replace the TRIGGERNAME in line 1 with the name of your new trigger. This will be the name you chose in step 5 above.
  10. Replace YOUR_CUSTOM_OBJECT_API_NAME with the API name of your custom object.
  11. Replace YOUR CUSTOM OBJECT LABEL with the label of your custom object.
    new-trigger-step-process-code.jpg
  12. Save the trigger by pressing Ctrl+S on your keyboard or by selecting File > Save in the top menu of the Developer Console.
  13. Close the Developer Console window.