0

I am trying to build "Send Email component" and showing the list of templates for the user to select. Upon select of the template I am populating body and subject of the email content in the component. I am facing a challenge wherein "onchange" event is not getting fired. I am not sure what I am doing wrong here. For testing purposes I am querying only one template in the controller(geteTemplates).

    <aura:component description="SendEmail" implements="force:appHostable,flexipage:availableForRecordHome,forceCommunity:availableForAllPageTypes,force:hasRecordId,force:lightningQuickAction,force:hasSObjectName"
                controller="SendEmailController">
    <aura:attribute name="subject" type="String"/>
    <aura:attribute name="body" type="String" />
    <aura:attribute name="toAddr" type="String" default="[email protected]"/>
    <aura:attribute name="ccAddr" type="String"/>
    <aura:attribute name="bccAddr" type="String"/>
    <aura:attribute name="templates" type="List" default=""/>
    <aura:attribute name="emailHeader" type="String" default="Send Email"/>    
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>


    <div class="demo-only demo-only_viewport" style="height: 600px;">
        <section role="dialog" tabindex="-1" class="slds-modal slds-fade-in-open slds-modal_medium" aria-labelledby="modal-heading-01" aria-modal="true" aria-describedby="modal-content-id-1">
            <div class="slds-modal__container">
                <header class="slds-modal__header">
                    <h2 id="modal-heading-01" class="slds-text-heading_medium slds-hyphenate">{!v.emailHeader}</h2>
                </header>
                <div class="slds-modal__content slds-p-around_medium" id="modal-content-id-1">
                    <lightning:combobox name="Email Templates" label="Email Templates" placeholder="Select Email Templates"
                                        value="Claim Email Template" options="{!v.templates}" onchange="{c.handleTemplateClick}"/>                   
                </div>
                <footer class="slds-modal__footer">
                    <button class="slds-button slds-button--brand" onclick="{!c.sendEmailMethod}">Send</button>
                    <button class="slds-button slds-button--brand" onclick="{!c.cancelMethod}">Cancel</button>
                </footer>
            </div>
        </section>
    </div>
</aura:component>

****JS Controller***

    ({
    doInit : function(component, event, helper) {
           console.log('doInit called ');
           var sObjname = component.get("v.sObjectName");
           var uId = $A.get('$SObjectType.CurrentUser.Id');
           component.set("v.userID",uId);
           var recordID     = component.get("v.recordId");
           component.set("v.body",'ref'+':'+sObjname+'-'+recordID);
           var action = component.get("c.getUserEmailToSFAddr");
           action.setParams({ "cUserId" : uId,
                               "sObjName" : sObjname
                            });
           action.setCallback(this, function(response) {
               var res = response.getReturnValue();
               console.log('response object::'+JSON.stringify(response.getReturnValue()));
               var state = response.getState();
               if (state === "SUCCESS") {
                    component.set("v.ccAddr",res[0].LocalPart+'@'+res[0].EmailDomainName);
               }
               else {
                 console.log(state);
               }
             });
             $A.enqueueAction(action);

             var action1 = component.get("c.geteTemplates");
             action1.setCallback(this,function(response){
                 var res1 = response.getReturnValue();
                 console.log('template object::'+JSON.stringify(response.getReturnValue()));
                 var state1 = response.getState();
                 if(state1 == "SUCCESS"){
                     var lables= [];
                     res1.forEach(function(key) {
                         lables.push({"label":key.Name ,"value":key.DeveloperName});
                     });
                     component.set("v.templates", lables);
                 }
             });
             $A.enqueueAction(action1);
    },

    handleTemplateClick:function(component,event,helper){
        var selectedOptionValue = event.getParam("value");
        alert(event.getSource().get("v.value"));
        alert("Option selected with value: '" + selectedOptionValue + "'");
        var action = component.get("c.getTemplateBody");
            action.setParams({tempName:event.getParam("value")});
            action.setCallback(this, function(response) {
                var res = response.getReturnValue();
                var state = response.getState();
                if (state === "SUCCESS") {
                    component.set("v.body" , res[0].Body);
                    component.set("v.subject" , res[0].Subject);
                }
            });
            $A.enqueueAction(action); */
    }
})

****Controller****

    public class SendEmailController
{
    @AuraEnabled
    public static List<EmailTemplate> geteTemplates(){
        return [SELECT Id,Body,Subject,DeveloperName,Name FROM EmailTemplate WHERE DeveloperName='Claim_Email_Template'];

    }

    @AuraEnabled
    public static List<EmailTemplate> getTemplateBody(String tempName){
        return [SELECT Body,Subject FROM EmailTemplate WHERE Name =: tempName];
    }


}

1 Answer 1

2

There is a typo in your onchange event,

<lightning:combobox name="Email Templates" label="Email Templates" placeholder="Select Email Templates"
              value="Claim Email Template" options="{!v.templates}" 
              onchange="{c.handleTemplateClick}"/>                   

It should be

onchange="{!c.handleTemplateClick}"/>

1
  • BUMMER. That works. Thanks for your help!
    – user28452
    Jun 3, 2019 at 11:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .