Lightning Components Tip: Don’t mess with Method names

In 2014, Salesforce introduced the front-end platform called Lightning, the next generation of CRM is based on front-end web technologies, providing much functionality to help customers close deals faster and smarter. Sales teams now can see and present data to their customers and potential customers in visualized ways. Lightning is a component-based framework allowing components to be reused in different places within organisation and admins to quickly customize pages for different needs from various users.

When it comes to lightning component development, lightning makes it easier and faster for developers to build and debug applications using frontend tools. Three basic elements of lightning components are Aura component, frontend controller (JavaScript) and backend controller (Apex). JavaScript controller acts as main controller to process business logic and Apex controller is more like database interpreter to query data from Salesforce Database.

However, when many methods have same actions, naming problems come in. In apex, the action methods to return some data are normally are declared in the format getVariableName. Similarly, in lightning, JavaScript methods in JS controller usually are declared in the same way, getVariableName to get data from Apex controller. Inside JS methods, an action variable is declared to retrieve data from Apex controller, which often is named getVariableName. This action will be put into a queued action list. Here is a screenshot how it looks.

In JS controller,

JS controller

In Apex controller,

apex controller

As a result, it creates an infinite loop which keeps firing getStrings function. The reason is that the queued action list has taken the method name, not the action variable. The queued action list fires getStrings function, inside the function, a statement puts function again into the list, and so on.

To avoid this problem, JS functions in JS controller can be named as getVariableNameFunction and inside the functions, query action variables can be named as getVaribleNameAction. By doing this, normal naming convention remains the same and can avoid confusion for Lightning framework when picking the actions up.

JS Controller:

fixed js controller

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s