- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2015 12:19 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2015 02:45 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2015 01:16 AM
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 .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2015 02:45 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2015 02:52 AM
Probir Das has provided entire code, just update it with your requirement and use, it will work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2015 06:53 AM
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'
});