We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

g_form.removeOption() is not working in multirow variable set catalog client script.

GopiKrishna Sir
Tera Contributor

We have a catalog client script on MRVS to hide drop choice based on users cost center. But choice is not getting removed even after adding a catalig client script.

Sample code : 

var allowedCC = 'a6754a8b97438a505f88b3a3f153afe5';
    var ga = new GlideAjax('GetUserCostCenter');
    ga.addParam('sysparm_name', 'getCostCenter');
    ga.addParam('sysparm_user', g_user.userID);
    alert(g_user.userID);
    ga.getXMLAnswer(function (cc) {
        alert(cc);
        // Optional: handle empty values or server error
        if (!cc) {
            console.warn('No cost center returned');
            return;
        }
       
        // Only run if user does NOT have allowed cost center
        if (cc !== allowedCC) {
            // Remove the specific option by label
            g_form.removeOptionByLabel('actor_type', 'Central F&A Actor Packages');
        }
    });
 
Did anyone faced similar issue & found work around, Please let us know to solve it.
Thanks
Gopi
6 REPLIES 6

Ankur Bawiskar
Tera Patron

@GopiKrishna Sir 

you are using wrong method removeOptionByLabel

correct one is this

g_form.removeOption('actor_type', 'choice value'); // give your choice value

💡 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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

GopiKrishna Sir
Tera Contributor

Ankur,

I have tried g_form.removeOption('actor_type', 'choice value'); // give your choice value, didn't worked. 

Thanks

Gopi

Hello @GopiKrishna Sir ,

 

 

Can you please check this method :

g_sc_form.removeOption('test_var', 'choice_one');

 

 

You can refer this : https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0678206

 

If above link not works then its content is already mentioned below please check this :

Note that the catalog client script has no issues with this method when running on com.glideapp.servicecatalog_cat_item_view.do.

Steps to Reproduce
Go to the sc_req_item table and go to the Blackberry catalog item.

Add a new variable select box named "test_var".

For more information, see the product documentation topic Create a Service Catalog variable.

Add three choices to the variable named "choice_one", "choice_two", and "choice_three".

Create a new client script with the following values:

Type: onLoad
Table: sc_req_item
Script:
function onLoad() {
g_form.removeOption('variables.test_var','choice_one');
}
Submit a new Blackberry catalog.

Go to the request item form.

The test_var variable should contain only two choices but three choices are available including choice_one, which should have been removed.

Workaround: g_sc_form.removeOption('test_var', 'choice_one');

 

Workaround
Note that only the following APIs are supported:

g_form.setDisplay(name, display)
g_form.setVisible(name, visibility)
g_form.setMandatory(name, mandatory)
g_form.setValue(name, value, display_value)
g_form.getValue(name)
g_form.setReadOnly(fieldName, boolean)
g_form.removeOption is not supported.

 

If this helps you then mark it as helpful and accept as solution.

Regards,

Aditya,

Technical Consultant

Hello @GopiKrishna Sir ,  

 

I hope you are doing well . Does my response helps you ? 

 

If my response helps you  then you cam mark it as helpful and accept as solution. This will helps further future queries.

 

Regards,

Aditya,

Technical Consultant