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

Ankur Bawiskar
Tera Patron
Tera Patron

@Sivaprasath 

any error when script runs in workspace?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

No @Ankur Bawiskar , when I opened the case form in workspace once created, that field is empty. then once i save the form with stage status is complete, then stage is showing options. 

Issue only happening at initial stage

@Sivaprasath 

why you want the onLoad to run on new form?

any client script is setting the field with value only in native and not in workspace?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

MaxMixali
Tera Guru

You’re seeing a classic Workspace vs Platform timing issue with choice lists.

 

In Platform UI, your onLoad runs after the choice list is ready, so addOption('stage', …) sticks.

In Workspace, the form finishes loading asynchronously and the dictionary choices are re-applied after your onLoad, overwriting what you added—so “Plan” doesn’t appear on first render. When the stage status changes later, your code runs again and then it works in both UIs.

 

Here’s how to make it reliable in Workspace (and Platform), plus a few cleanups.

 


 

 

What to change (summary)

 

 

  1. Make the script Workspace-safe: wait until the choice list is populated before calling clearOptions/addOption.

  2. Ensure your script’s UI type = All and Client Script type = Form onLoad.

  3. For new cases, set the default value for stage to "Plan" either in Dictionary or via g_form.isNewRecord() guard.

  4. Use choice values (not just labels) and pass the optional index to addOption to control order.

 

 


 

 

Workspace-safe onLoad (drop-in)

 

JS:

function onLoad() {
// For NEW records, ensure Stage shows "Plan" up-front
if (g_form.isNewRecord() && !g_form.getValue('stage')) {
g_form.setValue('stage', 'Plan'); // make sure "Plan" is an actual choice value
}

ensureChoicesReady('stage').then(function () {
filterStages();
});

// Also re-apply when stage_status changes while the form is open
g_form.onChange('stage_status', function() {
ensureChoicesReady('stage').then(filterStages);
});

function ensureChoicesReady(fieldName) {
// Workspace loads choices asynchronously: wait until the select has options
return new Promise(function(resolve) {
var tries = 0, maxTries = 40; // ~2s
var timer = setInterval(function () {
var ctrl = g_form.getControl(fieldName);
var ready = ctrl && ctrl.options && ctrl.options.length > 0;
if (ready || ++tries >= maxTries) {
clearInterval(timer);
resolve();
}
}, 50);
});
}

function filterStages() {
var stageField = 'stage';
var status = g_form.getValue('stage_status'); // e.g., 'Completed' or other values
var currentStage = g_form.getValue('stage'); // IMPORTANT: this is the **value**, not the label

// Use values that match your dictionary "Value" for the choice, not just the label
var stages = ['Plan', 'Build', 'Release', 'Proposals', 'Approval', 'Award'];

var currentIndex = stages.indexOf(currentStage);
if (currentIndex === -1) {
// If currentStage isn't in the list (e.g., new record), start from 'Plan'
currentStage = 'Plan';
currentIndex = stages.indexOf('Plan');
if (currentIndex === -1) return; // safety
g_form.setValue(stageField, 'Plan');
}

g_form.clearOptions(stageField);

// Helper to add a stage at a specific position (keeps order stable)
function addStageAt(idx) {
g_form.addOption(stageField, stages[idx], stages[idx], idx);
}

if (status != 'Completed') {
// Only allow up to the current stage
for (var i = 0; i <= currentIndex; i++) addStageAt(i);
// If you want to mirror in a second field, keep it—but ensure it exists
// g_form.setValue('u_rfx_stage', currentStage);
} else {
// Allow up to current + next stage
for (var j = 0; j <= currentIndex; j++) addStageAt(j);
if (currentIndex + 1 < stages.length) addStageAt(currentIndex + 1);
// g_form.setValue('u_rfx_stage', currentStage);
}

// Re-set the value to ensure selection is maintained after rebuilding options
g_form.setValue(stageField, currentStage);
}
}

 

 

 

 

Why this works

 

 

  • ensureChoicesReady() waits for Workspace to finish hydrating the select input before you manipulate it.

  • Re-applying the value after clearOptions/addOption prevents an empty display.

  • Binding onChange('stage_status', …) ensures the same logic runs when status changes (your second case).

 

 


 

 

Additional recommendations

 

 

  • Dictionary default: If the business rule is “Stage = Plan when a case is created”, set Default value = Plan on the stage dictionary entry. This is server-driven (most reliable across all UIs, imports, and APIs). Keep the client script only for limiting choices shown.

  • Use choice values: Make sure 'Plan', 'Build', etc. are exactly the value field for the choices (not just labels). If values differ, use those values in the array.

  • UI Type: Set the client script UI type = All (or specifically include Workspace on newer releases).

  • Avoid writing to other fields unless needed: You set u_rfx_stage; ensure that field exists and is intended. If it’s only for mirroring, consider removing to reduce noise.

 

 


 

 

If you want zero client-side timing

 

 

You can move the allowed options logic server-side:

 

  • Create a GlideAjax (client callable SI) that returns the allowed stages for the current record/state.

  • In the client script, just call GA to get the list, then build options once.

    This avoids depending on Workspace load timing and keeps rules in one place.

 

 

kaushal_snow
Giga Sage

@Sivaprasath ,

 

It looks like the issue is that while your on-load client script is working in the standard platform view, it doesn’t show the Plan option in the workspace because in the workspace context many of the classic g_form.addOption() and g_form.clearOptions() methods behave differently or are not fully supported for choice fields......for example g_form.addOption() often won’t work in a workspace view if the choice field is dependent or the script isn’t scoped for the workspace view........

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/