How to nest server side call in Lightning component in Salesforce?

How to nest server side call in Lightning component in Salesforce?

Sample Code:


Sample.cmp:


<aura:component implements=”force:appHostable” controller=”Sample”>
    
    <div class=”slds-box slds-theme_default”>
    <lightning:button variant=”brand” label=”Click It” onclick=”{!c.myAction}”/>
    </div>
    

</aura:component>


SampleController.js:


({
myAction : function(component, event, helper) {
var act1 = component.get(“c.action1”);
        act1.setCallback(this, function(response){
            var state = response.getState();
            if (state === “SUCCESS”) {
                alert(response.getReturnValue());
                var act2 = component.get(“c.action2”);
                act2.setCallback(this, function(response){
                    var state = response.getState();
                    if (state === “SUCCESS”) {
                        alert(response.getReturnValue());
                    }
                });
                $A.enqueueAction(act2);
            }
        });
        $A.enqueueAction(act1);
}

})


Sample Apex Class:


public class Sample {

    @auraEnabled
    public static String action1() {
        return ‘a’;
    }

    @auraEnabled
    public static String action2() {
        return ‘b’;
    }
    

}


Output:


Leave a Reply