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
MythiliMythili 

Can we have action.setCallback() inside another action.setCallBack()??

Hi,
Can we have action.setCallback() inside another action.setCallBack()??
My code goes like this:

controller.js
doInit : function(cmp,event,helper){
//call apex server method1 
action1.setCallBack(this,function(response){
    console.log('INSIDE action1');
    if(state == "SUCCESS"){
      //call apex server method2
     action2.setCallBack(this,function(resp){
          console.log('INSIDE action2');
      });
   }
});
}

On executing this code, 'INSIDE action1' is getting logged whereas 'INSIDE action2' is not getting logged. But apex method2 is getting invoked and both the apex methods works fine. 
Can we have an action.setCallback() inside another action.setCallback()??

Thanks,
Mythili

Suresh KandruSuresh Kandru
Hi,  just call helper methods and separate call back functions. always better to simplify components logic and separate components logic.
Raj VakatiRaj Vakati
Yes .. Code is here

 
public class demoCls {
    @Auraenabled 
    public static String message1(){
        return 'Hello ';
    }
    @Auraenabled 
    public static String message2(){
        return 'World ';
    }
    
    
}
 
<aura:component controller="demoCls">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    
</aura:component>
 
({
    doInit: function (cmp, event) {
        var action = cmp.get("c.message1");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                console.log(response.getReturnValue());
                var action2 = cmp.get("c.message2");
                action2.setCallback(this, function(response) {
                    var state = response.getState();
                    if (state === "SUCCESS") {
                        console.log(response.getReturnValue());
                        
                    }
                    
                });
                $A.enqueueAction(action2);
            }
            
        });
        $A.enqueueAction(action);
    }
})

 
MythiliMythili
Raj,
Thanks for the code.
Please do some DML operation in message2 function and then check action2.setCallback. It is not getting executed. 
Raj VakatiRaj Vakati
Its working ..see the code here 
 
public class demoCls {
    @Auraenabled 
    public static void message1(Account acc){
        insert acc ;
    }
    @Auraenabled 
    public static void message2(Account acc){
        insert acc;
    }
    
    
}
 
<aura:component controller="demoCls">
    <aura:attribute name="acc" type="Account" default="{'sObjectType':'Account', 'Name':'Account123'}"/>
   
    
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    
</aura:component>
 
({
    doInit: function (cmp, event) {
        var action = cmp.get("c.message1");
        action.setParams({acc:cmp.get("v.acc")});
        
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                console.log(response.getReturnValue());
                var action2 = cmp.get("c.message2");
                action2.setParams({acc:cmp.get("v.acc")});
                
                action2.setCallback(this, function(response) {
                    var state = response.getState();
                    if (state === "SUCCESS") {
                        console.log(response.getReturnValue());
                        
                    }
                    
                });
                $A.enqueueAction(action2);
            }
            
        });
        $A.enqueueAction(action);
    }
})

 
MythiliMythili
Thanks Raj.
I did some research around it and found that it breaks in the following scenario:
When I call e.force:closeQuickAction in js controller to close the white screen that pops up as soon the button is clicked. I am doing this because I do not want the white screen to be displayed. I just want some function to take place at the backgroud.
If     $A.get("e.force:closeQuickAction").fire(); had not been there in the code, then both the action.setCallback() gets fired.

I am posting my code here:
<aura:component controller="demoCls" implements="force:lightningQuickActionWithoutHeader">
 <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
</aura:component>

({
    doInit: function (cmp, event) {
        var action = cmp.get("c.message11");
        action.setCallback(this, function(response) {
            $A.get("e.force:closeQuickAction").fire();
            var state = response.getState();
            if (state === "SUCCESS") {
                console.log(response.getReturnValue());
                var action2 = cmp.get("c.message22");
                action2.setParams({
                    "acc" : response
                });
               
                action2.setCallback(this, function(response) {
                    console.log('dfsdgfsg');
                    var state = response.getState();
                    if (state === "SUCCESS") {
                    }
                    
                });
                $A.enqueueAction(action2);
            }
            
        });
        $A.enqueueAction(action);
    }
})

public class demoCls {
   @Auraenabled 
    public static Account message11(){
        Account acc = [select id,Name from Account where id='0019000000yqP5fAAE'];
        return acc;
    }
    
    @Auraenabled 
    public static void message22(Account acc){
        update acc;
    }
}