- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2023 10:55 AM
Hi,
I have a requirement to auto-populate a field value depending on the another field value. Both the variable type is 'List Collector' and referenced to same table 'server'. When user selects multiple hostname, the operating system of respective hostname should be populated.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var hostnames = g_form.getValue('system_hostname');
alert(hostnames);
if (hostnames) {
var hostnameArray = hostnames.split(',');
for (var i = 0; i < hostnameArray.length; i++) {
setOS(hostnameArray[i].trim());
}
}
}
function setOS(server) {
var gr = new GlideRecord('server');
if (gr.get('name', server)) {
g_form.setValue('os', gr.os);
g_form.addInfoMessage('Server ' + server + ' : OS ' + gr.os);
} else {
g_form.addInfoMessage('No os ' + server);
}
}
@Ankur Bawiskar @Manmohan K @Sagar Pagar @Amit Gujarathi @Karan Chhabra6
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2023 11:01 PM
HI @2022_ServiceNow ,
I trust you are doing great.
Please try below script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var hostnames = g_form.getValue('system_hostname');
if (hostnames) {
var hostnameArray = hostnames.split(',');
var osArray = [];
for (var i = 0; i < hostnameArray.length; i++) {
setOS(hostnameArray[i].trim(), osArray);
}
g_form.setValue('os', osArray.join(', '));
}
}
function setOS(server, osArray) {
var gr = new GlideRecord('server');
gr.addQuery('name', server);
gr.query();
if (gr.next()) {
osArray.push(gr.os.toString());
} else {
g_form.addInfoMessage('No OS found for ' + server);
}
}
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2023 11:57 PM
are you using correct field name for OS?
your table name is wrong
it should be cmdb_ci_server and not server
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var hostnames = g_form.getValue('system_hostname');
if (hostnames) {
var hostnameArray = hostnames.split(',');
for (var i = 0; i < hostnameArray.length; i++) {
setOS(hostnameArray[i].trim());
}
}
}
function setOS(server) {
var osArray = [];
var gr = new GlideRecord('cmdb_ci_server');
if (gr.get(server)) {
osArray.push(gr.os.toString());
} else {
g_form.addInfoMessage('No os ' + server);
}
g_form.setValue('os', osArray.join(', '));
}
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
10-30-2023 03:37 AM
Pls refer below link , it has similar requirement.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 09:43 AM
@Ankur Bawiskar Sure Sir, thank you so much!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2023 11:01 PM
HI @2022_ServiceNow ,
I trust you are doing great.
Please try below script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var hostnames = g_form.getValue('system_hostname');
if (hostnames) {
var hostnameArray = hostnames.split(',');
var osArray = [];
for (var i = 0; i < hostnameArray.length; i++) {
setOS(hostnameArray[i].trim(), osArray);
}
g_form.setValue('os', osArray.join(', '));
}
}
function setOS(server, osArray) {
var gr = new GlideRecord('server');
gr.addQuery('name', server);
gr.query();
if (gr.next()) {
osArray.push(gr.os.toString());
} else {
g_form.addInfoMessage('No OS found for ' + server);
}
}
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2023 03:37 AM
Pls refer below link , it has similar requirement.