4

does anyone know if it is possible possible to trigger Conga action(generate document/send email with attachment/create attachment) from apex code - best would be trigger or batch. Example will be appreciated.

7
  • 1
    Have you looked at Conga Conductor? I've not used it but came across it recently: congamerge.com/products/composer/add-ons
    – Girbot
    Mar 10, 2016 at 19:43
  • No, I am looking for free solution. As far as I know it was possible to use callout to generate document in conga 7 - but I can't achievw this in conga 8.... Mar 10, 2016 at 19:48
  • 5
    (1) did you see success.salesforce.com/… ?; (2) Conga has superb support, have you asked them?
    – cropredy
    Mar 10, 2016 at 20:08
  • 4
    Artur -- I 'love' "bothering" Conga Support because a) they are super friendly (and it is not bothersome to them) , b) they know tricks that aren't always documented and c) I learn something each time. Unlike support at say, Big Company X, where you might rather put pins through your eyes, Conga support is terrific - and if you are a licensed user, free.
    – cropredy
    Mar 13, 2016 at 19:00
  • 1
    I totally would bother them , some of the folks there need to get on salesforce and start supporting salesforce cases right away. They are super technical and courteous. Do not waste your time waiting, contact them right away
    – Rao
    Mar 16, 2016 at 18:55

4 Answers 4

2

According to @CongaPete in this thread: Run Conga Composer (release 8) in background mode via HTTP Callout, you cannot do this in Conga Composer 8. Instead, you can still use the Conga 7 endpoint https://www.appextremes.com/apps/conga/pm.aspx and the same querystring parameters.

One note, you will also need to use the old Conga 7 style merge syntax, not the new Text-Based Merge Fields.

I recommend the DS7=5 parameter as well.

1
  • Is it possible to this via trigger? I assume you will have to use a future method. But that is a asynchronous. And hence would not have the sessionID that conga needs.
    – RajeshShah
    Jul 3, 2019 at 15:06
1

Some working code to invoke through apex with conga 8 since nobody else seemed to share here yet :) I almost believed it wasn't possible after seeing responses above but I was able to make it work!

private static final String CONGA_ENDPOINT = 'https://composer.congamerge.com/composer8/index.html?';
String templateId = [SELECT APXTConga4__Key__c FROM APXTConga4__Conga_Template__c WHERE APXTConga4__Name__c = 'ISR Report' LIMIT 1].APXTConga4__Key__c;
String accountId = 'ExAmPlEaCcOuNtId';    // query the master record ID you need here, just keeping it simple for example
String sessionId = UserInfo.getSessionId();
String servUrl = System.Url.getSalesforceBaseUrl().toExternalForm() + '/services/Soap/u/14.0/' + UserInfo.getOrganizationId();
String endpoint = CONGA_ENDPOINT + 'sessionId=' + sessionId + '&serverUrl=' + EncodingUtil.urlEncode(servUrl, 'UTF-8');
//endpoint += '&id=' + accountId + '&TemplateId=' + templateId + '&defaultPDF=1' + '&APIMODE=1';    // if there are issues, Conga recommends trying to remove the DS7 (background mode) param
endpoint += '&id=' + accountId + '&TemplateId=' + templateId + '&defaultPDF=1' + '&DS7=12' + '&APIMODE=1';
System.debug('endpoint: ' + endpoint);
        
Http http = new Http();
HttpRequest request = new HttpRequest();       
request.setMethod('GET');
request.setTimeout(120000);    // conga recommends setting a long timeout for generation
request.setEndpoint(endpoint);
request.setHeader('Authorization',  'Bearer ' + sessionId);
HttpResponse response = http.send(request);
System.debug(response + '     ' + response.getBody());

Using APIMode=1 the document is attached to the record, the response body should look as follows with a debug: System.HttpResponse[Status=OK, StatusCode=200] <123exampleSFid> you can then use the given ID to find the file in your org!

You can reference the conga documentation for different API modes available, we ended up going with the email as well (mode 12): https://documentation.conga.com/composer/latest/how-to-call-conga-composer-from-apex-143199130.html

2
  • This is a better answer. Conga 8 does support APIMODE now. I had quite a lot of trouble getting it going, but stick with it and you'll eventually be able to. It may not be relevant, but I ended up dropping FlattenPDFForms and FP0 params before I was able to get it working. I also added SC0=1, SC1=Attachments, and the AttachmentParentID params.
    – Philip F
    Feb 22, 2023 at 7:34
  • thanks for the feedback @PhilipF! was definitely a learning experience for me too. We ended up doing something a bit diff too longer term. I'll post it in another comment :) Sep 8, 2023 at 17:06
0

It is possible. You need to purchase the Conga Workflow package.

See info here: https://support.getconga.com/Conga_Composer/Conga_Workflow

1
0

Adding another comment here! For our purposes we ended up sending the Conga generated PDF in the response with some slight changes. See below:

private static final String CONGA_ENDPOINT = 'https://composer.congamerge.com/composer8/index.html?';
// make a callout to Conga to generate document  
public static Blob getDocument(String institutionId) {  
    //String templateId= '';
    String solutionParams = '';
    String solutionName = 'yourCongaDocNameHere'; // replace with the Name of your Conga Solution
    if(!Test.isRunningTest()){
        solutionParams = [SELECT id, APXTConga4__Composer_Parameters__c FROM APXTConga4__Conga_Solution__c WHERE Name =: solutionName ORDER BY LastModifiedDate DESC LIMIT 1].APXTConga4__Composer_Parameters__c;
        //templateId = [SELECT APXTConga4__Key__c FROM APXTConga4__Conga_Template__c WHERE APXTConga4__Name__c = 'ISR Report' LIMIT 1].APXTConga4__Key__c;
        System.debug('solutionParams: ' + solutionParams);
    }
    String sessionId = UserInfo.getSessionId();
    String servUrl = System.Url.getSalesforceBaseUrl().toExternalForm() + '/services/Soap/u/14.0/' + UserInfo.getOrganizationId();
    String endpoint = CONGA_ENDPOINT + 'sessionId=' + sessionId + '&serverUrl=' + servUrl;
    //endpoint += '&id=' + institutionId + '&TemplateId=' + templateId + '&defaultPDF=1' + '&APIMODE=1';    // if there are issues, Conga recommends trying to remove the DS7 (background mode) param
    endpoint += '&id=' + institutionId + solutionParams + '&defaultPDF=1' + '&DS7=12' + '&APIMODE=1';
    //'&TemplateId=' + templateId + '&defaultPDF=1' + '&DS7=12' + '&APIMODE=1';
    System.debug('endpoint: ' + endpoint);
    endpoint = endpoint.replace('{!Account.Id}', institutionId);
    System.debug('endpoint after injecting account ID: ' + endpoint);
    endpoint = endpoint.replace('[', '%5B');
    System.debug('endpoint after encoding bracket [: ' + endpoint);
    endpoint = endpoint.replace(']', '%5D');
    System.debug('endpoint after encoding bracket ]: ' + endpoint);
    endpoint = endpoint.replace(',', '%2C');
    endpoint = endpoint.replace('\n', '');
    //System.debug('endpoint after encoding: ' + endpoint);
    Http http = new Http();
    HttpRequest request = new HttpRequest();       
    request.setMethod('GET');
    request.setTimeout(120000);
    request.setEndpoint(endpoint);
    request.setHeader('Authorization',  'Bearer ' + sessionId);
    //Cant send request in test method
    if(!Test.isRunningTest()){
        HttpResponse response = http.send(request);
        // with ApiMode=1 Conga will attach the file to the institution record and return the attachment ID in the response
        System.debug(response + '     ' + response.getBody());
        String attachmentId = response.getBody();
        Attachment att = [SELECT Body FROM Attachment WHERE Id = :attachmentId LIMIT 1];
        return att.Body;
    }
    else{
        return Blob.valueOf('Test Blob');
    }
}

We call this method to set the response body and add another line to set header for Content-Type = 'application/pdf'.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .