I am passing primary tab ids iteratively into a function to get the sub tub ids of each primary tab.
At the end of this function I have a list of all primary and sub tabs open. How can I iteratively pass these tab ids to the console page view function?
I would think pageInfoProcess() would invoke after the for loop has completed, but it does not which results in a undefined error when looking for the length. I looked in javascript's .then method, but I ran into the same issue as length with the variable being undefined.
<script>
var returnedIds;
var subTabIds;
function getPrimaryTabIdsFromConsole() {
sforce.console.getPrimaryTabIds(function(result) {
returnedIds = result.ids;
for (let k = 0; k < returnedIds.length; k++) {
sforce.console.getSubtabIds(returnedIds[k], function (subTabResults) {
let primaryIdCount = returnedIds.length;
let primaryIdProcessed = 0;
console.log('Subtab Ids Returned: ' + subTabResults.ids);
subTabIds.push(subTabResults.ids);
console.log('subTabIds after subtab push: ' + subTabIds);
primaryIdProcessed += 1;
if (primaryIdProcessed === primaryIdCount) {
allTabIds = returnedIds.push(subTabIds);
console.log('allTabIds: ' + allTabIds);
}
});
}
});
}
</script>
Best Answer
All console methods are asynchronous, so they have only been enqueued when the
for
loop completes. If you want it to execute after all of the calls have completed, you should put the logic in a callback. You will also need to implement some sort of counter to check how many tasks have completed.You may alternatively wish to play around with
window.setTimeout
.Side note on your JS, please use
const
instead ofvar
, except in cases where you assign a value more than once. In the latter case, uselet
. Never use undeclared variables (something you do in yourfor
loop).