function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jayanth reddyjayanth 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?
Best Answer chosen by jayanth reddy
Syed Insha Jawaid 2Syed Insha Jawaid 2
HI Jayanth

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

Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Jayanth

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!!!!!
jayanth reddyjayanth reddy
Hi Syed

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); 
}
Syed Insha Jawaid 2Syed Insha Jawaid 2
HI Jayanth

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!!!!
This was selected as the best answer
jayanth reddyjayanth reddy
Thanks Syed...
Mr.HarryMr.Harry
I have same doubt but this is in aura but i'm looking for lightning(lwc)..??