How to Use the Loop.LoopMessage Class in an Apex Trigger

Salesforce developers commonly inquire about the ability to trigger a DDP using custom Apex code. Is this possible? Yes! Below I’ll walk you through the basic setup and provide you with sample code. This example will generate an Invoice template and delivery via email when an Opportunity record is edited and the Stage field is ‘Proposal/Price Quote’.

Part 1: Create the DDP & Delivery Option
Part 2: Create the Apex Class
Part 3: Create the Trigger
Part 4: Test!

Part 1: Create the DDP & Delivery Option

The code used in this example will require you to have a DDP with the name ‘Invoice’. Additionally, you will need a Delivery Option on this DDP with the name Email. The Additional To field on the Email delivery option will need to contain a email address OR a field tag for a User/Contacts email address. This email address will receive the generated document when the code is executed so be sure to have access to the inbox.

Part 2: Create the Apex Class

Go to Setup > Develop > Apex Classes > click the New button

Enter your custom code. See sample below:

global class MySender {    public class NoDDPInfoException extends Exception { }        @future(callout=true)    public static void sendRequests(string encodedObjects, string sessionId) {        // CHANGE ANY FILTER (WHERE) CRITERIA NECESSARY        List<Loop__DDP__c> ddps = [SELECT Id, (SELECT Id FROM Loop__Custom_Integration_Options__r WHERE Name=’Email’) FROM Loop__DDP__c WHERE Name=’Invoice’];                // ALTERNATIVELY, DETERMINE WHICH DDP TO RUN FOR EACH RECORD                if (ddps == null || ddps.size() < 1 || ddps[0].Loop__Custom_Integration_Options__r == null || ddps[0].Loop__Custom_Integration_Options__r.size() < 1) {            // CHANGE THE EXCEPTION MESSAGE IF DESIRED            throw new NoDDPInfoException(‘The DDP or Delivery Option specified was not found.’);        }                Map<Id, Opportunity> mySObjects = (Map<Id, Opportunity>)JSON.deserialize(encodedObjects, Map<Id, Opportunity>.class);                Loop.loopMessage lm = new Loop.loopMessage();                // SESSION ID NEEDED IF IT CANNOT BE DETERMINED FROM UserInfo.getSessionId()        lm.sessionId = sessionId;                for (Id sObjectId : mySObjects.keySet()) {            Opportunity mySObject = mySObjects.get(sObjectId);            // ADD A DDP RUN REQUEST            lm.requests.add(new Loop.loopMessage.loopMessageRequest(                sObjectId, // MAIN RECORD ID – SAME OBJECT AS THE DDP RECORD TYPE SPECIFIES                ddps[0].Id,                new Map<string, string>{                    ‘deploy’ => ddps[0].Loop__Custom_Integration_Options__r[0].Id,                    ‘SFAccount’ => mySObject.Account.Id                    // THESE PARAMETERS ARE THE SAME AS THOSE FOUND IN OUR OUTBOUND MESSAGE DOCUMENTATION                    // PLEASE REFERENCE THAT DOCUMENTATION FOR ADDITIONAL OPTIONS                }            ));        }        // SEND ALL DDP RUN REQUESTS IN A SINGLE CALL OUT        lm.sendAllRequests();    }}

Click ‘Save’

Part 3: Create the Trigger

Go to Setup > Customize > Opportunities > Triggers > Click the ‘New’ button

Enter your custom code. See sample below:

trigger SObjectTrigger on Opportunity (after update) {    // CHANGE ANY FILTER (WHERE) CRITERIA NECESSARY    Map<Id, Opportunity> mySObjects = new Map<Id, Opportunity>([        SELECT Id, Account.Id        FROM Opportunity        WHERE Id IN :Trigger.newMap.keySet() AND StageName = ‘Proposal/Price Quote’    ]);        // ALTERNATIVELY, LOOP THROUGH Trigger.new TO DETERMINE WHICH RECORDS TO RUN THE DDP FOR        // SEND DDP REQUESTS IF NECESSARY    if (mySObjects.size() > 0) {        MySender.sendRequests(JSON.serialize(mySObjects), userInfo.getSessionId());    }}

Click ‘Save’

Part 4: Test! 

At this point, you were able to save the Apex Class and Trigger containing the code without receiving an error.  Good job! Now test the code by navigating to a Opportunity record and editing the Stage to ‘Proposal/Price Quote’.

Don’t see the generated document in your inbox? If you do not receive the email containing the generated document there is most likely an issue with the code or the DDP configuration. View the Job Queue by navigating to DDP Admin tab and clicking the ‘Job Queue’ button. If you do not see any information the issue is most likely with your code. Otherwise, you will see the job is either Pending or has an Error.

Scroll to top