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 ?
Best Answer
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: