Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Drop down value is not showing in workspace

Sivaprasath
Tera Contributor

Hi All,

 

I have created on load client script to add option in the field based on some validation.

 

Once the case created, stage field should display with Plan and when the case saved on stage status moved to complete, then adding next option in stage field. Here, once the case created the stage field displaying 'Plan' in platform view but not in workspace view and moving with next stage status, stage get updated with expected options in both view. 

 

Please refer the attachment and script. 

Your help will be appreciated.

 

function onLoad() {

    filterStages();

 

    function filterStages() {

        var stagefield = 'stage';

        var status = g_form.getValue('stage_status');

        var currentstage = g_form.getValue('stage');

 

        var stages = ['Plan', 'Build', 'Release', 'Proposals', 'Approval', 'Award'];

 

        var currentindex = stages.indexOf(currentstage);

        if (currentindex === -1) return;

 

        g_form.clearOptions(stagefield);

 

        if (status != 'Completed') {

            if (currentindex > 0) {

            for(var i = 0; i<= currentindex; i++){

                g_form.addOption(stagefield, stages[i], stages[i]);

            }

            }

            g_form.setValue('u_rfx_stage', currentstage);

        } else {

            if (currentindex > 0) {

                for(i = 0; i<= currentindex; i++){

                g_form.addOption(stagefield, stages[i], stages[i]);

                }

                g_form.addOption(stagefield, stages[currentindex + 1], (stages[currentindex + 1]));

            }

            else{

                g_form.addOption(stagefield, stages[currentindex], (stages[currentindex]));

                g_form.addOption(stagefield, stages[currentindex + 1], (stages[currentindex + 1]));

            }

            g_form.setValue('u_rfx_stage', currentstage);

        }

    }

}

Sivaprasath_1-1761129704528.png

 

Sivaprasath_0-1761129669682.png

 

@Ankur Bawiskar , Could you please help me on this.

 

Thanks

10 REPLIES 10

MaxMixali
Tera Guru

ServiceNow – Choice Field Options Differ in Workspace vs Platform (onLoad script)

Issue
- You add/remove options of a choice field (stage) in an onLoad client script.
- In Platform view it works.
- In Workspace (Now Experience), on a new Case the field does not show 'Plan' initially, but later transitions work.

Why It Happens
1) Workspace loads form data and choice lists asynchronously; your onLoad code can run before the choice list is ready.
2) Some legacy g_form methods behave slightly differently in Workspace. addOption/clearOptions work, but you must run them after options are available.
3) Initial value should not rely on client script; it’s best set server-side so both UIs are consistent.

Recommended Fix (Priority Order)
A) Server-side defaults and progression (most reliable)
- Set the default value of 'stage' to 'Plan' in the Dictionary (or a Before Insert Business Rule).
- Drive progression (allowing next option) server-side when 'stage_status' moves to 'Completed' (Business Rule or Flow).
- Client scripts should only adjust UI, not core data/choices.

B) If you must manage options client-side, use an async “choices ready” guard in Workspace
- Wrap your add/remove option logic so it runs after the choice list is present.

C) Ensure the Client Script UI Type includes Workspace
- Set UI Type = All (or Mobile / Service Portal). Workspace executes scripts marked for SP/Mobile/All.

Robust Client Script (works in Platform + Workspace)
Use a small wait-until-ready helper and guard array bounds.

function onLoad() {
// Ensure default for new records (UI help only; set server-side too)
if (g_form.isNewRecord() && !g_form.getValue('stage')) {
g_form.setValue('stage', 'Plan');
}
// Defer until Workspace has loaded choices
waitForChoices('stage', 20, 150, function() {
filterStages();
});
}

function waitForChoices(field, maxTries, delayMs, cb) {
var tries = 0;
(function poll() {
// Heuristic: check if we can get/set and at least one option exists by toggling add/remove temporarily
// Safer: probe with getOption for a known value; if null, choices may still be loading.
var test = g_form.getOption ? g_form.getOption(field, g_form.getValue(field)) : null;
if (test || tries >= maxTries) return cb();
tries++;
setTimeout(poll, delayMs);
})();
}

function filterStages() {
var stagefield = 'stage';
var status = g_form.getValue('stage_status');
var currentstage = g_form.getValue('stage');

var stages = ['Plan', 'Build', 'Release', 'Proposals', 'Approval', 'Award'];
var currentindex = stages.indexOf(currentstage);

// If not found, default to first stage
if (currentindex === -1) currentindex = 0;

// Clear then repopulate allowed options
try {
g_form.clearOptions(stagefield);
} catch (e) {
// Some Workspace builds require removing by value; fallback
for (var i = 0; i < stages.length; i++) {
if (g_form.getOption(stagefield, stages[i])) g_form.removeOption(stagefield, stages[i]);
}
}

if (status !== 'Completed') {
// Allow up to current
for (var a = 0; a <= currentindex; a++) {
g_form.addOption(stagefield, stages[a], stages[a]);
}
g_form.setValue(stagefield, stages[currentindex] || 'Plan');
} else {
// Allow up to next
for (var b = 0; b <= currentindex; b++) {
g_form.addOption(stagefield, stages[b], stages[b]);
}
var next = stages[currentindex + 1];
if (next) g_form.addOption(stagefield, next, next);
// Keep current selection unless you want to auto-advance
g_form.setValue(stagefield, stages[currentindex] || 'Plan');
}

// Mirror field, if needed
g_form.setValue('u_rfx_stage', g_form.getValue(stagefield));
}

Server-side (recommended) snippets

1) Default 'stage' to 'Plan' for new records (Before Insert BR)
(function executeRule(current, previous) {
if (!current.stage) current.stage = 'Plan';
})(current, previous);

2) On status complete, allow/advance stage (Before Update BR)
(function executeRule(current, previous) {
if (previous.stage_status != 'Completed' && current.stage_status == 'Completed') {
var order = ['Plan','Build','Release','Proposals','Approval','Award'];
var idx = Math.max(0, order.indexOf(previous.stage + ''));
var next = order[idx + 1];
if (next) current.stage = next; // or just allow next on UI, keep current
}
})(current, previous);

Other Tips
- Validate the choice values in Dictionary exactly match strings used in scripts.
- Avoid relying on client-only logic for business progression; prefer Flow/BR so reports and APIs remain consistent.
- In Workspace, prefer “UI Type = All” for client scripts that must run there.
- If you render the form via UI Builder, ensure the Form component has Legacy Client Scripts enabled (Compatibility Mode).

TL;DR
- In Workspace, your onLoad ran before choice options were ready. Add a small “wait for choices” guard or move defaults/progression to server-side (best). Set default stage to 'Plan' via Dictionary/BR to guarantee both Platform and Workspace show it consistently.