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

@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

@Ankur Bawiskar ,

In the actual script, I have used the proper table name and the field name for hostname as 'name'.

Os field name is also correct

2022_ServiceNow_0-1698652154130.png

 

@2022_ServiceNow 

did you add alert and see what came in the os value for each server?

what debugging have you done so far?

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

@Ankur Bawiskar 

it was giving me blank values. I have added alerts everywhere. I got the solution now.

@2022_ServiceNow 

Could you also mark my response as correct as I was having continuous discussions on thread with you regarding the updated script?

 

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