+ Start a Discussion
AmrutAmrut 

Accessing data of action.setCallback outside action.setCallback method

var action = component.get("c.fetchData");
   action.setCallback(this, function(response) {
                        var state = response.getState();
                        console.log("State " + state);
                        if (state === "SUCCESS") {
                            var storeResponse = response.getReturnValue();
                            component.set("v.Customer", storeResponse);
                            var Credit = storeResponse.field__c;
                            console.log("Credit  "+Credit);
                        } });
                    $A.enqueueAction(action);
                }

Not able to access "response.getReturnValue();" outside of setcallback, How to do that ?
Best Answer chosen by Amrut
GauravendraGauravendra
Hi Cena,

The reason behind getting null value is LIghtning is event driven software architecture. 
See, you are assigning the component value inside the setcallback method.
And accessing the value outside it. Thats fine. But the action setCallback method is called based on enqueueAction. 
The request is placed in queue. So whats happening is, you are accessing the value first and assigning it latter. Thats why you are getting value as null.
If you place some console log, you will find that console log placed outside the setcallback function are running first and console log inside are running after that.

The solution is to access the value after some time. You can access the component attribute in a helper method. And call that helper method inside the setcallback method. 
action.setCallback(this,function(response){
              var state = response.getState();
            if (state === "SUCCESS") {
                var res = response.getReturnValue();
                component.set("v.acclist",res);
                helper.secondfun(component,event,helper);
            }
                
        });
        $A.enqueueAction(action);
And inside the helper if you try to access the component attribute. It is easily available as it is getting called after the attribute being set.
 
secondfun : function(component, event, helper) {
        var outfun = component.get("v.acclist");
        console.log("acclist second fun## ",outfun);
    }

Hope it helps!
 

All Answers

Raj VakatiRaj Vakati
You need to use the promise in that case .. Please refer that link 

https://salesforce.stackexchange.com/questions/171073/lightning-multiple-enqueued-actions-execute-action-when-all-are-done
{!Piyush_soni__c}{!Piyush_soni__c}
as you set the Customer attribute value with response.getReturnValue() value so you can access that value from attribute outside from callback with :
var outSideCallBack = component.get("v.Customer");
console.log(outSideCallBack);
Hope it will helps you

 
Narender Singh(Nads)Narender Singh(Nads)
Hi,

What you can do is declare a variable outside callback scope and then assign the value to it in the call back. That way you can access that response value anywhere.

Like this:
var responseValue;
var action = component.get("c.fetchData");
   action.setCallback(this, function(response) {
                        var state = response.getState();
                        console.log("State " + state);
                        if (state === "SUCCESS") {
                            var storeResponse = response.getReturnValue();
                            responseValue=storeResponse;
                            component.set("v.Customer", storeResponse);
                            var Credit = storeResponse.field__c;
                            console.log("Credit  "+Credit);
                        } });
                    $A.enqueueAction(action);
                }

Let me know if that helps.
Thanks
AmrutAmrut
Hi Narender Singh, global declaration is also not working, only inside loop value comes.

@Piyush. value coming as null by your way
{!Piyush_soni__c}{!Piyush_soni__c}
hi Cena can you please elaborate your requrement more.

Thanks
GauravendraGauravendra
Hi Cena,

The reason behind getting null value is LIghtning is event driven software architecture. 
See, you are assigning the component value inside the setcallback method.
And accessing the value outside it. Thats fine. But the action setCallback method is called based on enqueueAction. 
The request is placed in queue. So whats happening is, you are accessing the value first and assigning it latter. Thats why you are getting value as null.
If you place some console log, you will find that console log placed outside the setcallback function are running first and console log inside are running after that.

The solution is to access the value after some time. You can access the component attribute in a helper method. And call that helper method inside the setcallback method. 
action.setCallback(this,function(response){
              var state = response.getState();
            if (state === "SUCCESS") {
                var res = response.getReturnValue();
                component.set("v.acclist",res);
                helper.secondfun(component,event,helper);
            }
                
        });
        $A.enqueueAction(action);
And inside the helper if you try to access the component attribute. It is easily available as it is getting called after the attribute being set.
 
secondfun : function(component, event, helper) {
        var outfun = component.get("v.acclist");
        console.log("acclist second fun## ",outfun);
    }

Hope it helps!
 
This was selected as the best answer
Raj VakatiRaj Vakati
Try some thing like below 
 
public with sharing class TaskAlertController {
    
    @AuraEnabled
    public static List<Task> getTasks(){
        Date thisDate = date.today();
        String userID = UserInfo.getUserId();
        return [SELECT WhoId,ActivityDate FROM Task];
    }
}
 
({
    doInit : function(cmp, event, helper) {
        var action = cmp.get("c.getTasks");
        
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                helper.calloutside(cmp , response.getReturnValue()); 
            }
        });
        $A.enqueueAction(action);
    },
   
    
})

 
({
    helperMethod : function() {
        
    },
    calloutside: function(cmp , data){
        console.log(data);
        
    }
})
AmrutAmrut
@Gauravendra @Raj V So its just a call to another helper method, how is it different than writing the logic in the same setcallback method rather than transfering it to another helper method. Helper certailnly helps in reusability thats a different story though.

Thanks for the responses.