Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save SalesforceBobLightning/579a4a2e04bffab79806a9e5ba6ae82d to your computer and use it in GitHub Desktop.
Generate PDF using Email Template Developer Name and Docomotion Form Name with and send as email attachment via Salesforce Process Builder. Rather than using Email Template ID and Docomotion Form ID which change per Org
/**
* @description: Using Docomotion: create PDF and send via Email from Process Builder using Email Template Developer Name and Form Name
* @documentation: https://www.docomotion.com/support/generating-documents/silent-mode-generating-output-through-an-api/?highlight=API
**/
public class SendEmailAndPDFAction {
@InvocableMethod(
label='Send Email with PDF'
description = 'Send email with PDF attachment, which takes an Form Name, Email Template Developer Name, who Id and What Id'
)
public static void sendEmailsWithPDFs(List<Request> requests) {
for(Request request : requests){
sendEmailWithPDF(request.formName, request.whatId, request.targetObjectId, request.filename, request.isActivatedVersion, request.emailTemplateDeveloperName);
}
}
@future(callout=true)
public static void sendEmailWithPDF(string formName, Id whatId, Id targetObjectId, string filename, boolean isActivatedVersion, string emailTemplateDeveloperName) {
try {
Id templateId = getEmailTemplateId(emailTemplateDeveloperName);
if (templateId == null){
throw new NoEmailTemplateFoundException(templateId);
}
Integer formId = getFormId(formName);
if (formId == null){
throw new NoFormFoundException(formId);
}
doco.RenderAPI.renderCreateFile(formId, whatId, doco.RenderAPI.Mode.Attachment, doco.RenderAPI.Format.PDF, targetObjectId, filename, isActivatedVersion, templateId);
} catch (Exception e) {
System.debug(e.getMessage());
}
}
private static Id getEmailTemplateId(String emailTemplateDeveloperName) {
List<EmailTemplate> emailTemplates = [SELECT Id
FROM EmailTemplate
WHERE DeveloperName = :emailTemplateDeveloperName];
if (emailTemplates.size() > 0){
return emailTemplates[0].Id;
}
return null;
}
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;
}
public class Request {
@InvocableVariable(
label = 'Form Name'
description = 'Name of Doco Form to use'
required=true
)
public String formName;
@InvocableVariable(
label = 'Email Template Developer Name'
description = 'Developer Name of the email template to use'
required=true
)
public String emailTemplateDeveloperName;
@InvocableVariable(
label = 'Filename'
description = 'Filename of the file to be created'
required=true
)
public String filename;
@InvocableVariable(
label = 'Use Active Version'
description = 'Use Active Version'
required=true
)
public Boolean isActivatedVersion;
@InvocableVariable(
label = 'Who ID'
description = 'Email recipient and who the email Activity will be related to (User, Contact, or Lead)'
required=true
)
public Id targetObjectId;
@InvocableVariable(
label = 'What ID'
description = 'What the email Activity will be related to (Account, Case, Opportunity, etc.). Cannot be set if Who Id is a User record.'
required=true
)
public Id whatId;
}
private class NoEmailTemplateFoundException extends Exception {
public override String getMessage() {
List<String> args = new String[]{super.getMessage()};
return String.format('No Email Template ID found with Developer Name: {0}', args);
}
}
private class NoFormFoundException extends Exception {
private Integer formId;
public NoFormFoundException(Integer formId){
this.formId = formId;
}
public override String getMessage() {
List<String> args = new String[]{string.valueOf(formId)};
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