Generating documents from Salesforce Apex triggers

Similar to how you can automatically generate documents from Salesforce flows, you can automatically generate documents from Salesforce Apex triggers once you have installed our Salesforce package and set up the Inkit app in Salesforce. You can generate documents based on an event or on a regular schedule.

In this tutorial, we will show you how to generate a proposal when the Stage of a Sales Opportunity is set to Proposal/Price Quote.

Creating a template and copying its ID

Before you can create an Apex trigger and initiate it, you need to create an Inkit template for the proposal and embed the Salesforce fields into it. To use this template in your Apex trigger, copy the ID of the template from the Inkit web app by selecting Templates in the left sidebar and then clicking the copy button by the template whose ID you want to copy.

Creating an Apex trigger

To create an Apex trigger that will automatically generate a proposal from the Inkit template you created, do the following:

  1. In Salesforce, click the App Launcher.

    In the Search apps and items... field, enter sales and click the Sales app.

  2. In the Sales page, click the Opportunities tab.

  3. In the Opportunity page, click the settings gear and then click Edit Object.

  4. In the Object Manager page, select Triggers in the left sidebar and click New.

  5. In the Apex Trigger Edit panel, enter your trigger code, including the ID of the template you created.

    // Generate a document whenever an Opportunity Stage Name is set to "Proposal/Price Quote"
    trigger OpportunityTrigger on Opportunity (after update) {
        // Create a list of documents to generate
        List<Inkit.GenerateDocumentPB.RenderInput> inputs = new List<Inkit.GenerateDocumentPB.RenderInput>();
        // Iterate through all changed Opportunities
        for (Opportunity opp: Trigger.new) {
            // Check if the Stage Name property has changed to "Proposal/Price Quote"
            if (Trigger.oldMap.get(opp.Id).StageName != 'Proposal/Price Quote' && opp.StageName == 'Proposal/Price Quote') {
                // Create a new document object
                Inkit.GenerateDocumentPB.RenderInput input = new Inkit.GenerateDocumentPB.RenderInput();
                // Set the Opportunity ID
                input.recordId = opp.id;
                // Input the ID of Inkit template you want to use
                input.templateId = 'ENTER YOUR TEMPLATE ID';
                // Input the name of the generated document.
                input.docName = 'Apex Proposal';
                // Set the destination type. Input 'link' to send a link to Salesforce. Input 'file' to send a file.
                input.salesforceDeliveryType = 'link';
                // Add the document object to the list
                inputs.add(input);
            }
        }
        // Check if there's anything to generate
        if (!inputs.isEmpty()) {
            // Generate documents
            Inkit.GenerateDocumentPB.createRenders(inputs);
        }
    }
    

    Then click Quick Save.

    Note: We recommend that you process all your Salesforce triggers in a single location.

Initiating an Apex Trigger

Once you've created an Apex trigger for the proposal, you can generate a document from it by setting the Stage of a Sales Opportunity to Proposal/Price Quote. To do this:

  1. Click the App Launcher.

    In the Search apps and items... field, enter sales and click the Sales app.

  2. In the Sales page, click the Opportunities tab and click on an Opportunity Name.

  3. In the Opportunity page, select the Details tab and change Stage to Proposal/Price Quote, and then click Save.

Putting it all together

The following is a simple DOCX template proposal with Salesforce fields, which you can download and modify:

After you have created a template from this and copied its ID, created an Apex trigger with the ID and initiated the trigger, Inkit will generate a document similar to the following:

📘

Note: You can also automatically generate documents from Salesforce Flows.