I am comparing two date time fields in Apex.If start date and time is greater than end date,I have to display error in salesforceLightning component.

 Datetime EventStartDate= datetime.valueOf(stringdate1);
 Datetime EventEndDate= datetime.valueOf(stringdate2);

 if(EventStartDate< EventEndDate){    
     //How to throw this error to javascript side

How to display error message in javascript side based on server side error.

2 Answers 2


On the server, use the AuraHandledException:

if(EventStartDate > EventEndDate) {
  throw new AuraHandledException('End date must be after start date');

You can then detect this situation in your controller's callback:

if(response.getState() === "ERROR") {
  // show the message with a toast or other mechanism

I think you can throw error in apex using

throw new AuraHandledException('');

More info here https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_apex_custom_errors.htm

Hope it will 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.