Show Parent.sys_class_name choices based on another field reference

JuniorDev82
Tera Contributor

I have a catalog that need to display the class name of the web application based on the selection of a variable called Server. I'm performing an upstream search from the Server CI to retrieve the list of CIs associated with the web component variable, which involves traversing from child to parent. However, I'm currently stuck on how to display the class name of the web application. When I select a server, the web application should show the parent.sys_class_name, such as TOMCAT, APACHE etc. It may consists multiple web applications. If I simply retrieve the class name from the server, it returns Linux Server instead. I can take an alternative approach by selecting the web component first and then displaying the web application, but the customer prefers not to do it this way. If anyone has experienced this issue before, please help me. Thank you!

JuniorDev82_0-1734875539171.png

JuniorDev82_1-1734876213882.png

 

 

1 ACCEPTED SOLUTION

@JuniorDev82 

I hope you have the correct choice values and choice labels configured for that web_application variable

I assume your choice value and choice label both are same in that web_application choice variable, if yes then below script will ensure it will add options based on the array returned from script include function

If your choice labels and values are different then you need to use IF else within your script include function and return a JSON object containing choice label and value

Then within client script, parse the json and add the choice labels and choice values.

I hope I have answered your question and you can take it further from here.

if (newValue == '' || newValue == null) {
g_form.setValue('web_application', '');
} else if (newValue != oldValue) {
g_form.setValue('web_application', '');

var ga = new GlideAjax('CLOUAjaxUtils');
ga.addParam('sysparm_name', 'getParentClassName');
ga.addParam('sysparm_childsysid', newValue);

ga.getXMLAnswer(function(response) {
console.log("suraya =:" + response);
var parentClassName = response;
if (parentClassName != '') {
var arr = parentClassName.split(',');

for (var i in arr) {
    g_form.addOption('web_application', arr[i], arr[i]);
}


} else {
g_form.clearOptions('web_application'); // Clear the field if no parent class name is found.
}
});
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

@JuniorDev82 

you can use onChange catalog client script + GlideAjax here

what did you start with and where are you stuck?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Ankur Bawiskar 

 

Thank you for the reply. I've applied the onchange client script + GlideAjax, however i got the result in string instead of choices, refer to the ss. The variable type i'm using currently for web applications is single line text.

 

I created  a function that called via client script onchange:
===========================================

getParentClassName: function() {
    var arrList = [];
    var arrClassName = [];
    var parentClassName = '';
    var childSysId = this.getParameter("sysparm_childsysid");
    var rel = new GlideRecord('cmdb_rel_ci');
    rel.addQuery('child', childSysId);
    rel.addEncodedQuery('parent.sys_class_name=cmdb_ci_apache_web_server^ORparent.sys_class_name=cmdb_ci_microsoft_iis_web_server^ORparent.sys_class_name=cmdb_ci_db_mysql_instance^ORparent.sys_class_name=cmdb_ci_app_server_tomcat');
    rel.query();

    while (rel.next()) {
        var parentSysId = rel.parent.sys_id;
        var parentCI = new GlideRecord('cmdb_ci');
        if (parentCI.get(parentSysId)) {
            parentClassName = parentCI.sys_class_name.getDisplayValue();
            arrClassName.push(parentClassName);
        }
    }
    var arrayUtil = new ArrayUtil();
    arrList = arrayUtil.unique(arrClassName);
    return JSON.stringify(arrList);
},

====================================

 

JuniorDev82_0-1735018938244.png

 

@JuniorDev82 

return array and not JSON

return arrList.toString();

when you get this array in GlideAjax use g_form.addOption to add the choices to Web component 2 variable

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Ankur Bawiskar 

 

Thank you, but should I use the variable type select box instead of single line text?