g_form.getOption() doesn't work in Workspace - need an alterative

Marc D
Tera Contributor

Hello everyone. My current project has a requirement - when Category is set to a certain value, it adds an extra option to the Subcategory choices. To do this, I created a UI Policy with scripts like these:

Execute if true:

function onCondition() {
    if (!g_form.getOption('subcategory', 9)) {
        g_form.addOption('subcategory', 9, '<insert some name here>');
    }
}

Execute if false:

function onCondition() {
    g_form.removeOption('subcategory', 9);
}

This worked fine at first, but we later discovered that it didn't work when testing in the Workspace. After looking it up, I discovered that g_form.getOption() doesn't work in Workspace. Are there any alternatives I can use? I was using getOption() specifically to check to see if the extra Subcategory choice already existed.

7 REPLIES 7

Hello, subhamdubey.

I did see that article earlier, but it does not seem like it would work for my use case. The algorithm I am trying to use is:
Does option <x> already exist?
If yes, do nothing

If no, add option <x>

The getDisplayValue() function appears to only get the currently selected option - it needs to be able to check on existing options without selecting one. If that's NOT how getDisplayValue works, please explain what it would do in this situation.

Amit Verma
Kilo Patron
Kilo Patron

Hi @Marc D 

 

Is your subcategory field dependent on category field ? If yes, then you can't add options to subcategory field. Can you please confirm on this to debug it further ?


Refer https://www.servicenow.com/community/now-platform-forum/removeoption-and-addoption-not-working/m-p/1...

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Hello, Amit.
In this specific implementation, the Subcategory field is NOT dependent on the Category field. The addOption and removeOption functions work fine - it's the getOption function that doesn't work properly. I COULD remove the checking for duplicates, but then I risk having duplicates.