0

I'm getting 'credCycleId is not defined' error at the run time execution of my lightning component.

when i'm trying to assign the value of my attribute 'parentcredCycleId' to 'credCycleId' parameter in controller, it's throwing an error.

i have verified below things from my end:-

  1. var hh1 = component.get("v.parentcredCycleId"); //this is showing NULL

    alert('---hh1'+hh);

  2. alert('--resp'+response.getReturnValue()); //this is showing correct value.

  3. var hh = component.get("v.parentcredCycleId"); // this is again showing null

    alert('---hh'+hh);

Below is my helper method for your reference. Please help.

({
    getQuickLinkWrapperList : function(component, event, helper) {
        var action = component.get("c.getCredCycleId");     
        var sObjectRecordId= component.get("v.recordId");
        var sObjectAPIName= component.get("v.sObjectName");     
        
        action.setParams({
            childRecordId: sObjectRecordId,
            childAPIName : sObjectAPIName
        });        
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.parentcredCycleId",response.getReturnValue());
                var hh1 = component.get("v.parentcredCycleId");
                alert('---hh1'+hh);
                alert('--resp'+response.getReturnValue());
                this.handleSuccess(component, response.getReturnValue());
            } else {
                this.handleError(component, response.getError());
            }
        });        
        
        var hh = component.get("v.parentcredCycleId");
        alert('---hh'+hh);
        
        var action1 = component.get("c.getQuickLinkWrapperList");       
        action1.setParams({
            credCycleId: component.get("v.parentcredCycleId")
        }); 
        action1.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                this.handleSuccess(component, response.getReturnValue());
            } else {
                this.handleError(component, response.getError());
            }
        });
        
        $A.enqueueAction(action);
        $A.enqueueAction(action1);
    }
})

1 Answer 1

1

The call to the apex is not synchronous. That is why below code runs before the callBack function of the action. That is the reason it's null.

var hh = component.get("v.parentcredCycleId");
alert('---hh'+hh);

You need to call the action1 inside the callBack function of the first action.

The below code should work just fine.

({
    getQuickLinkWrapperList : function(component, event, helper) {
        var action = component.get("c.getCredCycleId");     
        var sObjectRecordId= component.get("v.recordId");
        var sObjectAPIName= component.get("v.sObjectName");     

        action.setParams({
            childRecordId: sObjectRecordId,
            childAPIName : sObjectAPIName
        });        
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.parentcredCycleId",response.getReturnValue());

                // call the second action starts.
                var action1 = component.get("c.getQuickLinkWrapperList");       
                action1.setParams({
                    credCycleId: component.get("v.parentcredCycleId")
                }); 
                action1.setCallback(this, function(response) {
                    var state = response.getState();
                    if (state === "SUCCESS") {
                        this.handleSuccess(component, response.getReturnValue());
                    } else {
                        this.handleError(component, response.getError());
                    }
                });
                $A.enqueueAction(action1);
                // call the second action end.

                var hh1 = component.get("v.parentcredCycleId");
                /// hh is not define ===> alert('---hh1'+hh);
                alert('--resp'+response.getReturnValue());
                this.handleSuccess(component, response.getReturnValue());
            } else {
                this.handleError(component, response.getError());
            }
        });
        $A.enqueueAction(action);
    }
})

Also if possible you can combine the both apex methods into one and you won't need to make two subsequent calls to apex.

3
  • 1
    thanks a ton.. it is working like a butter now.:) Commented Jun 7, 2020 at 10:34
  • thanks a ton.. it is working like a butter now.:) Commented Jun 7, 2020 at 10:34
  • you are welcome Commented Jun 7, 2020 at 11:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .