- 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
10-29-2023 11:26 AM
Hi @2022_ServiceNow,
Try to push os values into an array
var osArray=[];
var os = getOS(serverName);
if (os) {
osArray.push(os);
}
}
// Set the "os" field with the collected OS values.
g_form.setValue('os', osArray.join(', '));
}
Thanks,
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2023 09:05 PM - edited 10-29-2023 09:31 PM
Thanks for your response. Can you please help me with the script?
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(',');
var osArray = [];
for (var i = 0; i < hostnameArray.length; i++) {
setOS(hostnameArray[i].trim(), osArray);
}
}
}
function setOS(server, osArray) {
var gr = new GlideRecord('server');
if (gr.get('name', server)) {
osArray.push(gr.os);
} else {
g_form.addInfoMessage('No os ' + server);
}
g_form.setValue('os', osArray.join(', '));
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2023 10:42 PM
Hi @2022_ServiceNow ,
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++) {
var server = hostnameArray[i].trim();
var gr = new GlideRecord('server');
if (gr.get('name', server)) {
osArray.push(gr.os.toString());
} else {
g_form.addInfoMessage('No os for ' + server);
}
}
g_form.setValue('os', osArray.join(', '));
}
}
Thanks,
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2023 08:09 PM
it makes no meaning to have 2nd list collector also referring same table.
For a table only 1 field can be marked as Display=true at dictionary level.
So if user selects few records i.e. hostnames in 1st list collector it won't show the Operating system in 2nd one as the hostname will be possibly marked as Display=true
Please discuss this requirement with your business team
What I would suggest is to use onChange client script on 1st list collector + GlideAjax and fetch the OS and populate in multi-line text variable so that user knows the OS
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