1

I'm new to lightning. Need your help.

I have to check for a field value in Case fields and add an error Div / Message on the component. This component will be used for Lightning actions as a custom button. I have tried this to pass the value of the String to js Controller but it throws null error.

Component :

<aura:component controller="FreightJSONAWBController" implements="flexipage:availableForAllPageTypes,force:hasRecordId,force:lightningQuickAction">
<aura:attribute name="messageError" type="Boolean" />
<aura:attribute name="message" type="String" />
<aura:attribute name="recordId" type="String" />

<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<aura:handler event="force:refreshView" action="{!c.doInit}" />

<aura:if isTrue="{!v.messageError}">
    <!-- Load error -->
    <div class="userCreateError">
        <ui:message title="Error" severity="error" closable="true">
            <ui:outputText value="{!v.message}"/>
        </ui:message>
    </div>
</aura:if>  

Apex Controller:

    global class Freight_AWBDetailsRequestController {

    public class ErrorHandler{

        @AuraEnabled
        public Boolean isSuccess { get; set; }

        @AuraEnabled
        public String message { get; set; }

        public ErrorHandler(Boolean isSuccess,String msg){
            this.isSuccess = isSuccess;
            this.message = msg;
        }
    }

    @AuraEnabled
    public static ErrorHandler getRecordUpdate(Id CaseId){

        String msg;
        ErrorHandler errH;

        Case caseRecord = [SELECT Id, Freight_AWB_Number__c, Freight_AWB_Status__c FROM Case WHERE Id =: CaseId];

        // Checking availability of AWB Number, accordingly requesting 
        if(caseRecord.Freight_AWB_Number__c != ''){
            requestJson = getRequestString(username, password, caseRecord.Freight_AWB_Number__c);
        }
        else{
            // show error AWB Number not provided. Lightning work to be done
            msg = 'AWB Freight Number cannot be null';
            errH = new ErrorHandler(false,msg);
            return errH;
        }

        try{
            caseRecord = returnParsedCase(res.getBody(), caseRecord);
            caseRecord.Freight_AWB_Status__c = 'Infomation Recieved';
            update caseRecord;
            errH = new ErrorHandler(true,'');
            return errH;
        }
        catch (JSONException jsonExcep){
            system.debug(' @@ No Response @@ ' + jsonExcep);
            caseRecord.Freight_AWB_Status__c = 'Infomation Not Available';
            msg = 'Case did not update. Error has occured.';
            update caseRecord;
            errH = new ErrorHandler(false,msg);
            return errH;
        }


    }
}

Client side controller :

    ({
    doInit : function(component, event, helper) {

        var action = component.get("c.getRecordUpdate"); // dummy call for dummy method

        action.setParams({
            "CaseId" : component.get("v.recordId")
        });
        console.log("caseId :" + component.get("v.recordId"));

        action.setCallback(this, function(response) {
            var output = response.getReturnValue();
            console.log('output :'+output);

            var state = response.getState();
            if(component.isValid() && state == "SUCCESS" && output.isSuccess){
                $A.get("e.force:closeQuickAction").fire();
                var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    "title": "Success!",
                    "message": "AWB Details updated to Case Successfully!"
                });
                toastEvent.fire();
                $A.get('e.force:refreshView').fire();
            } else if (state == "ERROR" || !output.isSuccess) {
                var errors = response.getError();
                if (errors[0] && errors[0].message) {
                    // Did not catch on the Server Side
                    component.set("v.message", errors[0].message);
                    component.set("v.messageError", true);
                } else if ( !output.isSuccess ) {
                    // Did catch on the Server Side
                    component.set("v.message", output.message);
                    component.set("v.messageError", true);
                }
            }

        });
        $A.enqueueAction(action);
    }
})

In Console log, am getting output value as null. What am i missing here ?

4
  • What is the value not returned? – Martin Lezer Aug 16 '17 at 12:39
  • I wanted the error message "AWB Freight Number cannot be null" to display on v.message. But it throws null on isSuccess. and Component is throwing error. – AjaySFDC Aug 16 '17 at 12:45
  • I guess your getRequestString(username, password, caseRecord.Freight_AWB_Number__c); line raises an exception. So your errH variable is not instantiated yet. This is why you receive a null variable. Try to replace this line: ErrorHandler errH; by: ErrorHandler errH = new ErrorHandler(false,'If I get this error message, this is because the getRequestString() method failed.'); – Martin Lezer Aug 16 '17 at 12:51
  • That != '' condition was the mistake on the code. Now i changed my code and it works. Thanks for your help martin. – AjaySFDC Aug 16 '17 at 13:25
1

By a "null error", I'm assuming you mean a null pointer exception. If that's the case, you'll want to use a try-catch block where you catch the null-pointer exception. I'd also recommend you change your If condition to isEmpty. Your code in the server side controller would look something like this:

try{

if(!isEmpty(caseRecord.Freight_AWB_Number__c){
            requestJson = getRequestString(username, password, caseRecord.Freight_AWB_Number__c);
        }
}catch(nullpointerexception e){

            // show error AWB Number not provided. Lightning work to be done
            msg = 'AWB Freight Number cannot be null';
            errH = new ErrorHandler(false,msg);
            return errH;
}
1
  • I found the mistake just as the same time i read your comment :). I added a different null check by adding String.isNotBlank and it got the code working. Thanks for your help :) – AjaySFDC Aug 16 '17 at 13:24
1
if(String.isNotBlank(caseRecord.Freight_AWB_Number__c)){
            requestJson = getRequestString(username, password, caseRecord.Freight_AWB_Number__c);
        }
        else{
            // show error AWB Number not provided. Lightning work to be done
            msg = 'AWB Freight Number cannot be null';
            errH = new ErrorHandler(false,msg);
            return errH;
        }

I found my mistake. That != '' did not work . So i tried with String.isNotBlank and it works fine. Thanks for help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.