Showing Error from ServerSide controller in UI using Lightning Components



Error In Server Side Controller ---Error Exception happendInvalid field Count for AggregateResult



ServerSide Controller




public class Agg_exam_opp {
     @AuraEnabled
    public Static list<aggregateresult> aggEx()
    {
        try{
            list<aggregateresult> aggres=[SELECT StageName Name,Count(Id) Cnt FROM Opportunity GROUP BY Stagename];
             for(aggregateresult agg:aggres)
            {
                System.debug('avgval'+agg.get('Name'));
                System.debug('conval'+agg.get('Count'));
            }  
             return aggres;
        }catch(Exception e){
           
            throw new AuraHandledException('Error Exception happend'+e.getMessage());          
           
        }
    }
   
}


Aura Component


<aura:component controller="Agg_exam_opp">
               
      <aura:attribute   name="errorMessage" type="String" default="test"/>
                 <aura:attribute   name="showError" type="Boolean" default="false"/>
   
        <aura:handler name="init" value="{!this}" action="{!c.doinit}"></aura:handler>
   
    {!v.errorMessage}
  <div>  boolean val is {!v.showError}
      </div>
                  <!-- If v.showError is true it will display Error>
      <aura:if isTrue="{!v.showError}">
        <!-- Show the message to ui -->
        <div class="slds-notify slds-notify_toast slds-theme_error">
            <span class="slds-assistive-text">error</span>
            <div class="slds-notify__content">
                <h5 class="slds-text-heading_small slds-align_absolute-center">Error Message </h5>
                <br/>
                <p class="slds-align_absolute-center">{!v.errorMessage}</p>               
            </div>
        </div>
    </aura:if>    
</aura:component>



JS Controller

({
    doinit:function(component,event,helper)
    {
        //Calling method AuraMethod from server side controller
        var action=component.get("c.aggEx");
        action.setCallback(this,function(response){
            var state=response.getState();
            if(state==='SUCCESS')
            {
                console.log("executed");
                var res=response.getReturnValue();
                alert(JSON.stringify(res));
                console.log(res);
                //component.set("v.acc",res);
            }
            else if(state == "ERROR"){
                var errors = response.getError();
                console.log('errors is' +errors);
                //alert(JSON.stringify(errors));
                console.log(JSON.stringify(errors));
                console.log('executed in erros');
               //setting errors to Show Error Atributes
                component.set("v.showError",true);
                component.set("v.errorMessage",errors[0].message);
            }
        });
        $A.enqueueAction(action);
    }
})



OUTPUT:


Comments

Popular posts from this blog

Lightning Grid Custom Datatable

Comparing Two Values Using Aura If

Dynamic Standard Datatable Using Fieldset