function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Surender reddy SalukutiSurender reddy Salukuti 

How can i show Error messages based on error code I need show messages

Hi Everyone 

I have a requirement I need to show error messages in UI Based on error codes 

Error codes are provided from the client I am following the method 

if(errorcode == '20002'){
component.set ('v.message','Detailes are not correct');

}

But hard coding error codes are not good practice how can I implement this functionality 

 

Thank you 
Surender reddy

 

Maharajan CMaharajan C
Hi Surender,

You can use the Custom Labels. Store the error messages in Custom Label.

Setup -> Custom Label.

Then  in Js using the below syntax you can refer the custom labels.
$A.get("$Label.namespace.labelName")

For Example if i have  "Details are not correct" in Custom Label  detailserror . Then i have to use the below code.
if(errorcode == '20002'){
    component.set ('v.message', $A.get("$Label.c.detailserror") );
}
https://www.biswajeetsamal.com/blog/custom-label-in-lightning-component/

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/labels_dynamic.htm​​​​​​​ (https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/labels_dynamic.htm)


Thanks,
Maharajan.C
Surender reddy SalukutiSurender reddy Salukuti
Thanks for your response , I need to store error codes also how can I do that
mukesh guptamukesh gupta
Hi Surender,

First you need to 

Add below line in componet:-
<aura:attribute name="errorCodeMap" type="map"/>


Apex class:
public class Mapvalue {
    @AuraEnabled     // Method must be AuraEnabled in apex
    public static map<string,string> getmymap()
    {
        map<string,string> putkeyvalue= new map<string,string>();
        putkeyvalue.put('errorCode1', 'Value1');  // Setting key,value in map
        putkeyvalue.put('errorCode2', 'Value2');
        putkeyvalue.put('errorCode3', 'Value3');
        return putkeyvalue;
    }
}

Javascript Controller,
 
({
 doinit : function(component, event, helper) {  
      var action=  component.get('c.getmymap');  // Calling apex method
        action.setCallback(this,function(response)   // getting response back from apex method
        {
        var state=response.getState();            // getting the state
        if(state==='SUCCESS')
        {
      component.set('v.errorCodeMap',response.getReturnValue());   // setting the value in map attribute
     

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

After that you can get error code:-
 
var conts = component.get('c.errorCodeMap');;
for ( var key in conts ) {
if(errorcode == key){
  component.set('v.message',conts[key]);
}
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Maharajan CMaharajan C
Same you can store the error codes in Custom Label.
 
var error1 = $A.get("$Label.c.errorcode1");   /// Store 20002 in errorcode1 custom label
var error2 = $A.get("$Label.c.errorcode2");   /// Store 20003 in errorcode1 custom label
var error3 = $A.get("$Label.c.errorcode3");   /// Store 20004 in errorcode1 custom label

if(errorcode == error1){
    component.set ('v.message', 'Detailes are not correct' );
}
else if(errorcode == error2){
	component.set ('v.message', 'Error Message 2' );
}
else if(errorcode == error3){
	component.set ('v.message', 'Error Message 3' );
}

Thanks,
Maharajan.C