Auto-populate a field value depending upon another field

2022_ServiceNow
Tera Expert

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 

3 ACCEPTED SOLUTIONS

Amit Gujarathi
Giga Sage
Giga Sage

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



View solution in original post

@2022_ServiceNow 

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.

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

View solution in original post

17 REPLIES 17

Anand Kumar P
Giga Patron
Giga Patron

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

@Anand Kumar P 

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(', '));
}

 

 

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

Ankur Bawiskar
Tera Patron
Tera Patron

@2022_ServiceNow 

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.

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