Skip to content

Instantly share code, notes, and snippets.

@SalesforceBobLightning
Last active July 10, 2018 09:30
Show Gist options
  • Select an option

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

Select an option

Save SalesforceBobLightning/bfe11269477c92eb3bd5e1eb22bc78a6 to your computer and use it in GitHub Desktop.
Email Template Lookup Component for Salesforce Lightning

Salesforce Lightning Email Template Lookup Component

This Salesforce Lightning Component allows the user to select from system Email Templates.

Email Template Lookup

Usage

<aura:component implements="force:hasRecordId">

    <c:EmailTemplateLookup 
        label='Welcome Email Template' 
        objectName='Custom_Object__c' 
        fieldName='Custom_Field__c' 
        recordId='{! v.recordId }' 
    /> 

</aura:component>

Custom_Object__c is the Object you are storing the Email Template ID to.

Custom_Field__c is the field on the Object where the Email Template ID is stored

How it works

It automatically loads and it automatically saves onchange

Dependancies

It is dependant on the Lightning Universal Lookup Component by Synebo.io

<aura:component controller="EmailTemplateLookupController">
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:handler name="change" value="{!v.selectedRecordId}" action="{!c.onChange}"/>
<aura:attribute name="label" type="String" />
<aura:attribute name="objectName" type="String" />
<aura:attribute name="fieldName" type="String" />
<aura:attribute name="recordId" type="String" />
<aura:attribute name="selectedRecordId" type="String" />
<aura:attribute name="setTo" type="String" />
<l_lookup:Lookup aura:id="EmailTemplateLookup" label="{! v.Label }" objectType="EmailTemplate" showFiveRecent="false" setTo="{!v.setTo}" selectedRecordId="{!v.selectedRecordId}" />
</aura:component>
public class EmailTemplateLookupController {
private static String getQueryString(String objectName, String fieldName, String recordId) {
List<String> args = new String[]{fieldName, objectName, recordId};
return String.format('SELECT Id, {0} FROM {1} WHERE Id = \'\'{2}\'\'', args);
}
@AuraEnabled
public static String getEmailTemplateId(String objectName, String fieldName, String recordId){
String queryString = getQueryString(objectName, fieldName, recordId);
List<sObject> results = Database.query(queryString);
if (results.size() > 0) {
return (String) results[0].get(fieldName);
}
return null;
}
@AuraEnabled
public static void updateEmailTemplate(String objectName, String fieldName, String fieldValue, String recordId){
String queryString = getQueryString(objectName, fieldName, recordId);
List<sObject> results = Database.query(queryString);
if (results.size() > 0) {
SObject record = results[0];
record.put(fieldName, fieldValue);
update record;
}
}
}
({
doInit : function(cmp, event, helper) {
let objectName = cmp.get("v.objectName");
let fieldName = cmp.get("v.fieldName");
let recordId = cmp.get("v.recordId");
helper.callAction(cmp, "c.getEmailTemplateId", {'objectName' : objectName, 'fieldName' : fieldName, 'recordId' : recordId}, function(result){
console.log(result);
if (result){
cmp.set("v.setTo", result);
cmp.find("EmailTemplateLookup").fireChanging();
}
});
},
onChange : function(cmp, event, helper) {
let objectName = cmp.get("v.objectName");
let fieldName = cmp.get("v.fieldName");
let fieldValue = event.getParam("value");
let recordId = cmp.get("v.recordId");
helper.callAction(cmp, "c.updateEmailTemplate", {'objectName' : objectName, 'fieldName' : fieldName, 'fieldValue' : fieldValue, 'recordId' : recordId}, null);
}
})
@isTest
public class EmailTemplateLookupControllerTests {
final static String expected = 'ABCDEFGHIJK';
final static String objectName = 'Account';
final static String fieldName = 'Name';
final static String fieldValue = 'lmnopqrstuvwxyz';
@testSetup
public static void testSetup(){
Account account = new Account();
account.Name = expected;
insert account;
}
@isTest
public static void getEmailTemplateId_ReturnId(){
// arrange
Account account = [SELECT Id FROM Account WHERE Name = :expected LIMIT 1];
Id recordId = account.Id;
// act
String actual = EmailTemplateLookupController.getEmailTemplateId(objectName, fieldName, recordId);
// assert
System.assertEquals(expected, actual);
}
@isTest
public static void updateEmailTemplate_Valid(){
// arrange
Account account = [SELECT Id FROM Account WHERE Name = :expected LIMIT 1];
Id recordId = account.Id;
// act
EmailTemplateLookupController.updateEmailTemplate(objectName, fieldName, fieldValue, recordId);
Account updatedAccount = [SELECT Id, Name FROM Account WHERE Id = :recordId];
// assert
System.assertEquals(fieldValue, updatedAccount.Name);
}
}
({
callAction : function(cmp, methodName, params, callback){
var action = cmp.get(methodName);
action.setParams(params);
action.setCallback(this, function(response) {
var state = response.getState();
if(cmp.isValid() && state === "SUCCESS"){
var result = response.getReturnValue();
if (callback) callback(result);
} else if (state === "ERROR"){
var errors = response.getError();
if (errors) {
if (errors[0] && errors[0].message) {
console.log(errors[0].message);
}
}
}
});
$A.getCallback(function() {
$A.enqueueAction(action);
})();
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment