Using Out-of-the-box Indient Category & Subcategory Fields on a Request Form

Wesley Breshear
Tera Expert

Hello

I trying to keep this simple and align with ServiceNow OOTB features.   Currently in ServiceNow on an Incident Record there is Category (string) and Subcategory (reference) fields.   I am creating a Service Portal/Catalog form request to get End User values and directly create an Incident record; standard or basic Help Desk form "I have a problem and need to create an Incident".   So my thinking was to match to the exact current "Category" and "Subcategory" field values of our ServiceNow instance, that way the values stay consistent with the values on the service catalog form vs. creating two static choice string fields and just passing the values to the incident record; more maintenance keep the form up to date with Incident record field values.

I am having two issue.

1.)   I am struggling in getting the string values of the 'Category' field from the Incident table to appear on my catalog from. I am using a reference field type on my catalog form but obviously the Category field on the Incident table is just a string box, and I am not able to find an appropriate table to reference, which maybe reason why it is not working.   So how do I reference this field from the Incident table?   Or am I just going to need to create a choice field with the same values as on my Incident Category column field?

2.)   Not seeing how to make the Subcategory field dependent on Category field selection like it is done within the Incident record.   I found the following article, so do I still need create a script include to make this functionality work on the catalog form request?   Or is there already an Out-of-the-box script include for the this function?   http://wiki.servicenow.com/index.php?title=Adding_Dependent_Reference_Variables#gsc.tab=0

______________________________________________________________________________________________________________________________

Fig #1

find_real_file.png

______________________________________________________________________________________________________________________________

Fig #2

find_real_file.png

1 ACCEPTED SOLUTION

You should just use Select Box for the Category/Subcategory on the record producer.


Screen Shot 2017-03-20 at 7.14.04 AM.png



I recently ran into a problem where I couldn't filter subcategory based on category on Service Portal. This link helped with that: Dependent Variables in Service Portal do not reload on values when changed


View solution in original post

17 REPLIES 17

Check here for help with #2: Adding Dependent Variables - ServiceNow Wiki


Wesley,



Michael's link to the wiki page is the common approach. The "built-in" dependency available in forms isn't available in catalog items. When one of the fields changes, a client script is used to query the choice table to return the values and essentially rebuild the choices for the drop-down. This article is quite old so you might want to use the logic from it and build an asynchronous glide ajax call: GlideAjax - ServiceNow Wiki  


Hi Cary & Michael,



I am a neophyte when is comes to Javascript.   I am thinking that I have everything correct from using the example you sent me to, but currently displaying no subcategories on my Subcategory field.   Can you please assist with my the code I have so far?



On my Form


Category field:   category


Subcategory field:   subcategory


Subcategory Ref qaul:   javascript: new u_getSubcatagories().MyFunction(current.variables.category);


Script Include


Script Name:   u_getSubcatagories



var u_getSubcatagories = Class.create();



u_getSubcatagories.prototype = Object.extendsObject(AbstractAjaxProcessor, {



        MyFunction: function(dependent_category){



  var subcategoryList = ' ';



  var subCat = new GlideRecord('sys_choice');


  subCat.addQuery('dependent_value',dependent_category);


  subCat.query();


  while(subCat.next()) {


  if (subcategoryList.length > 0) {


  subcategoryList += (',' + subCat.label);


  }


  else {


  subcategoryList = subCat.label;


  }


  }


  return 'sys_idIN' + subcategoryList; // sys_idIN, not sure what it should be changed to?


      },


  type: 'u_getSubcatagories'


});



Thank you,


-Wesley


You should just use Select Box for the Category/Subcategory on the record producer.


Screen Shot 2017-03-20 at 7.14.04 AM.png



I recently ran into a problem where I couldn't filter subcategory based on category on Service Portal. This link helped with that: Dependent Variables in Service Portal do not reload on values when changed


Hi Michael,



So I broke away from the http://wiki.servicenow.com/index.php?title=Adding_Dependent_Reference_Variables#gsc.tab=0 article and removed the Reference qual condition value.   And went with your suggested 'Dependent Variables in Service Portal do not reload on values when changed' article.   But still having problems with the Subcategory list being narrowed down based on the selected Category.   I switched both my Category and Subcategory fields on request form (incident record producer) to Select Box types.   My Subcategory field box is still pulling all possible values and is not filtering down based off the selected category. Could you please assist in what I missing?



Then created/copied the Script Include from the article.


Name:   HMgetSubCat


Client callable: [check]


Accessible from:   All application scopes



var HMgetSubCat = Class.create();


HMgetSubCat.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  getSubCat: function(){



  // Build a new list of dependent options


  var newValue = this.getParameter('sysparm_category');


  var gp = new GlideRecord("sys_choice");


  gp.addQuery("inactive", false);


  gp.addQuery("name", "sc_req_item");


  gp.addQuery("dependent_value", newValue);


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


  gp.query();



  var subcategories = [];



  while(gp.next()){


          subcategories.push({value: gp.value.toString(), label: gp.label.toString()});


  }


  return new JSON().encode(subcategories);


  },


  type: 'HMgetSubCat'


});



And created/copied the Catalog Client Script from the article.


Name:   HM Cat Subcat Portal Ajax


Applies to:   A Catalog Item


UI Tupe:   Mobile / Service Portal


Type:   onChange


Catalog Item:   Broken / Not working correctly


Variable name:   category



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



  if (isLoading) {


// Remove all items from subcat drop down to start


  //g_form.clearOptions('subcategory');


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


  return;


  }



  if (newValue == oldValue) {


  return;


  }



  // Remove all items from subcat drop down to start


  gb = new GlideAjax('HMgetSubCat');


  gb.addParam('sysparm_name', 'getSubCat');


  gb.addParam('sysparm_category', oldValue);


  gb.getXML(function(response) {


  var answerA = gb.getAnswer();


  var objA = JSON.parse(answerA);


  for(var j=0; j<objA.length; j++){


  g_form.removeOption('subcategory',objA[j].value);


  }


  });



  // ADD all new items from subcat drop down to start


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


  ga = new GlideAjax('HMgetSubCat');


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


  ga.addParam('sysparm_category', newValue);


  ga.getXML(function(response) {


  var answer = ga.getAnswer();


  var obj = JSON.parse(answer);


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


  g_form.addOption('subcategory',obj[i].value, obj[i].label);


  }


  });


}



My Subcategory field box is still pulling all possible values and is not filtering down based off the selected category.