+ Start a Discussion
SFDC@ErrorSFDC@Error 

Getting Error in Lightning component

Hi All,
I have developed a component for mass email sending .but i am getting an error like This page has an error. You might just need to refresh it. Aura.loadComponent(): Failed to initialize the application. An internal server error has occurred Error ID: 1863109829-477750 (412317498).
User-added image
<aura:component controller="ListEmailOppCntrlr" implements="force:hasRecordId,flexipage:availableForAllPageTypes,force:lightningQuickActionWithoutHeader" access="global">
    <aura:handler name="init" value="{!this}" action="{!c.loadComponent}"/> 
    <aura:attribute name="errorMsg" type="String" description=""/>
    <aura:attribute name="shoMsg" type="boolean" default="false" description=""/>
    <aura:attribute name="shwSucesMsg" type="boolean" default="false" description=""/>
    <aura:attribute name="showLoader" type="boolean" default="false" description=""/> 
    <aura:attribute name="Case" type="Case" 
                    default="{ 'sobjectType': 'Case', 'SampleRichText__c':''}"/>
    
    <aura:attribute name="disableTemplate" type="boolean" default="false" description=""/>
    <aura:attribute name="selTempl" type="String" description=""/>
    <aura:attribute name="templates" type="EmailTemplate[]" default="[]"/>
    <aura:attribute name="addnlEmails" type="String" default="" description=""/>
    <aura:attribute name="subjTxt" type="String" default="" description=""/>
    <aura:attribute name="msgReview" type="String" default="" description=""/>
    <aura:attribute name="accIds" type="String" access="GLOBAL" default="" description=""/>
    <aura:attribute name="oppIds" type="String" access="GLOBAL" default="" description=""/>
    <aura:attribute name="accRecords" type="sObject[]" default="[]" />
    <aura:attribute name="templDetail" type="EmailTemplate" default="{}" />
    
    <div class="slds-page-header" role="banner">
        <div class="slds-grid">
            <div class="slds-col slds-has-flexi-truncate">
                <!-- SEARCH AREA -->
                <p class="slds-text-title--caps slds-line-height--reset">
                    
                    <span title="lead Standard Icon">
                        
                    </span>&nbsp;&nbsp;Send Email</p>
                <!-- / SEARCH AREA -->
            </div>
            
        </div>
    </div>
    
    
    
    <br/><br/>
    <!-- Connection 1 Section -->
    <div class="slds-grid slds-grid_vertical">
        <div class="slds-box slds-box--small slds-theme--shade slds-text-align--left">Email Message</div>
        <br/>
        <div class="slds-col">
            
            <div class="slds-form-element slds-size--1-of-1">
                <!-- <label class="slds-form-element__label" for="input-02">Recipients<span color="red"><b>*</b></span></label> -->
                
                <!--  <input type="Text" autocomplete="off" style="border-left:4px solid red;" value=""  class="slds-input" /> -->
                
                                <c:reUsableMultiSelectLookup objectAPIName="account"
                                             IconName="standard:account"
                                             lstSelectedRecords="{!v.accRecords}"
                                             label="Recipients" 
                                             />
                

            </div>
            
        </div> <br/><br/>
        
        <div class="slds-col">
            <lightning:select disabled="{!v.disableTemplate}" onchange="{!c.loadTemplate}"  name="SelectDivision" label="Select a Template:" aura:id="templateId" value="{!v.selTempl}">
                <option text="None" value=""/>
                
                <aura:iteration items="{!v.templates}" var="item">
                    <option text="{!item.Name}" value="{!item.Id}"/>
                </aura:iteration> 
            </lightning:select>
        </div> <br/><br/> 
        
        <div class="slds-col">
            <div class="slds-form-element slds-size--1-of-1">
                <label class="slds-form-element__label" for="input-02">Subject:</label>
                
                <input id="subjMatter" type="Text" autocomplete="off" value="{!v.subjTxt}"  class="slds-input" />
            </div>  
            
        </div><br/><br/>
        
        <div class="slds-col" aura:id="emailBodyDiv">   
            <div class="slds-form-element slds-size--1-of-1">
                <label class="slds-form-element__label" for="input-02">Content :</label>
                <span></span>
                <force:inputField class="richTxt" value="{!v.Opportunity.SampleRichText__c}"/>
            </div>
        </div><br/><br/>
        
        <aura:if isTrue="{!!empty(v.selTempl)}" >
            <lightning:textarea name="myTextArea" value="{!v.templDetail.Body}" 
                                label="Content:" maxlength="700" 
                                class="txtAreaCls" disabled="true" />
            <br/><br/>
        </aura:if>
        
        
    </div>    
    
    <br/>
    
    <!-- Error message area -->
    
    <br/><br/>
    <aura:if isTrue="{!v.shoMsg}" >
        <ui:message aura:id="errPanel"  title="Error" severity="error" closable="false">
            {!v.errorMsg}
        </ui:message>
        <br/><br/>
    </aura:if>
    
    <aura:if isTrue="{!v.shwSucesMsg}" >
        <ui:message aura:id="errPanel"  title="Success!" severity="confirm" closable="false">
            Email has been sent!
        </ui:message>
        <br/><br/>
    </aura:if>	
    
    <!-- Buttons and Loader section -->
    <div class="slds-align_absolute-center " style="padding:2%;">
        
        <lightning:button variant="brand" label="Send Email" onclick="{!c.sendEmailAction}" />
        
        <input type="button" value="Cancel" class="slds-button slds-button--neutral" onclick="{!c.closeDialog}"/>
        <aura:if isTrue="{!v.showLoader}">
            <div class="demo-only" style="height: 6rem;">
                <div role="status" class="slds-spinner_brand slds-spinner slds-spinner_small">
                    <span class="slds-assistive-text">Loading</span>
                    <div class="slds-spinner__dot-a"></div>
                    <div class="slds-spinner__dot-b"></div>
                </div>
            </div>
        </aura:if>  
        
    </div>     
</aura:component>
 
({
    
     loadComponent : function(component, event, helper) {
        helper.getSelctedAccountsOfOpportunity(component, event);
        helper.getEmailTempaltes(component, event);
        console.log(component.get("v.accIds"));
    }, 
   
    sendEmailAction : function(component, event, helper) {
        helper.sendEmails(component, event);
        
    },
    loadTemplate : function(component, event, helper) {
        helper.getTemplate(component, event);
        
    },
    closeDialog : function(component, event, helper)
    {
        helper.cancelAction(component, event);
    }
    
    
    
    
    
})
 
({
    getEmailTempaltes : function(component, event) {
        var action = component.get("c.getTemplates");
        //action.setParams({"divisionId":selectedDivision});
        
        action.setCallback(this,function(response){
            var loadResponse = response.getReturnValue();
            console.log('templates..!',loadResponse);
            
            if(!$A.util.isEmpty(loadResponse)){
                
                component.set('v.templates',loadResponse);
                
            }
        });
        $A.enqueueAction(action);
    },
    getSelctedAccountsOfOpportunity : function(component, event) {
        
        var accIdsofOpp = component.get("v.accIds");
        
        if(!$A.util.isEmpty(accIdsofOpp)){
            
            var action = component.get("c.getAccountsOfOpportunity");
            action.setParams({"accIds":accIdsofOpp});
            
            action.setCallback(this,function(response){
                var responseVal = response.getReturnValue();
                console.log('responseVal..** ',responseVal);
                
                if(!$A.util.isEmpty(responseVal)){
                    
                    component.set("v.accRecords",responseVal);
                    
                }
            });
            $A.enqueueAction(action);
        }
        
    },
    sendEmails : function(component, event) {
        component.set("v.shoMsg", false);
        component.set("v.showLoader", true);
        var selRec = component.find("lookupRes").get("v.lstSelectedRecords");
        var templateId = component.get("v.selTempl");
        var oppRecIds = component.get("v.oppIds");
        console.log('oppRecIds ', oppRecIds);
        console.log('sel records ', selRec);
        console.log('sel template ', templateId);
        
        //var addnlEmails = document.getElementById("addnlEmail").value;
        
        var subjMatter = document.getElementById("subjMatter").value;
        console.log('subjMatter ',subjMatter);
        var emailBody = !$A.util.isEmpty(component.get("v.Case").SampleRichText__c) ? component.get("v.Case").SampleRichText__c : '';
        console.log('emailBody ',emailBody);
        
        if(!$A.util.isEmpty(selRec) && (!$A.util.isEmpty(emailBody) || !$A.util.isEmpty(templateId)) ){
            
            var accIds = [];
            for (var i = 0; i<selRec.length; i++) {
                accIds.push(selRec[i].PersonContactId);
            }
            console.log('---accIds--- ', accIds);
            var accIdStr = !$A.util.isEmpty(accIds) ? accIds.join() : '';
            console.log('---accIdStr--- ', accIdStr);
            
            if(!$A.util.isEmpty(accIdStr) || !$A.util.isEmpty(addnlEmails)){
                
                console.log('---accIdStr--- ', accIdStr);
                console.log('--Case--', component.get("v.Case"));
                
                var action = component.get("c.sendAnEmailMsg");
                action.setParams({"templateId":templateId,
                                  "accIds":!$A.util.isEmpty(accIdStr) ? accIdStr : '',
                                  "opty":component.get("v.Case"),
                                  "subj" : !$A.util.isEmpty(subjMatter) ? subjMatter : '',
                                  "addnlEmails" : '',
                                  "oppIds" : oppRecIds });
                
                action.setCallback(this,function(response){
                    
                    var emailMsgResp = response.getReturnValue();
                    console.log('--emailMsgResp--', emailMsgResp); //isSuccess  errMsg
                    component.set("v.showLoader", false);
                    
                    if(emailMsgResp.isSuccess){
                        component.set("v.shwSucesMsg", true);
                        this.cancelAction(component, event);
                    }
                    else {
                        component.set("v.shoMsg", true);
                        component.set("v.errorMsg", emailMsgResp.errMsg);
                    }
                    
                });
                $A.enqueueAction(action);
            }
            
            
            
        }
        else {
            component.set("v.showLoader", false);
            component.set("v.shoMsg", true);
            component.set("v.errorMsg", "Please provide Recipient, Template or Email Body");
        }
        
    },
    getTemplate : function(component, event) {
        
        var templId = component.get("v.selTempl");
        component.set("v.showLoader", true);
        if(!$A.util.isEmpty(templId)){
            
            var action = component.get("c.getTemplateDetails");
            action.setParams({"templteId":templId});
            
            action.setCallback(this,function(response){
                component.set("v.showLoader", false);
                var responseVal = response.getReturnValue();
                console.log('responseVal..@getTemplate ',responseVal);
                
                if(!$A.util.isEmpty(responseVal)){
                    
                    component.set("v.templDetail",responseVal);
                    component.set("v.subjTxt",responseVal.Subject);
                    if(!$A.util.hasClass(component.find("emailBodyDiv"), "slds-hide")){
                        
                        $A.util.addClass(component.find("emailBodyDiv"), 'slds-hide'); 
                    }
                    
                }
            });
            $A.enqueueAction(action);
        }
        else {
            component.set("v.showLoader", false);
            if($A.util.hasClass(component.find("emailBodyDiv"), "slds-hide")){
                
                $A.util.removeClass(component.find("emailBodyDiv"), 'slds-hide');
            }
        }
    },
    cancelAction: function(component, event){
        var urlEvent = $A.get("e.force:navigateToURL");
        urlEvent.setParams({
            "url": '/500/o'
        });
        urlEvent.fire()
    }
    
})

 
Rishab TyagiRishab Tyagi
Hello SFDC@Error,

It seems like there is some error in your controller 'ListEmailOppCntrlr'. Please check the logs generated against your component in your Developer Console. There must be some error message in your status column under the logs tab.

Hope that answers your question.
SFDC@ErrorSFDC@Error
Hi @Rishab

I have tried in log to find out the issue. but i am not unable.
public class ListEmailOppCntrlr {
    
    
    
    @AuraEnabled
    public static list<EmailTemplate> getTemplates(){
        //RelatedEntityType='Account' AND 
        list<EmailTemplate> emailTemp = new list<EmailTemplate>();
        emailTemp = [SELECT Id,Name,Subject,TemplateType FROM EmailTemplate WHERE TemplateType IN ('custom','text')];
        return emailTemp;
    }
    
    @AuraEnabled 
    public static list<Account> getAccountsOfOpportunity(string accIds){
        
        list<string> accIdsList = string.isNotBlank(accIds) ? accIds.split(',') : new list<string>();        
        list<Account> accRecords = accIdsList.size()>0 ? [SELECT Id,Name,PersonContactId FROM Account WHERE ID IN:accIdsList] : new list<Account>();        
        return accRecords;
    }
    @AuraEnabled 
    public static EmailTemplate getTemplateDetails(string templteId){
        
        EmailTemplate emailTemp = new EmailTemplate();
        list<EmailTemplate> emailTempLst = new list<EmailTemplate>();
        emailTempLst = [SELECT Id,Name,Subject,TemplateType,body FROM EmailTemplate WHERE ID=: templteId];
        
        emailTemp = emailTempLst.size()>0 ? emailTempLst[0] : emailTemp;
        return emailTemp;
        
    }
    
    @AuraEnabled
    public static sucesMsg sendAnEmailMsg(string templateId, string accIds, Case opty, string subj, string addnlEmails, string oppIds){
        try {
            List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
            Map<Id,Account> accMap = new map<Id,Account>();
            list<Case> oppList = new list<Case>();
            list<string> accIdList = string.isNotBlank(accIds) ? accIds.split(',') : new list<string>();
            list<string> oppIdList = string.isNotBlank(oppIds) ? oppIds.split(',') : new list<string>();
            list<string> addnlEmailList = string.isNotBlank(addnlEmails) ? addnlEmails.split(',') : new list<string>();
            
            system.debug('---accIdList-- '+accIdList);
            system.debug('---templateId--- '+templateId);
            system.debug('---oppIdList--- '+oppIdList);
            
            if(accIdList.size()>0 && oppIdList.size()>0)
            { 
                accMap = new Map<Id,Account>([select id,name,personEmail,PersonContactId from Account WHERE PersonContactId IN: accIdList]);
                oppList = [select id,AccountId from Case WHERE ID IN: oppIdList];
                for(Case oppRec : oppList)
                {
                    
                    if(oppRec.accountId!=null && accMap.containsKey(oppRec.AccountId))
                    {
                        
                        Account persnAcc = accMap.get(oppRec.AccountId);
                        if (persnAcc.personEmail != null) 
                        {
                            
                            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();                                   
                            
                            if(string.isNotBlank(templateId)){
                                mail.setTemplateId(ID.valueOf(templateId));//00XW0000000MjvI 00XW0000000MjuyMAC
                                mail.setTargetObjectId(persnAcc.PersonContactId);                                  
                                mail.saveAsActivity=true;
                                mail.setWhatId(oppRec.id); //006W000000DYyeL
                                
                            }
                            else if(string.isNotBlank(opty.SampleRichText__c)){
                                mail.setSubject(string.isNotBlank(subj) ? subj : '');
                                List<String> sendTo = new List<String>();
                                sendTo.add(persnAcc.personEmail);
                                mail.setToAddresses(sendTo);
                                mail.setHtmlBody(opty.SampleRichText__c);
                                mail.saveAsActivity=true;
                                mail.setWhatId(oppRec.id);
                            }                    
                            // Step 5. Add your email to the master list
                            mails.add(mail);
                        }
                    }
                    
                    
                }
                
                
                
            }
            
            if(mails.size()>0){
                
                try {
                    
                    Messaging.sendEmail(mails);
                    return new sucesMsg(true, 'Sucess!');
                    
                } catch(Exception e) {
                    system.debug('*************--Exception @SendEmail method ---*********');
                    System.debug('****--Exception type caught: ' + e.getTypeName());    
                    System.debug('****--Message: ' + e.getMessage());    
                    System.debug('****--Cause: ' + e.getCause());    
                    System.debug('****--Line number: ' + e.getLineNumber());    
                    System.debug('****--Stack trace: ' + e.getStackTraceString());    
                    
                    return new sucesMsg(false, e.getMessage()+' - '+e.getCause()+' - '+e.getStackTraceString()+' - '+e.getLineNumber());
                }
                
                
            }     
            return new sucesMsg(true, 'Sucess!');
        }
        catch(Exception e) {
            system.debug('*************--Exception @sendAnEmailMsg method ---*********');
            System.debug('****--Exception type caught: ' + e.getTypeName());    
            System.debug('****--Message: ' + e.getMessage());    
            System.debug('****--Cause: ' + e.getCause());    
            System.debug('****--Line number: ' + e.getLineNumber());    
            System.debug('****--Stack trace: ' + e.getStackTraceString());    
            
            return new sucesMsg(false, e.getMessage()+' - '+e.getCause()+' - '+e.getStackTraceString()+' - '+e.getLineNumber());
        }
    }    
    
    public class sucesMsg {
        
        @AuraEnabled public boolean isSuccess;
        @AuraEnabled public string errMsg;
        
        public sucesMsg(boolean isSuccess, string errMsg){
            this.isSuccess = isSuccess;
            this.errMsg = errMsg;
        }
        
    }
}

 
Rishab TyagiRishab Tyagi
User-added image

Is the Status same in your Developer Console?
I believe that there is some issue with your sendAnEmailMsg function. The status window will further help you debug the issue. If it still does not works then post the Log generated with the related function.
SFDC@ErrorSFDC@Error
Hi Tyagi,
I am new in the lighting component. Please help me how to solve this.