2

I have a simple Aura comp. where I am trying to open custom object record creation page. Here, I am trying to pre-populate some of the fields on this creation form as I can access parent Case id from Pagereference as below:

CMP.js

var hotelId = helper.getHotelIdValue(component, caseId);
//other fields directly from Pagereference.
.
.
workspaceAPI.getEnclosingTabId().then(function(parentTabId) {
    //OpenSubtab with defaultFieldValues : component.find("pageRefUtils").encodeDefaultFieldValues(prepopulationValues)
}).catch(function(error) {
        console.log("error "+error);
});

HELPER.js

getHotelIdValue: function(component, caseId){
    var action = component.get("c.getHotelIdMethod");
    var hotelId;
    action.setParams({ caseId : caseId });
    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            console.log('Return value: ' +response.getReturnValue());
            hotelId= response.getReturnValue();
        } else if (state === "ERROR") {
            var errors = response.getError();
            if (errors) {
                console.log(errors[0].message);
            } else {
                console.log("Unknown error");
            }
        }
    });
    $A.enqueueAction(action);
}

The problem here is - $A.enqueueAction(action); will place the server-side callback in the queue while my doInit gets executed on load. Due to this, my variable with CMP.js file var hotelId is getting as undefined as there won't be any value on initialization. I know the issue as stated in this SF documentation

$A.enqueueAction(action) adds the server-side controller action to the queue of actions to be executed. All actions that are enqueued will run at the end of the event loop. Rather than sending a separate request for each individual action, the framework processes the event chain and batches the actions in the queue into one request. The actions are asynchronous and have callbacks.

What is the workaround? How can I get the value of any field within my doInit function to pass it along to my subtab?

1 Answer 1

4

Convert your helper method to a Promise:

getHotelIdValue: function(component, caseId){
    return new Promise((resolve, reject) => {
        var action = component.get("c.getHotelIdMethod");
        var hotelId;
        action.setParams({ caseId : caseId });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                console.log('Return value: ' +response.getReturnValue());
                hotelId = response.getReturnValue();
                resolve(hotelId);
            } else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    console.log(errors[0].message);
                } else {
                    console.log("Unknown error");
                }
                reject(errors);
            }
        });
        $A.enqueueAction(action);
    })
}

Then use that to perform more async work, something like this:

helper
  .getHotelIdValue(component, caseId)
  .then((hotelId) => {
    workspaceAPI.getEnclosingTabId().then(function (parentTabId) {
      //OpenSubtab with defaultFieldValues : component.find("pageRefUtils").encodeDefaultFieldValues(prepopulationValues)
    });
  })
  .catch(function (error) {
    console.log("error " + error);
  });
2
  • Thank you @sfdcfox. I definitely need to learn the concepts around "Promise". I will try the above approach and share my feedback shortly. :) Nov 22, 2021 at 21:38
  • I am getting confused by myself with variable names. As you could see from my post, I have: var hotelId = helper.getHotelIdValue(component, caseId); The async part you stated above should replace var hotelId? Or, can I still keep it and have another async call as: helper .getHotelIdValue(component, caseId) .then((hotelId) =>? If I rename the variable var hotelId; within helper.js, how I should make async work within js with new variable? Please assist. Nov 24, 2021 at 19:20

You must log in to answer this question.

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