Getting Started with Salesforce Quick Action APIs – Part 4 (Email Action with email template)
This is in continuation of previous posts on Salesforce Action APIs. For previous posts refer to
this link Getting Started with Quick Action APIs
Pre-requisites for this exercise:
- Create an HTML email template (with or without letterhead) named "Quote Dealer Email Template" or any other type based on your choice.
For this, we have to build a scenario where we are using the visualforce
page to generate PDF and the same PDF needs to be sent out to the customer
using a pre-defined email template 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.
- Then query the required email template named "Quote Dealer Email Template" using the apex class method "getQuoteEmailTemplate".
Here is a catch, as per Salesforce Document the field used for setting
an email template is "EmailTenplateId" but if you set the email template
record id directly to this variable of Quick Action API function it will
not work.
So what we need to do:
- Instead of using "EmailTemplateId" as an attribute to set email template Id used "EmailTemplate" attribute and set JSON object with values as template id.
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 ☹️
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
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)); | |
}); | |
}, | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:
- EmailTemplateId: Instead of using "EmailTemplateId" use "EmailTemplate" with a JSON object with value attribute as template record Id.
Comments
Post a Comment