Skip to content

Instantly share code, notes, and snippets.

@SalesforceBobLightning
Created July 26, 2018 22:48
Show Gist options
  • Select an option

  • Save SalesforceBobLightning/faebaaddf5832e1f65dbc2630154cf50 to your computer and use it in GitHub Desktop.

Select an option

Save SalesforceBobLightning/faebaaddf5832e1f65dbc2630154cf50 to your computer and use it in GitHub Desktop.
Generate PDF from a Docomotion Form Name via Salesforce Process Builder. Rather than using Docomotion Form ID
/**
* @description: Using Docomotion: create PDF via Process Builder using Form Name. Rather than using Form IDs which are different in each Org
* @documentation: https://www.docomotion.com/support/generating-documents/silent-mode-generating-output-through-an-api/?highlight=API
**/
public class GeneratePDFAction {
@InvocableMethod(
label='Custom Generate PDF'
description = 'Generate PDF using a Form Name and What Id'
)
public static void generatePDFs(List<Request> requests) {
for(Request request : requests){
generatePDF(request.formName, request.whatId, 'Chatter', 'PDF', request.whoId, request.filename, request.isActivatedVersion);
}
}
@future(callout=true)
public static void generatePDF(string formName, Id whatId, string mode, string format, Id whoId, string filename, boolean isActivatedVersion) {
try {
Integer formId = getFormId(formName);
if (formId == null){
throw new NoFormFoundException(formName);
}
doco.RenderAPI.renderCreateFile(formId, whatId, doco.RenderAPI.Mode.Chatter, doco.RenderAPI.Format.PDF, whoId, filename, isActivatedVersion);
} catch (Exception e) {
System.debug(e.getMessage());
}
}
private static Integer getFormId(String formName) {
List<doco__Form__c> formList = [SELECT Id, doco__Form_Id__c
FROM doco__Form__c
WHERE doco__Name__c = :formName];
if (formList.size() > 0){
return Integer.valueOf(formList[0].doco__Form_Id__c);
}
return null;
}
private static doco.RenderAPI.Mode getMode(string mode){
switch on mode {
when 'Attachment' {
return doco.RenderAPI.Mode.Attachment;
}
when 'Chatter' {
return doco.RenderAPI.Mode.Chatter;
}
when 'Document' {
return doco.RenderAPI.Mode.Document;
}
when else {
return doco.RenderAPI.Mode.Chatter;
}
}
}
private static doco.RenderAPI.Format getFormat(string format){
switch on format {
when 'PDF' {
return doco.RenderAPI.Format.PDF;
}
when 'Docx' {
return doco.RenderAPI.Format.Docx;
}
when 'Html' {
return doco.RenderAPI.Format.Html4s;
}
when 'Html4s'{
return doco.RenderAPI.Format.Html4s;
}
when else {
return doco.RenderAPI.Format.PDF;
}
}
}
public class Request {
@InvocableVariable(
label = 'Form Name'
description = 'Name of Doco Form to use'
required=true
)
public String formName;
@InvocableVariable(
label = 'Filename'
description = 'Filename of the file to be created'
required=true
)
public String filename;
@InvocableVariable(
label = 'Use Active Version'
required=true
)
public Boolean isActivatedVersion;
@InvocableVariable(
label = 'Who ID'
required=true
)
public Id whoId;
@InvocableVariable(
label = 'What ID'
required=true
)
public Id whatId;
}
private class NoFormFoundException extends Exception {
public override String getMessage() {
List<String> args = new String[]{super.getMessage()};
return String.format('No Doco Form ID found with Form Name: {0}', args);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment