Skip to content

Instantly share code, notes, and snippets.

@RatanPaul
Last active November 28, 2025 08:14
Show Gist options
  • Select an option

  • Save RatanPaul/1d4707ee12056b3fd5239e60e7dc93b1 to your computer and use it in GitHub Desktop.

Select an option

Save RatanPaul/1d4707ee12056b3fd5239e60e7dc93b1 to your computer and use it in GitHub Desktop.
Conga composer pdf generation, attaching to record and email send functionality
// Provide the Proforma_Invoice__c Id
Id singleId = '';
// Build a query for only that record
String singleRecordQuery = 'SELECT Id, Account_Name__c, Renewal_Quote__c, Renewal_Quote__r.Contact__c, ' +
'Renewal_Quote__r.SBQQ__Account__c, Renewal_Quote__r.SBQQ__Account__r.Id, ' +
'Renewal_Quote__r.SBQQ__Account__r.Name ' +
'FROM Proforma_Invoice__c ' +
'WHERE Id = \'' + singleId + '\'';
Invoice__c renInvoice = Database.query(singleRecordQuery);
String sessionId = UserInfo.getSessionId();
String serverUrl = URL.getOrgDomainUrl().toExternalForm() + '/services/Soap/u/59.0/' + UserInfo.getOrganizationId();
String fileName = 'Test';
// EncodingUtil.urlEncode uses '+' for spaces; Conga examples often show %20, so normalize.
String encodedFileName = EncodingUtil.urlEncode(fileName, 'UTF-8').replace('+', '%20');
// Base Conga params (template & queries)
String baseParams =
'&queryid=' +
'[QuoteLineRenewal]0Q_026MAQ975123' +
',[RenewalLetter]0Q_030MAQ331123' +
',[StaticRenewal]0Q_174MAQ153123' +
',[SelectRenewal]0Q_175MAQ769123' +
'&defaultPDF=1' +
'&templateid=0T_071MAQ089123' +
'&OFN=' + encodedFileName +
'&EmailToId=' + renInvoice.Renewal_Quote__r.Contact__c) +
'&CongaEmailTemplateId=0E_003EAU88123' +
// IMPORTANT: Use ONLY APIMode OR DS7, not both.
// APIMode=12 → run in background and send email.
'&APIMode=12' +
// Save a copy as Salesforce File (this still works without DS7)
'&SC1=SalesforceFile&SC0=1';
String congaUrl = Label.Conga_Composer_URL +
'sessionId=' + sessionId +
'&serverUrl=' + serverUrl +
'&id=' + renInvoice.Id +
baseParams;
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(congaUrl);
req.setMethod('GET');
req.setTimeout(120000);
req.setHeader('Authorization', 'Bearer ' + sessionId);
HttpResponse res = http.send(req);
System.debug('Conga response status: ' + res.getStatusCode() + ' - ' + res.getBody());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment