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

Hi Rene, Sub Category field is a choice field. The moment I try to use that, I dont see 'is empty' option. All I see is the choices that I have for this field.


pradeepgupta
Giga Expert

As onChange of CI field Sub Category is getting filled, client script can be written onChange of CI field to check option value of Sub Category and accordingly you can hide/show the Sub Category.



You can try below script:



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


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


          return;


    }



var subValue=g_form.getValue('sub_category'); //Assuming sub_category


if(subValue=='')


{


g_form.setDisplay('sub_category',false);


}


else


{


g_form.setDisplay('sub_category',true);


}



}


Hi Pradeep, Thanks for the input. I tried this already. The problem here is subcategory field. Remember its a choice field. Initially it will be none. By default, the value will be null for var subValue=g_form.getValue('sub_category');


So the script automatically sets the field invisible.


srijanani_mohan
Giga Contributor

Hi,



You can add a client script to check whether the CI has any subcategory and then you can use setVisible('sub_category',true) function to make the subcategory field visible.



Regards,


Srijanani


Hi Srijanani, How can I check whether the CI has subcategory or not. That gives me a hard time.