· tutorials · 3 min read

Streamlining Document Creation with Conga Composer in Salesforce

Learn how to automate document merging with Conga Composer calling Conga with Apex.

As a Salesforce professional, you understand the importance of having accurate and up-to-date information readily available in order to make informed decisions. With Conga Composer, you can easily merge data from Salesforce to create complex, branded documents in a variety of formats, including Excel, Word, and PDFs.

However, one challenge with using Conga is that batching records and automating business processes can be difficult with this tool. But don’t worry, there is a solution! By using Apex, you can merge and create documents without ever touching the record in the UI. This opens up a world of possibilities for automating document creation, such as sending receipts to customers or automatically sending contracts based on opportunity stage.

Today, we’ll explore how to set up this automation using a template. We’ll walk you through a simple example of creating a custom email that is sent when an opportunity reaches the “Closed Won” stage. The email includes a PDF document that is generated from a merge of the opportunity and contact data.

Prerequisites

  • Conga Composer 8
  • Salesforce Admin Permissions
  • (Optional) Org-Wide Email Address

Remote Site Setup

You will need to create a remote site for the composer api. You can do this using the following setup:

name: Composer_API url: https://composer.congamerge.com

Picture here

Conga Composer Remote Site Details

Custom Metadata

We’ll be using a custom metadata object to hold information about the email. This allows end users to update data without having to make changes to the code in the sandbox. The custom metadata object includes fields for the body of the email, the file name, the subject, and the template ID.

To create the custom metadata:

Label: Composer Automation Plural Label: Composer Automations Object Name: Composer_Automation

Composer Automation Metadata

And the necessary cutom fields:

  • Body - Long Text Area(32768)
  • File Name - Text(255)
  • Include Date - Checkbox
  • Org Wide Email - Email
  • Subject - Text(255)
  • Template Id - Text(255)

Composer Automation Metadata

We’ll use the custom meatadata to drive the settings for the email and content functionality. This way code updates are not needed if a simple title update is needs.

Apex Code

The sample code supplied is an example of how to merge documents using apex. You can find the full source code in the github repository

private Id mergeDocument(Id templateId) {
    Http h = new Http();
    HttpRequest req = new HttpRequest();
    HttpResponse res = new HttpResponse();
    String fileName = EncodingUtil.urlEncode(conga_automation_settings.file_name__c, 'UTF-8');

    if(conga_automation_settings.include_date__c){
      fileName += String.valueOf(date.today());
    }

    String baseEndpoint = 'https://composer.congamerge.com/composer8/index.html';

    String endpoint = baseEndpoint + '?sessionId=' + this.sessionId +
    '&serverUrl=' + System.Url.getSalesforceBaseUrl().toExternalForm() + '/services/Soap/u/51.0/' + UserInfo.getOrganizationId() +
    '&id=' + this.opportunityId +
    '&templateid=' + templateId +
    '&DefaultPDF=1' +
    '&OFN='+ fileName +
    '&APIMode=1';

    req.setEndpoint( endpoint );
    req.setMethod('GET');

    // Large Timeout because files are large and take time to send via https
    req.setTimeout(60000);
    System.debug(req.getEndpoint());
    res = h.send(req);
    System.debug(res.getStatusCode());
    System.debug(res.getBody());

    try{
      Id bool = (Id) res.getBody();
    }catch(Exception e){
      System.debug(e);
      return null;
    }

    return (Id) res.getBody();
}

Conclusion

In conclusion, Conga Composer is a powerful tool for creating complex documents in Salesforce. With the ability to automate document creation using Apex, you can streamline your processes and improve the accuracy of your data. If you want to learn more about using Apex to automate document creation in Salesforce, check out the links below.

Need Our Help To Get Your Data Into Salesforce?

Join dozens of other companies by learning how you can get all your company's data in one place.

Back to Blog