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

@JuniorDev82 

if you just want to show and don't want user to select then use single line text variable

if you want to use single line text then simply set it with the value received from ajax function

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 

My last question, I'm using select box since i want user to be able to select the web application based on the server selection. Here is my client script. Instead of showing the specific web app from the server selection, it displays all the 4 choices. Any idea what did i miss 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 != '') {
                g_form.addOption('web_application', 'cmdb_ci_app_server_tomcat', 'Tomcat');
                g_form.addOption('web_application', 'cmdb_ci_db_mysql_instance', 'LAMP');
                g_form.addOption('web_application', 'cmdb_ci_apache_web_server', 'Apache');
                g_form.addOption('web_application', 'cmdb_ci_microsoft_iis_web_server', 'IIS');
            } else {
                g_form.clearOptions('web_application'); // Clear the field if no parent class name is found.
            }
        });
    }

@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

Thank you so much 

@Ankur Bawiskar This is working now!

@JuniorDev82 

Thank you for marking my response as helpful.

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