You need to sign in to do that
Don't have an account?
jayanth reddy
Automatic Email send on click of button in lightning by picking email template
I am having a button in classic version which is a javascript button
On click of that button it is picking the email template and send sending emails to that candidate automatically
I want to achieve this in lightning
What is the procedure?
On click of that button it is picking the email template and send sending emails to that candidate automatically
I want to achieve this in lightning
What is the procedure?
Please find the code below :
Markup :
<aura:component implements = "force:hasRecordId" controller = "SendEmailTemp">
<div>
Do you want to send details?
<lightning:button onclick = "{!c.sendDetails}">
Yes
</lightning:button>
<lightning:button onclick = "{!c.closeQuickaction}">
No
</lightning:button>
</div>
</aura:component>
Controller :
sendDetails: function(component,event,helper) {
helper.sendDetailsToApex(component);
},
closeQuickaction: function(component,event,helper) {
$A.get("e.force:closeQuickAction").fire();
}
Helper :
sendDetailsToApex: function(component,event,helper) {
var recordId = component.get("v.recordId);
var sendCandidate = component.get("c.sendCandidate");
sendCandidate .setParams({
"cId" : recordId ,
"obj" : 'Template-01'
});
sendCandidate.setCallback(this, function(response){
var state = response.getState();
if(state == 'SUCCESS'){
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({ "title": "Success!", "message": response.getReturnValue() });
toastEvent.fire();
$A.get("e.force:closeQuickAction").fire();
}
});
$A.enqueueAction(sendCandidate);
}
Note : Add @AuraEnabled to your method : sendCandidate.
Cheers!!!!
All Answers
Can you share the JS written over the button.
The basic procedure is to convert the button to quick action and then bind a lightning component.
On load of the component the Email sending code needs to be written.
Cheers!!!!!
This the code in apex class
global class SendEmailTemp
{
public SendEmailTemp()
{
}
webservice static String sendCandidate(String cId,String obj)
{
EmailTemplate emiailTemp = new EmailTemplate();
List<Object__c> cpps = new List<Object__c>();
List<Contact> contact = new List<Contact>();
List<String> sendTo = new List<String>();
cpps = [SELECT Id,CandidateID__c,Object__r.Name,Object__r.Email,Candidate_Name__c
FROM Object__c where Id=: cId order by CreatedDate desc LIMIT 1];
if(cpps != null && cpps.size() > 0)
{
contact = [SELECT Id, Email FROM Contact WHERE Id =: cpps[0].CandidateID__c LIMIT 1];
emiailTemp = [SELECT Id, Name, DeveloperName FROM EmailTemplate where DeveloperName = 'tempNameName'];
sendTo.add(contact[0].Email);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId(emiailTemp.Id);
mail.setWhatId(cpps[0].Id);
mail.setTargetObjectId(cpps[0].Candidate_Name__c);
mail.setToAddresses(sendTo);
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setReplyTo('abcd@hotmail.com');
mail.setSenderDisplayName('testmail');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
return 'Successfully Sent an Email';
}
return 'Sending mail failed';
}
}
}
JS Code
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
//alert('test');
try
{
var r = confirm("Do you want to send details?");
if(r == true)
{
var cId = '{!object__C.Id}';
//var obj = 'cpf';
result = sforce.apex.execute("SendEmailTemp","sendCandidate",{cId:cId, templateName:'Template-01'});
alert(result);
}
}
catch(ex) {
alert("Error in sending mail, Please contact administrator.");
//alert(ex);
}
Please find the code below :
Markup :
<aura:component implements = "force:hasRecordId" controller = "SendEmailTemp">
<div>
Do you want to send details?
<lightning:button onclick = "{!c.sendDetails}">
Yes
</lightning:button>
<lightning:button onclick = "{!c.closeQuickaction}">
No
</lightning:button>
</div>
</aura:component>
Controller :
sendDetails: function(component,event,helper) {
helper.sendDetailsToApex(component);
},
closeQuickaction: function(component,event,helper) {
$A.get("e.force:closeQuickAction").fire();
}
Helper :
sendDetailsToApex: function(component,event,helper) {
var recordId = component.get("v.recordId);
var sendCandidate = component.get("c.sendCandidate");
sendCandidate .setParams({
"cId" : recordId ,
"obj" : 'Template-01'
});
sendCandidate.setCallback(this, function(response){
var state = response.getState();
if(state == 'SUCCESS'){
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({ "title": "Success!", "message": response.getReturnValue() });
toastEvent.fire();
$A.get("e.force:closeQuickAction").fire();
}
});
$A.enqueueAction(sendCandidate);
}
Note : Add @AuraEnabled to your method : sendCandidate.
Cheers!!!!