Getting Started with Salesforce Quick Action APIs – Part 3 (Email Action with file attachment)

This is in continuation of previous posts on Salesforce Action APIs. For previous posts refer to this link Getting Started with Quick Action APIs

This Quick Action APIs – Part 3 where we will focus on how to set email action fields value especially Attachments/Content documents.

For this, we are building a scenario where using the visualforce page a PDF will be generated and the same PDF needs to be sent out to the customer using "Send Email" Quick Action.

Steps will be the following:
  • Create a simple visualforce page using the standard controller of "Quote" object named "QuotePDF"
  • Embed the "QuotePDF" in lightning Component using an iframe for preview.
  • Upon iframe, Load saves the PDF under the current Quote record as Content Document and return content document id.
  • Now pass this Content Document ID array under the "ContentDocumentIds" attribute of setActionFieldValues funtion of Quick Action APIs.
Note: For Quick Action APIs to work the requested component should be present on the record current screen from where this action will invoke else system will through error ☹️
<aura:component controller="QuickActionAPIScreenController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<!-- Attribute defination -->
<lightning:quickActionAPI aura:id="quickActionAPI" />
<aura:attribute name="emailTemplateRecordId" type="String" />
<aura:attribute name="documentIds" type="List" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<!-- Screen defination -->
<div>
<iframe src="{!'/apex/QuotePDF?Id='+ v.recordId }" style="height: 390px; width: 100%; overflow: hidden; margin: 0px;"/>
</div>
<br/>
<div>
<lightning:button label="Submit Quote" onclick="{!c.callQuoteSendEmailAction}"/>
</div>
</aura:component>
({
doInit: function(component) {
/* Action for Email Template Id */
var action = component.get("c.getQuoteEmailTemplate");
// Here you can define your Email Template Name
action.setParams({
"templateName": "Quote Dealer Email Template"
});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
component.set("v.emailTemplateRecordId", response.getReturnValue());
}
else {
console.log("Failed with state: " + state);
}
});
/* Action for Email Template Id */
var action2 = component.get("c.getQuotePDFAsFile");
action2.setParams({
"quoteId": component.get("v.recordId")
});
action2.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
component.set("v.documentIds", response.getReturnValue());
}
else {
console.log("Failed with state: " + state);
}
});
$A.enqueueAction(action);
$A.enqueueAction(action2);
},
callQuoteSendEmailAction : function( component, event, helper ) {
var actionAPI = component.find("quickActionAPI");
var documentIdsLst = component.get("v.documentIds");;
//documentIdsLst.push("0690K00000RuSEL");
//documentIdsLst.push("0690K00000RuSEQ");
var emailTempleteId = component.get("v.emailTemplateRecordId");
var fields = {
ContentDocumentIds : {value : documentIdsLst},
EmailTemplate : { value : emailTempleteId },
};
var args = { actionName : "SendEmail",
entityName : "Quote",
targetFields : fields };
actionAPI.setActionFieldValues(args).then(function() {
//actionAPI.invokeAction(args);
}).catch(function(e) {
console.log('Error upon calling Email Action: ', JSON.stringify(e));
});
},
})
public class QuickActionAPIScreenController {
@AuraEnabled
public static String getQuoteEmailTemplate(String templateName){
// templateName = 'Quote Dealer Email Template'
EmailTemplate eTemplate;
List <EmailTemplate> templateLst = [SELECT Id, Name,Subject, HtmlValue FROM EmailTemplate where Name =:templateName];
eTemplate = ( templateLst.size() > 0 ? templateLst.get(0) : new EmailTemplate(Subject = '',HtmlValue= 'No Template Found...') );
return eTemplate.Id;
}
@AuraEnabled
public static String[] getQuotePDFAsFile(String quoteId){
PageReference PDf = Page.QuotePDF;
PDf.getParameters().put('Id',quoteId);
PDf.setRedirect(true);
//ContentVersion
ContentVersion contentVersion_1 = new ContentVersion(
Title='Quote001',
PathOnClient ='/Quote001.PDF',
VersionData = PDf.getContentAsPdf(),
origin = 'H'
);
insert contentVersion_1;
Id cdocumentid = [SELECT Id, Title, ContentDocumentId FROM ContentVersion
WHERE Id = :contentVersion_1.Id LIMIT 1].ContentDocumentId;
ContentDocumentLink contentlink = new ContentDocumentLink();
contentlink.LinkedEntityId = quoteId;
contentlink.contentdocumentid = cdocumentid;
contentlink.ShareType = 'V';
insert contentlink;
return new List<String>{cdocumentid};
}
}

The key points to consider:

  • ContentDocumentIds: This accepts an array of Content Document Ids so in case multiple files need to attach under email than it will be possible.

Comments

Popular posts from this blog

Getting Started with Salesforce Quick Action APIs – Part 1 (Invoke using custom button)

Getting Started with Salesforce Quick Action APIs – Part 4 (Email Action with email template)