0

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.

CMP:

<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>
</aura:component>

Controller.js:

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

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

Helper.js:

fetchQTCount : function(component, callback) {
            console.log('entered fetchQTCount');
            var opId = component.get("v.recordId");
            var action = component.get("c.getCPQQuoteCount");
            action.setParams({
                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());
                    component.set("v.cpqQuoteCount",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");
                }
            });
            $A.enqueueAction(action);
        }

Apex Controller:

@AuraEnabled(cacheable=false)
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 Feb 27 at 13:58
1

Better to use Javascript promissing .

Suspect here is :In doInit method only you are calling

     helper.fetchQTCount(component);

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:

        helper.fetchQTCount(component);

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 – codeyinthecloud Feb 27 at 8:34

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.