0

Is it possible to embed two call back function in single function in lighinting component code:

Apex class:

@AuraEnabled
public static Profiles__c getconfig()
{
return Profiles__c.getInstance();
}

lightning component code:

({
    doInit : function(component, event, helper){

       component.set("v.foundBusinessUnitData", true);
        var userBizUnit;
        var action = component.get("c.getCurrentUserBusinessUnit");

        action.setCallback(this, function(data){
            var userBusiness = data.getReturnValue()[0];
            debugger;
            if(userBusiness!=null){



               component.set("v.foundBusinessUnitData", true);
        var action = component.get("c.getconfig");
        action.setCallback(this, function(data){
            component.set("v.foundBusinessUnitData", true);
        var customsetting  = data.getReturnValue();

        if(customsetting.Profile_Type1__c){



                    if(customsetting.Profile_Type2__c){

                       }
                    else if(customsetting.Profile_Type3__c){

                    }
              }
                    else{
                        component.set("v.isNewButton",true);
                    }



        }
         $A.enqueueAction(action);  

            }
            else{

            }
        });
        $A.enqueueAction(action);
3

getconfig returns only one record, but you're trying to use it like an array.

 var userBusiness = data.getReturnValue()[0];

Use the return value directly from getReturnValue:

 var userBusiness = data.getReturnValue();

Your second action should be called then. As noted in the comments, you may want to refactor your code as a Promise chain, which would be more legible.

0

Yes, you can

  1. call another callback function in the response of the first callback or
  2. use a promise pattern to achieve the same result.

The 2nd option is cleaner, but both will achieve the same end result. Depending on how committed you are to one pattern should help determine which choice to make.

Also, if possible you should refactor your backend to send all the data you need in one response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.