Get CI attributes on the form and populate in Client Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
Hi,
Good Day..!!
The requirement is to populate the attributes on the form in other field.
E.g. If we select widows test server 1, then below field should list down all the fields present on that form.
I have written the script include as below
"
"
Can anyone help how to fetch those fields and show them on client side as dropdown options?
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
58m ago
This is not as straight-forward as it may seem as forms have form_sections, which have sections, which hold the elements. Here is an example I have used in a Catalog Item with the CI Name variable named v_ci and the Select field... select box type variable named v_field. Here's my Catalog Client Script when v_ci changes:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.clearOptions('v_field');
var ga = new GlideAjax('CIFormLookup');
ga.addParam('sysparm_name', 'getCIFields');
ga.addParam('sysparm_ci', newValue);
ga.getXML(addChoices);
function addChoices(response) {
var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
g_form.addOption('v_field','',' -- None --');
for(var i=0;i<answer.length;i++) {
g_form.addOption('v_field',answer[i].value,answer[i].label);
}
}
}
And the Client-callable Script Include:
var CIFormLookup = Class.create();
CIFormLookup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCIFields : function(){
var optionsArray = [];
var json = new JSON();
var ci = this.getParameter('sysparm_ci');
var ciClass = new GlideRecord('cmdb_ci');
if (ciClass.get(ci)) {
var frmGr = new GlideRecord('sys_ui_form');
frmGr.addQuery('name', ciClass.sys_class_name);
frmGr.addQuery('view', 'Default view');
frmGr.query();
while (frmGr.next()) {
var frmsecGr = new GlideRecord('sys_ui_form_section');
frmsecGr.addQuery('sys_ui_form', frmGr.sys_id);
frmsecGr.query();
while (frmsecGr.next()){
var secGr = new GlideRecord('sys_ui_section');
secGr.addQuery('sys_id', frmsecGr.sys_ui_section);
secGr.query();
while (secGr.next()) {
var eleGr = new GlideRecord('sys_ui_element');
eleGr.addQuery('sys_ui_section', secGr.sys_id);
eleGr.addNullQuery('type');
eleGr.query();
while (eleGr.next()) {
var dict = new GlideRecord('sys_dictionary');
dict.addQuery('name', ciClass.sys_class_name);
dict.addQuery('element', eleGr.element);
dict.query();
if (dict.next()) {
optionsArray.push({
'label' : dict.column_label.toString(),
'value' : dict.element.toString()
});
}
}
}
}
}
}
return json.encode(optionsArray);
},
type: 'CIFormLookup'
});
This populates the select box drop-down list with every field that is shown on the form for the selected CI.
