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.


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);
            console.log('subTabIds after subtab push: ' + subTabIds);
            primaryIdProcessed += 1;

        if (primaryIdProcessed === primaryIdCount) {
            allTabIds = returnedIds.push(subTabIds);
            console.log('allTabIds: ' + allTabIds);




1 Answer 1


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.

let completedCount = 0;
let totalCount = 10;
for (let i = 0; i < totalCount; i++) {
    sforce.console.doSomeStuff(parameter, function () {
        completedCount += 1;
        if (completedCount === totalCount) {
            // now you are "done with the for loop"

You may alternatively wish to play around with window.setTimeout.

Side note on your JS, please use const instead of var, except in cases where you assign a value more than once. In the latter case, use let. Never use undeclared variables (something you do in your for loop).


You must log in to answer this question.

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