how Hide choice option which do not have dependent value mentioned

vaishnavi g
Tera Contributor

There are 3 field .Category and SubCategory1,subCategory2.

category is independent. but subcategory1 have 'Category 'as  dependent field and its choice have also assigned dependent value.

bur 'subcategory2' has 'SubCategory1 as dependent value but choices are not assigned to dependent value.

As I write onload client script and uses the removeOption() function. and it also functioning for 'category ' and 'subcategory1'.

it also functioning for 'subcategory2' but once change the category value it showing all the values which I remove when form is onloaded.

 

Remove option is working for choices of all categories and those have dependent values.

But it not working for choice which do not have assigned the dependent value. Does it possible?

1 ACCEPTED SOLUTION

Satishkumar B
Giga Sage
Giga Sage

Hi @vaishnavi g 

  • Explicitly Assigned Dependent Values: removeOption() will reliably remove options that are dynamically added based on explicitly assigned dependent values in ServiceNow configurations.

  • Handling Fields without Assigned Dependent Values: For fields like 'SubCategory2' where choices do not have explicitly assigned dependent values, removeOption() may not consistently remove options. This is because the function primarily targets options based on explicit dependencies.

  • Alternative Approaches: If removeOption() does not behave as expected for fields without assigned dependent values, consider alternative approaches such as resetting the entire choice list based on conditions or using server-side scripts for more complex logic.

    --------------------------------------------------------------------------------------------------------------------
    If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

 

 

View solution in original post

2 REPLIES 2

Satishkumar B
Giga Sage
Giga Sage

Hi @vaishnavi g 

  • Explicitly Assigned Dependent Values: removeOption() will reliably remove options that are dynamically added based on explicitly assigned dependent values in ServiceNow configurations.

  • Handling Fields without Assigned Dependent Values: For fields like 'SubCategory2' where choices do not have explicitly assigned dependent values, removeOption() may not consistently remove options. This is because the function primarily targets options based on explicit dependencies.

  • Alternative Approaches: If removeOption() does not behave as expected for fields without assigned dependent values, consider alternative approaches such as resetting the entire choice list based on conditions or using server-side scripts for more complex logic.

    --------------------------------------------------------------------------------------------------------------------
    If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

 

 

vaishnavi g
Tera Contributor

Hi @Satishkumar B 

 

Thank you for your response. 

 

I have written the client script as well server side script. Still facing the same issue. I 'm using onload client script.

when form is load  this will apply on both dependent and independent choice , but when I'm changing the category field value it showing only independent choices of subcategory2.

 

Below is server side script:

var uhriCatDetails = Class.create();
uhriCatDetails.prototype =Object.extendsObject(AbstractAjaxProcessor, {
    isPublic: function() {
        gs.info("In public function");
        return true;
       
    },
subCategory2:function(){
        var arr3 = [];
        var subCa2=this.getParameter('sysparm_subcategory2');
        var gr = new GlideRecord('sys_choice');
        gr.addQuery('element', subCa2);
        gr.addQuery('hint', 'UHRI Option');
        gr.query();
        while (gr.next()) {
            arr3.push(gr.value.toString());
        }
        gs.log("list of cat:" + arr3);
        return JSON.stringify(arr3);
       
    },
type: 'uhriCatDetails'
});
 
Below is client script:

function onLoad() {

    //Type appropriate comment here, and begin script below

    if (!g_user.hasRole('jgs_it_uhri')) {

var ajax2 = new GlideAjax('uhriCatDetails');

        ajax2.addParam('sysparm_name', 'subCategory2');//function name

        ajax2.addParam('sysparm_subcategory2', 'u_subcategory_5');//parameter

        ajax2.getXML(ajaxResponse2);

        function ajaxResponse2(serverResponse) {

            var answer1 = JSON.parse(serverResponse.responseXML.documentElement.getAttribute("answer"));

            var i = 0;

            //alert('insideajaxResponse2 function');

            for (i = 0; i < answer1.length; i++) {

                alert("array value for subcat 2 : " + answer1[i]);

                g_form.removeOption('u_subcategory_5', answer1[i]);

            }

        }

}

 

Could you please advice me what is wrong in this code?

after using the server side script still facing the issue for removing independent choices ? is the drawback of snow? or missing the something in above code? could you please advice?