Making a choice field visible

catchdini
Tera Expert

Hi, I have two fields (Incident table) CI and Sub Category for CI's, which is a dependent to CI field. Sub Category for CI's is a choice field and will have choices based on the CI field. I have to make Sub Category for CI's field visible only if there is a choice list available based on CI field. If a CI doesnt have a Sub Category for CI's choice associated, then the Sub Category for CI's should not be visible.

Ex" CI is Lync and Sub Category for CI's choice list is telephone, Voice - In this case, Sub Category for CI's should be visible

          CI is Lync 2010 and Sub category for CI's is not having any choices - In this case, Sub Category for CI's should NOT be visible.

Can someone please guide me in achieving this. Thanks in advance.

1 ACCEPTED SOLUTION

ProbirDas
Tera Expert

For demonstration, I have created a category "No Subcategory" with no dependent values.



onChange of the category field, wrote the below script:



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


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


  return;


  }


  //Type appropriate comment here, and begin script below



  var ga = new GlideAjax('SubCategoryExists');


  ga.addParam('sysparm_name',"ifSubCategoryExists");


  ga.addParam('sysparm_category',newValue);


  ga.getXML(HelloWorldParse);



}




function HelloWorldParse(response) {


  var answer = response.responseXML.documentElement.getAttribute("answer");


  if ( answer == "No Subcategory")


  {


  g_form.setDisplay("subcategory", false);


  }


  else


  {


  g_form.setDisplay("subcategory", true);


  }


}



The corresponding script include is as below. Make sure to make it client callable.



var SubCategoryExists = Class.create();


SubCategoryExists.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  ifSubCategoryExists: function() {


  var iChoice = new GlideRecord('sys_choice');


  iChoice.addEncodedQuery('name=incident^element=subcategory^dependent_value='+this.getParameter('sysparm_category'));


  iChoice.query();


  if(iChoice.next())


  {


  return "Subcategory Exists";


  }


  else


  {


  return "No Subcategory";


  }


  },


      type: 'SubCategoryExists'


});



The client script is written on "onChange". You might want to extend it to the onLoad of the form as well.



I have tested this and it works. Let me know how it goes for you.


View solution in original post

18 REPLIES 18

Mukesh Sharma
Giga Expert

        well I checked on wiki   that is there any method in choice list which will give us the length of list. unfortunately there in no such method . still you can try your luck with this alternate solution   .


        Since the field belongs to CI table. you can hit a GlideRecord on sys_choice table. filter will be table name which   is cmdb_ci , and field name which ic subcategory . and dependent value will be you category value .


        By this way you can get number of records. if records are 0. then you can make subcategory field invisible of your incident form .


ProbirDas
Tera Expert

For demonstration, I have created a category "No Subcategory" with no dependent values.



onChange of the category field, wrote the below script:



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


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


  return;


  }


  //Type appropriate comment here, and begin script below



  var ga = new GlideAjax('SubCategoryExists');


  ga.addParam('sysparm_name',"ifSubCategoryExists");


  ga.addParam('sysparm_category',newValue);


  ga.getXML(HelloWorldParse);



}




function HelloWorldParse(response) {


  var answer = response.responseXML.documentElement.getAttribute("answer");


  if ( answer == "No Subcategory")


  {


  g_form.setDisplay("subcategory", false);


  }


  else


  {


  g_form.setDisplay("subcategory", true);


  }


}



The corresponding script include is as below. Make sure to make it client callable.



var SubCategoryExists = Class.create();


SubCategoryExists.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  ifSubCategoryExists: function() {


  var iChoice = new GlideRecord('sys_choice');


  iChoice.addEncodedQuery('name=incident^element=subcategory^dependent_value='+this.getParameter('sysparm_category'));


  iChoice.query();


  if(iChoice.next())


  {


  return "Subcategory Exists";


  }


  else


  {


  return "No Subcategory";


  }


  },


      type: 'SubCategoryExists'


});



The client script is written on "onChange". You might want to extend it to the onLoad of the form as well.



I have tested this and it works. Let me know how it goes for you.


Probir Das has provided entire code, just update it with your requirement and use, it will work.


Thanks a lot Probir. Your code helped after little modifications. Below is the final piece of script include that worked.



var AjaxSubCategoryExists = Class.create();


AjaxSubCategoryExists.prototype = Object.extendsObject(AbstractAjaxProcessor, {  


  ifSubCategoryExists: function() {  


  var iChoice = new GlideRecord('sys_choice');  


  //iChoice.addEncodedQuery('name=incident^element=subcategory^dependent_value='+this.getParameter('sysparm_category'));


// iChoice.addQuery('dependent_value', current.cmdb_ci.name);


  iChoice.addQuery('dependent_value', '!=',"");


iChoice.addQuery('dependent_value', this.getParameter('sysparm_category'));


iChoice.query();


      if(iChoice.next())  


  {  


  return "Subcategory Exists";  


  }  


else  


  {  


  return "No Subcategory";  


  }  


  },  


      type: 'AjaxSubCategoryExists'


});