Catalog variable value from script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2025 02:24 AM
Hi,
I have a catalog form where one of the variables are a reference to the cmdb for the user to select a server.
Based on this server I would like to have a read only field that dynamically lists any Application Service that has either has a "Depends on" or "Hosted on" type of relationship to the selected server.
How can I achieve this?
I assume that I need to work with the cmdb_rel_ci table and I guess I need to write a script in some way. I really appreciate any input I can get.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2025 02:25 AM
yes that's correct.
What type of variable did you create and what ref qualifier you applied?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2025 02:48 AM
Hi @magnushedlu ,
You can do the following:
- Create On Change Client Script on variable reference "CMDB" table.
- Pass the Value of reference variable to script include function.
- Create Client callable Script Include.
- Do a glide record in cmdb_rel_ci and use addQuery and the value of CI from client script as per requirement.
- Declare array variable (if in case there are more than one CI are getting query)
- Push the sys_id in array and use getDisplayValue() so that display of CI's are stored in your readonly variable.
- Return the array
- Now in Client Script us g_form.setValue("variable_name",answer)
If my response helped, please hit the Thumb Icon and accept the solution so that it benefits future readers.
Regards,
Rohit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2025 04:03 AM
Add a reference variable in your catalog item that allows users to select a server (e.g., referencing cmdb_ci).
Add a read-only variable (e.g., a multi-line text or a list) to display the related Application Services.
se an onChange client script on the server reference variable to query the cmdb_rel_ci table and fetch related Application Services.
Client script: (modify the variable names accordingly)
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || !newValue) {
return;
}
var ga = new GlideAjax('GetRelatedAppServices'); // Call Script Include
ga.addParam('sysparm_server', newValue);
ga.getXMLAnswer(function(response) {
g_form.setValue('application_services', response); // Set value to the read-only variable
});
}
Script inlcude:
getRelatedServices: function() {
var serverId = this.getParameter('sysparm_server');
if (!serverId) return '';
var serviceList = [];
var gr = new GlideRecord('cmdb_rel_ci');
gr.addQuery('parent', serverId); // "Depends on" relationship
gr.addQuery('type.name', 'IN', 'Depends on, Hosted on'); // Filter by relationship type
gr.query();
while (gr.next()) {
var appService = gr.child.getDisplayValue();
if (appService && serviceList.indexOf(appService) === -1) {
serviceList.push(appService);
}
}
return serviceList.join(', ');
}
Please mark correct/helpful if this helps you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2025 06:54 AM
Thx for the good input.
Sorry for this noob question, but what table should I set in the client script form to find the form variable?