0

I have an Id variable undefined in javascript aura - not getting set in time before rest of code runs. I am needing the variable v.workOrderId to be populated before other helper code runs. I'm calling an apex controller for an SOQL query in order to get the workOrderId, however it's not getting the data back from the server-side call before the rest of the code runs. I need to get the workOrderId from the page and then load the rest of the page with the rest of the data. However it's not working.

How can I get the v.workOrderId to populate from my helper.getWOIdNow before my var woId = component.get("v.workOrderId"); runs?

controller.js

    doInit : function(component, event, helper) {
        debugger;
        
        console.log('component.get(recordId)==== '+component.get("v.recordId"));

        // Get WorkOrderId
        helper.getWOIdNow(component, event, helper);

        // Do Init
        //var woId = component.get("v.pageReference").state.c__woId;
        var woId = component.get("v.workOrderId");
        if(woId != undefined && woId != null && woId != ''){
            component.set("v.showSpinner",true);
            //component.set("v.workOrderId",woId);
            helper.getAllFaultCodes(component, event, helper);
            helper.getAllComponentCodes(component, event, helper);
            helper.getAllAvoidableDamages(component, event, helper);
            helper.getRepairTypes(component, event, helper);
            helper.getDataOnLoad(component, event, helper);
        }else{
            component.set("v.showSpinner",false);
            alert('woId is undefined, null or blank');
        }
    },

helper.js

getWOIdNow : function(component, event, helper){
        component.set("v.showSpinner",true);
        var action = component.get("c.getWOIdApex");
        action.setParams({ tranId : component.get("v.recordId") });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var result = response.getReturnValue();
                component.set("v.workOrderId",result);
                console.log('workOrderId: ' + component.get("v.workOrderId"));
            }else if (state === "INCOMPLETE") {
                // do something
            }else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
        });
        $A.enqueueAction(action);        
    },

    getDataOnLoad : function(component, event, helper) {
        component.set("v.showSpinner",true);

        var action = component.get("c.getWorkOrderDetails");
        action.setParams({ woId : component.get("v.workOrderId") });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var result = response.getReturnValue();
                var woData = JSON.parse(result);
                component.set("v.responseData", woData);
                if(woData.woNumber != undefined && woData.woNumber != null){
                    component.set("v.woNumber",woData.woNumber);
                }
                
                if(woData.dispatchNotes != undefined && woData.dispatchNotes != null){
                    component.set("v.dispatchNotes",woData.dispatchNotes);
                }
                
                if(woData.meterReading != undefined && woData.meterReading != null){
                    component.set("v.meterReading",woData.meterReading);
                }
                
                if(woData.truckSN != undefined && woData.truckSN != null){
                    component.set("v.truckSN",woData.truckSN);
                }
                
                if(woData.failureDate != undefined && woData.failureDate != null){
                    component.set("v.failureDate",woData.failureDate);
                }
                
                if(woData.segmentIdSegmentDataMap != undefined && woData.segmentIdSegmentDataMap != null){
                    var segmentsMap = [];
                    for(var key in woData.segmentIdSegmentDataMap){
                        segmentsMap.push({value:woData.segmentIdSegmentDataMap[key], key:key});
                    }
                    component.set("v.segmentDataMap",segmentsMap);
                }
                component.set("v.showSpinner",false);
                
            }else if (state === "INCOMPLETE") {
                // do something
            }else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
                component.set("v.showSpinner",false);
            }
        });
        $A.enqueueAction(action);
    },

controller.cls

    @AuraEnabled
    public static String getWOIdApex(String tranId){
        List<SMAX_PS_Transaction_Document__c> woID = new List<SMAX_PS_Transaction_Document__c>([SELECT SMAX_PS_Work_Order__c FROM SMAX_PS_Transaction_Document__c WHERE Id = :tranId LIMIT 1]);

        return string.valueOf(woID[0].SMAX_PS_Work_Order__c);
    }
<aura:component implements="force:appHostable,lightning:isUrlAddressable,forceCommunity:availableForAllPageTypes,flexipage:availableForAllPageTypes,force:hasRecordId, flexipage:availableForRecordHome" controller="SVMX_PS_WorkOrderRepairSegmentsCtrl" access="global">
    <aura:attribute name="options" type="List" default="[
                                                        {'label': 'Show All', 'value': 'showAll'},
                                                        {'label': 'Involved Parts', 'value': 'involvedParts'},
                                                        {'label': 'Labor', 'value': 'labor'},
                                                        {'label': 'Additional Info:', 'value': 'additionalInfo'}
                                                        ]"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="selectedTab" type="String"></aura:attribute>
    <aura:attribute name="showParts" type="Boolean" default="true"></aura:attribute>
    <aura:attribute name="showLabor" type="Boolean" default="true"></aura:attribute>
    <aura:attribute name="showAdditionalInfo" type="Boolean" default="true"></aura:attribute>
    <aura:attribute name="workOrderId" type="String"></aura:attribute>
    <aura:attribute name="woNumber" type="String"></aura:attribute>
    <aura:attribute name="dispatchNotes" type="String"></aura:attribute>
    <aura:attribute name="meterReading" type="Decimal"></aura:attribute>
    <aura:attribute name="truckSN" type="String"></aura:attribute>
    <aura:attribute name="failureDate" type="String"></aura:attribute>
    <aura:attribute name="segmentDataMap" type="List"></aura:attribute>
    <aura:attribute name="responseData" type="Object"></aura:attribute>
    <aura:attribute name="segmentsMap" type="Map"></aura:attribute>
    <aura:attribute name="repairTypes" type="List"></aura:attribute>
    <aura:attribute name="avoidableDamages" type="List"></aura:attribute>
    <aura:attribute name="componentCodes" type="List"></aura:attribute>
    <aura:attribute name="faultCodes" type="List"></aura:attribute>
    <aura:attribute name="showAddSegment" type="Boolean" default="false"></aura:attribute>
    <aura:attribute name="showEditSegment" type="Boolean" default="false"></aura:attribute>
    <aura:attribute name="showAddPart" type="Boolean" default="false"></aura:attribute>
    <aura:attribute name="showAddLabor" type="Boolean" default="false"></aura:attribute>
    <aura:attribute name="selectedSegmentId" type="String"></aura:attribute>
    <aura:attribute name="segmentId" type="String"></aura:attribute>
    <aura:attribute name="id" type="String"></aura:attribute>
    <aura:attribute name="IsPrimary" type="String"></aura:attribute>
    <aura:handler name="deleteSegmentEvent" event="c:SVMX_PS_SendStatus" action="{!c.handleDeleteSegmentStatus}"/>
    <aura:handler name="editSegmentEvent" event="c:SVMX_PS_SendStatus" action="{!c.handleEditSegmentStatus}"/>
    <aura:handler name="addSegmentEvent" event="c:SVMX_PS_SendStatus" action="{!c.handleSegmentStatus}"/>
    <aura:handler name="addPartEvent" event="c:SVMX_PS_SendStatus" action="{!c.handlePartStatus}"/>
    <aura:handler name="addLaborEvent" event="c:SVMX_PS_SendStatus" action="{!c.handleLaborStatus}"/>
    <aura:attribute name="showSpinner" type="Boolean" default="false"></aura:attribute>
1

In your init function your helper method helper.getWOIdNow runs asynchronously and before it gets a response it moves on to the next line.. so your best bet is to take all the lines in your init method after the helper function and add them in helper.getWOIdNow once you have the record ID, or break it down into a separate function and call it after you receive the response in the helper method

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.