I'm trying to make a logical decision based on count of child records of opportunity. To achieve this I have an apex method that returns a count of child records. In the js controller looking for the count and routing to the appropriate action.

The issue I'm having is the attribute type integer that I set in the helper.js is returning undefined in the controller.js despite having value in helper.


<aura:component controller="CSSQuoteRelatedListCtrl" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name="cpqQuoteCount" type="Integer" />                                                                                                                                        
<button class="slds-button slds-button_neutral" onclick="{!c.onNewQuote}" >New</button>


onNewQuote: function(component,event, helper) {
    //var oppId = component.get("v.recordId");
    var currentOppQuoteCount = component.get("v.cpqQuoteCount"); // its undefined here

    console.log('currentOppQuoteCount in controller '+currentOppQuoteCount);
  //  debugger;
    if (currentOppQuoteCount===0) {
    }else if(currentOppQuoteCount !== 0){
    helper.showToast(component,"Error!","Error","some error");


fetchQTCount : function(component, callback) {
            console.log('entered fetchQTCount');
            var opId = component.get("v.recordId");
            var action = component.get("c.getCPQQuoteCount");
                oppId : opId
            action.setCallback(this, function(response) {
                var state = response.getState();
                console.log('success from server'+state);
                if (state === "SUCCESS") {
                    console.log('success from server');
                    console.log("From fetchQTCount: " + response.getReturnValue());
                var currentOppQuoteCount = component.get("v.cpqQuoteCount");
                console.log("currentOppQuoteCount from helper: " + currentOppQuoteCount); // the values are returning correctly
                else if (state === "ERROR") {
                    console.log("Error occurred from server");

Apex Controller:

public static Integer getCPQQuoteCount(Id oppId) {
    Integer cpqQuoteCount=0 ;
    if(oppId != null){
        cpqQuoteCount = [SELECT count() FROM CPQ_Quote__c where Opportunity__c =: oppId];
    return cpqQuoteCount;

Question: What am I doing wrong, Am I missing something obivious?

NOTE: I trimmed up irrelevant code.

  • 1
    I think you already figured this out, but because the server call would have been async in nature and that when you were accessing the attribute in the controller, it may not have been set by that time in the helper. I would think if you put a timeout after the helper call, that may have given you the results.
    – Jayant Das
    Commented Feb 27, 2019 at 13:58

1 Answer 1


Better to use Javascript promissing .

Suspect here is :In doInit method only you are calling


In the same method you are trying to access cpqQuoteCount

  var currentOppQuoteCount = component.get("v.cpqQuoteCount");//Intial its undefined only so that value may showing here

My suggestion is better to write like this:


In fetchQTCount method, after getting success add your validation Logic:

Note: first component will load,then doInit method because of that reason you may getting 'undefined,' value

  • 1
    moving it to success of the previous method helped, but it has nothing to do with init as the method was called on click of a button! Thanks Commented Feb 27, 2019 at 8:34

You must log in to answer this question.

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