Interview Questions – Lightning Framework – Part 10 – Apex Controller

Interview Questions – Lightning Framework – Part 10 – Apex Controller

Checkout earlier posts in interview question series to read previous questions.

81. What are the considerations to make an apex method available for Lightning Components?

You can make any server-side apex method available to be invoked from Lightning Component. Before doing so, you need to make sure:

  • Your class is either global or public.
  • The method you want to call must be either public or global.
  • The method must be static.
  • The method must be annotated with @AuraEnabled annotation.
  • You should avoid using the same method name as your client side method name.
    Example:
    public class ServerClass {
    @AuraEnabled
    public static String serverMethod(String param){
    return ‘value’;
    }
    }


82. What are some of the basic steps to call an Apex Controller method?

Before you can call an Apex method, you need to create the binding between your component and apex controller. Follow the below steps:

  • Create an AuraEnabled method in your Apex class. (Read question 81 for all considerations of AuraEnabled method)
  • Bind component and apex class together. For this, set “controller” attribute in your component with the class name. Example:
    <aura:component controller=“MyCustomController”>
  • Define an action on client-side JS controller. (Read question 83)
  • Enqueue the action.
  • Handle the callback response.

83. What is an action in client-side JS controller?
An action allows you to make a call to the Apex controller method. Every action has:
– Name of the Apex method to be called.
– The parameter to pass to apex method (optional)
– A callback method which is called on server response

Every action needs to be enqueued before you can make a call to the server. The lightning framework combines all enqueued method from a single transaction in one bundle, and send all these requests in one go instead of making multiple server calls. This ensured that we are not making multiple server side call. However, the callback of each method is returned individually.

Example of a basic action is:

//creating an action with server side method 
var action = component.get(“c.serverMethodName”);

//setting parameter in the action to pass to parameter

action.setParams({
"paramName" : paramValue,
});

//assigning a callback function

action.setCallback(this,function(response) {
var state = response.getState();
if (state === "SUCCESS") {
// Handle response here - response.getReturnValue();
}
});

//enqueuing the action

$A.enqueueAction(action);

84. What are callback methods?

Callback methods are used to execute a task after an async operation is completed. For example, you can execute a Javascript function after a Javascript promise(Async operation) either resolved or failed.
In case of Lightning Framework, we use the callback method with actions, to execute a task on the client side once server response is received.

85. How many server calls can you make from a component?

You can make unlimited server-side call from an Aura Component, however, it always advised to limit the number of calls to a minimum.

86. How can you store the apex method response in browser cache?

To store the response of a server-side call, you can either of the following:

  • You can mark your client action as storable using setStorable() function, which stores the server response in the browser cache. Example:
    action.setStorable();
  • You can also mark your AuraEnabled method as cacheable, which will store the response in the browser cache. Example
@AuraEnabled(cacheable=true)
public static String serverMethod(String param){
return ‘value’;
}

The key difference here is, if you use the 1st approach using setStorable(), only the particular action will be cached. If another action makes a call to the same server method, the response will not be cached. However, if you use the 2nd approach using cacheable=true, this makes your server method cacheable, which means all action’s response will be cached.

87. What is the refresh interval and expiration interval?

Refresh Interval – Refresh interval is the time for which the cached response is treated as fresh data. If another server call is made within refresh interval, no server call will be made and response will be returned from the browser cache. In lightning, it is 30 sec.
Expiration Interval – Expiration interval is the time of cached data expiration. After the expiration interval, the data from cache will be deleted. In lightning it is 900sec.


88. How does the storable method works?

Storable Action Lifecycle
Image from: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_storable_lifecycle.htm

 

– If no cache found in browser, the server call will be made and response will be cached.
– If cache found in the browser, and the call is made within refresh interval (30 sec), no server call is made and response will be returned from the browser cache.
– If cache found in the browser, and call if made after refresh interval (30 sec) and before expiration interval (900 sec), the first response will be returned from the browser cache, however, the framework will also make a server call in the background to get a fresh copy of the data. Once the data is returned from the server, another callback exception will happen.

89. How can we chain server-side calls or how can we make another server call after we get the response from the first one?

You can make another server-side call from the callback method of the first call. This will always ensure that a 2nd server call will be made only after the response of the first one.

90. How can you return the response from multiple object/classes?

You can use wrapper class here to combine response from multiple Salesforce object or data from multiple class. Example:

public class WrappedResponse{
@AuraEnabled public String searchString;
@AuraEnabled public List<Account> accountsWithSearchString;
@AuraEnabled public List<Lead> leadWithSearchString;
@AuraEnabled public Integer totalRecords;
@AuraEnabled public CustomApexClass classResult;
}

Manish Choudhari

I am a certified Salesforce Application & System Architect and Developer working on Salesforce Technology since 2014. Currently, I have 14 Salesforce certifications along with OCPJP (Oracle Certified Profession JavaSE6 Programmer) working in Salesforce.com Hyderabad as a Technical Engineer. Writing technical blogs, learning new technologies and frameworks and sharing knowledge is my hobby.

Leave a Reply

Close Menu