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

RaghavSh
Mega Patron

Is the UI type of client script set to All?

 

Also is the plan field added to workspace view?


Raghav
MVP 2023
LinkedIn

Yes, UI type is All.

Also is the plan field added to workspace view? - not sure, here 'Plan' is a option in stage field. 

Is the stage field on workspace?

I dont see anything in this script which stops it from working in workspace view, can you inspect the page and see the error in console?

 


Raghav
MVP 2023
LinkedIn

SufiyanMurshad
Tera Contributor

 

Hi @Sivaprasath,

The script you wrote is correct — it works fine in the normal platform view. The reason it doesn’t show the 'plan' stage in Workspace when the case is first created isn’t because of your logic, but because of how Workspace loads forms differently.

In Workspace, the form fields (like the stage field) load a bit slower. So your script runs before the field is fully ready. That’s why the 'plan' option doesn’t appear right after creation, but works fine once the status changes (since the field is loaded by then).

To fix it, we need delay the function so it runs after the form is finished loading in Workspace

for ex..

if (window.NOW && NOW.workspace) {
setTimeout(filterStages, 500); // wait a bit for Workspace to load the field
} else {
filterStages();
}

 

This makes sure the logic runs at the right time in both Form and Workspace.

Also, when a record is new, you can set the default stage directly with

if (g_form.isNewRecord()) {
g_form.setValue('stage', 'Plan');
}
That's way, 'plan' will always show as the first value.

 

Please marked my answer is correct or helpful.