7 Replies · Latest reply on Jan 4, 2018 10:05 AM by nrgodale

    Allowing Categories and Subcategories to be available for specific Companies

      What is the procedure to allow certain Categories and Subcategories (during Incident creation) to be available for specific Companies?

        • Re: Allowing Categories and Subcategories to be available for specific Companies
          Shishir Srivastava

          Can we try to add one more column in choice table with company name and when user selects particular company we can have an onChange() client script to make a GlideAjax call to add the corresponding choice lists in Categories (if Categories and Subcategories are dropdown field). I think Subcategories will be auto-populated based upon the Categories.

          Thank you!

          Regards,
          Shishir Srivastava

          PS: Your feedback (Like, Helpful or Correct) helps community

          • Re: Allowing Categories and Subcategories to be available for specific Companies
            Sachin Namjoshi

            You need to configure on change client script to add, remove category and subcategory for specific company.

             

            Use addOption() and removeoption() for the other field choices.

             

            Refer the below threads may helpful to you.

             

            Making sub subcategory dependent on subcategory and category.

             

            Category/subcategory in incident

             

            http://wiki.servicenow.com/index.php?title=Creating_New_Fields#Making_a_Field_Dependent&gsc.tab=0

             

            Regards,

            Sachin

            Please mark answer as Correct, helpful as appropriate.

            • Re: Allowing Categories and Subcategories to be available for specific Companies
              nrgodale

              you can use similar client side script as below to populate value based on company selection.

               

              function onChange(control, oldValue, newValue, isLoading, isTemplate) {

                  if (newValue == '') {

                      return;

                  }

                  var Categories= g_form.getValue('u_categories');

                  // Clear all of the choices from the Categories field choice list

                  g_form.clearOptions('u_Categories');

                  // If the the value of the Company field is Comp1, add

                  // additional choices to Category field choice list

                  if (newValue == 'Comp1') {

                      g_form.addOption('u_u_categories', 'hr', 'Human Resources ');

                      g_form.addOption('u_u_categories', 'fn', 'Finance');

                      g_form.addOption('u_u_categories', 'Legal', 'Legal');

                  }

              Make this client side script to run onChange for Company Field.

               

              Note: This is just a sample code.

               

              Hope this helps.

               

              PS: Your feedback (Like, Helpful or Correct) helps community

              • Re: Allowing Categories and Subcategories to be available for specific Companies
                Michael Fry

                Can you give a little more detail, like are you using record producer for inserting new incidents, or just inputting on form? What about when the form loads? a catalog items?

                 

                We filter Subcategories based on Company & Category, so can probably help you.

                  • Re: Allowing Categories and Subcategories to be available for specific Companies
                    Brian Greene

                    Hi Michael,

                     

                    Just inputting on form.  Below is an example.  Company A and B having separate Category and Subcategory options.  From the previous replies it appears this can be done but I'm not sure as to the script needed.  Any thoughts?

                     

                    Company A is presented with the following:

                    Category A

                    Category B

                    Category C

                    >Sub-Cat A

                    >Sub-Cat B

                    >Sub-Cat C

                     

                    Company B is presented with the following:

                    Category D

                    Category E

                    Category F

                    >Sub-Cat D

                    >Sub-Cat E

                    >Sub-Cat F

                      • Re: Allowing Categories and Subcategories to be available for specific Companies
                        Michael Fry

                        This should get you started, but again, we're filtering subcats based on company/category, not filtering category & subcats.

                         

                        We added a new field to the choice form, company. Then we can define company(s) that can see the choices or leave blank to make global:

                        Screen Shot 2018-01-03 at 6.11.21 PM.png

                         

                         

                        We use 2 client scripts, one onLoad, one onChange. Here is the onChange:

                        function onChange(control, oldValue, newValue, isLoading) {

                           

                            if (isLoading || newValue == '') {

                                return;

                            }

                           

                            g_form.clearOptions('subcategory');

                            g_form.addOption('subcategory', '', '-- None --');

                           

                                //build a new list of dependent options

                            var ga = new GlideAjax('GetSubcats');

                            ga.addParam('sysparm_name', 'getmysubcats');

                            ga.addParam('sysparm_comp',g_form.getValue('company'));

                            ga.addParam('sysparm_cat',g_form.getValue('category'));

                            ga.getXML(ajaxResponse);

                           

                            function ajaxResponse(serverResponse) {

                                var subcats = serverResponse.responseXML.getElementsByTagName("subcat");

                                var output = "";

                               

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

                                    var label = subcats[i].getAttribute("label");

                                    var value = subcats[i].getAttribute("value");

                                    //output += label + " = " + value + "\n ";

                                    //alert('output '+output);

                                    g_form.addOption('subcategory', value, label);

                                }

                            }

                        }

                         

                        The script includes looks like this:

                        var GetSubcats = Class.create();

                        GetSubcats.prototype = Object.extendsObject(AbstractAjaxProcessor, {

                           

                            //called from client scripts

                            getmysubcats: function() {

                                var comp = this.getParameter('sysparm_comp');

                                var cat = this.getParameter('sysparm_cat');

                                //gs.log('company!! '+ comp);

                                //gs.log('category!! '+ cat);

                               

                                var gp = new GlideRecord('sys_choice');

                                gp.addQuery('table', 'incident');

                                gp.addQuery('element', 'subcategory');

                                gp.addQuery('dependent_value', cat);

                                gp.addQuery('inactive', false);

                                var qc = gp.addQuery('u_company','CONTAINS', comp);

                                qc.addOrCondition('u_company','');

                                gp.addQuery('inactive', 'false');

                                gp.orderBy('label');

                                gp.query();

                                while(gp.next()){

                                    this._addSubcategories(gp.label, gp.value);

                                    //gs.log('getmysubcats '+gp.label +' / '+ gp.value +' / '+ gp.sequence);

                                }

                            },

                           

                            _addSubcategories : function(label, value) {

                                var scat = this.newItem("subcat");

                                scat.setAttribute("label", label);

                                scat.setAttribute("value", value);

                            },

                           

                            type : "GetSubcats"

                           

                        });

                         

                        You're going to need 2 client scripts and 1 script includes for Category, and then same for Subcategory.

                        Hope this helps!