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
Ayush Kumar 31Ayush Kumar 31 

doInIt: Can we call more than one server method from doInIt?

I've two methods that query on Account and a custom object, and I need values from both of them to initialize attributes on component. Can any one help to achieve this?
SKSANJAYSKSANJAY
Hi Ayush,

There is a question for you: Are you asking for lightning controller doInit Method.

Yes you can make as many server calls from doInit method. The best way is to use multiple helper methods which will seperately call apex methods.

Consider : getAccountList and getCustomObjectRecordList are two apex method then in helper create two seperate functions. Call these methods from DoInit : helper.getAllAccounts(component); helper.getAllObjectRecords(component);

MyCompHelper.js
getAllAccounts :function(component){
    try {  
        var action = component.get("c.getAccountList");            
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (component.isValid() && state === "SUCCESS") {
                var getAllAccountResponse = response.getReturnValue();         
                console.log("getAllAccountResponse : ", JSON.stringify(getAllAccountResponse));
                
                //Parse the response according to your requirement
            }
        }
    }
},
getAllObjectRecords :function(component){
    try {  
        var action = component.get("c.getCustomObjectRecordList");            
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (component.isValid() && state === "SUCCESS") {
                var getAllObjectRecResponse = response.getReturnValue();         
                console.log("getAllObjectRecResponse : ", JSON.stringify(getAllObjectRecResponse));
                
                //Parse the response according to your requirement
            }
        }
    }
}

Hope this will help you.

Thanks,
Sanjay
RudrarajuRudraraju
Hi Ayush,
Move the two methods from controller to helper and In controller call those two helper methods in doInIt
Ayush Kumar 31Ayush Kumar 31
Hi Sanjay,
Thank you for answer.
I'm new to lightning and would be grateful to you if you could tell that as we call twice to the server so do we need to have enqueueAction in each of the functions? Here in your answer would it have two $A.enqueueAction(action), at the end of each response?
Ashif KhanAshif Khan
Hi Ayush
yes you need  two $A.enqueueAction(action) for server calling.
but I would say instead of two servers calling in lightning you can perform your work with single server call as

use return type of @AuraEnabled method return type as
Map<string, List<sObject>>
and in JS controller response result
 
if (component.isValid() && state === "SUCCESS") {
                var accountData= response.getReturnValue()['keyAccount'];         

                var customObjectData= response.getReturnValue()['keyCustomObject'];         
              

                
                //Parse the response according to your requirement
            }

Let Me know Is it work for you.
Regards
Ashif
SKSANJAYSKSANJAY
Hi Ayush,

Yes you are correct, you must call for both the apex methods using $A.enqueueAction(action) in helper function. enqueAction is the process of adding apex method call to queau which will be processed in batch. This is the standard process of making AJAX call from lightning controller or helper. The best practice is to make apex method call from helper function and call these helper functions from corresponding controller functions.

Hope this will help you... In case you need help in lightning coding standard, feel free to post.

Thanks,
Sanjay