I have a component that is using aura:iteration to loop through some data and output a table of results. One column in this table is a link to the record. I created a clickevent handler and a helper to generate this navigation link for me.
Component:
<lightning:navigation aura:id="navService"/>
<a data-id="{!v.rec.CreatedById__r.Id}" onclick="{!c.handleRecordClick}">Link</a>
Controller:
({
handleRecordClick: function(component, event, helper) {
var currentRecordId = event.currentTarget.dataset.id;
var url = helper.createRecordUrl(
component,
currentRecordId,
"EntUser__x",
"view"
);
console.log(url); // undefined
}
});
Helper:
({
createRecordUrl: function(component, clickedRecordId, objectApiName, action) {
var navService = component.find("navService");
var pageReference = {
type: "standard__recordPage",
attributes: {
recordId: clickedRecordId,
objectApiName: objectApiName,
actionName: action
}
};
// Set the URL on the link or use the default if there's an error
var defaultUrl = "#";
navService.generateUrl(pageReference).then(
$A.getCallback(function(url) {
console.log(url); // /r/EntUser__x/x000x00000BSaH7AAL/view
return url ? url : defaultUrl;
}),
$A.getCallback(function(error) {
return defaultUrl;
})
);
}
});
My controller is getting undefined when I try and return the generated URL. If I try and console.log() within the getCallback in the helper, the URl is generated fine.
I am thinking this could be an issue with timing where my controller is requesting the URL before the helper callback has completed?
How should I approach the controller to wait for this returned value and prevent this issue?