salesforce data migration services

Learn Salesforce Lightning with Examples – Part 2 (Handling Apex Messages in Lightning Component)

Learn Salesforce Lightning with Examples – Part 2 ( Handling Apex Messages in Lightning Component ) is the second part of the series – “Learn Salesforce Lightning with Examples” where We would share the key practical examples and experience with lightning.We are focusing on Lightning Components for now.

Background,Blog Contributors and Pre-Requisites

To get started for this blog you need to fulfill all the Pre-requisites mentioned here.In this link,you will also get to know the background and blog contributors for this series.

Handling Apex Messages in Lightning Component

Business Case:

Adam is working as a Senior Application Developer in Universal Containers. Company wants to move their traditional (classic version )recruitment app to lighting. Adam needs to do a Proof of Concept by building a custom lighting component for creating the Candidate’s record into Salesforce. Now that he has created the first part which is explained here,He would like to showcase even users know the success or error messages occurring from Apex.This will help users to understand if there have been receiving any error from Apex and Handling Apex Messages in Lightning Component if required with custom messages.

Solution:

Adam decided to handle the error and success messages using wrapper class in apex and then use it displaying the error messages in Apex.

Before getting started,Adam makes sure that he doesn’t miss any pre-requisites mentioned here.Below are the steps Adam takes for creating the solution after completing the pre-requisites.If you have followed my previous blog,then you just need to update the components and follow these steps(Please note that if you just copy paste the complete code then you need not follow my previous blog,but I would insist you to do so if you want to learn in details):

  1. Define Wrapper Class in Apex
  2. Update Apex Class and and method.
  3. Get response(wrapper data) in lightning controller
  4. Test the Application

Define Wrapper Class in Apex

Create a Wrapper Class inside the existing Apex Class or a separate class like this :

[codeblocks name=’AuraProcessingMessageWrapperClass’] 

you need to twist your code accordingly so that the apex code saves messages from wrapper code accordingly.Your complete apex class should look like :

[codeblocks name=’CreateCandidateRecord_2′] 

Get response (wrapper data) in lightning controller

In lightning controller Check the Boolean flag if method executed successfully and display error or success message accordingly.Like this :

[codeblocks name=’CreateCandidateRecordController_2.js’] 

Note : There will be no change in component and application code mentioned in previous post.

Test the Application

Now since We are done with the codes,lets test the application.So in our case,We need to try with the URL format :

https://{Domain Name}.lightning.force.com/c/{App Name}.app

and then you would get a similar page like this :

Lightning App Screen

Error Message

So now if there is any exception/error occurs while apex operations,then you would get an apex error message as an alert which would help you to easily identify the error similar to like this :

ErrorMesg

In the above example,Since the maximum length for the SSN field is set to 9,If you put any numbers beyond it,You will get exception message thrown.

Success Message

If you fill the information without any exception,then you would get a success message as well showing like this :

SuccessMesg

You can also create pop- ups to display these messages in them to give more good look and feel.

You can also download the code from the github repository here.

Hope you enjoyed the blog! Stay tuned and there will be more examples to come ..