Need to auto populate ip address field based on the computer field(list collector)

Shidhi
Tera Contributor

Hello,

I have a requirement to auto-populate the IP address field with the Ip address of the computer selected in another field(which is a list collector referencing cmdb_ci).

Thank you.

1 ACCEPTED SOLUTION

Hi,

update as this

Script Include:

var IP_addresses = Class.create();
IP_addresses.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getIpAddress: function(){
		var arr = [];
		var computer = this.getParameter('sysparm_ip');
		var gr = new GlideRecord('cmdb_ci_computer');
		gr.addQuery('sys_id', 'IN' ,computer);
		gr.query();
		while(gr.next()){
			arr.push(gr.getValue('ip_address'));
		}
		return arr.toString();
	},

	type: 'IP_addresses'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading) {
		return;
	}

	if (newValue == '') {
		g_form.clearValue('ipa_rdp');
		return;
	}

	var com = g_form.getValue('names_rdp');
	var ga = new GlideAjax('IP_addresses');
	ga.addParam('sysparm_name', 'getIpAddress');
	ga.addParam('sysparm_ip', com);
	ga.getXML(getIp);
}

function getIp(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	g_form.setValue('ipa_rdp',answer);
}

Regards
Ankur

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

View solution in original post

8 REPLIES 8

Hi,

update as this

Script Include:

var IP_addresses = Class.create();
IP_addresses.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getIpAddress: function(){
		var arr = [];
		var computer = this.getParameter('sysparm_ip');
		var gr = new GlideRecord('cmdb_ci_computer');
		gr.addQuery('sys_id', 'IN' ,computer);
		gr.query();
		while(gr.next()){
			arr.push(gr.getValue('ip_address'));
		}
		return arr.toString();
	},

	type: 'IP_addresses'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading) {
		return;
	}

	if (newValue == '') {
		g_form.clearValue('ipa_rdp');
		return;
	}

	var com = g_form.getValue('names_rdp');
	var ga = new GlideAjax('IP_addresses');
	ga.addParam('sysparm_name', 'getIpAddress');
	ga.addParam('sysparm_ip', com);
	ga.getXML(getIp);
}

function getIp(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	g_form.setValue('ipa_rdp',answer);
}

Regards
Ankur

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

Shidhi
Tera Contributor

Hi Ankur,

Thank you very much, it worked. I have a few doubts.

1. In the attachment, we can see the log (one sys_id after that two sys_id) I selected two computers I have no idea why like this can you please explain.

2. Also, in the IP address can we display space after ','. I think, since we are converting to the string we can't do it.

 

3. 

gr.addQuery('sys_id', 'IN' ,computer);

Here 'IN' the object operator, how it will identify the comma-separated sys_id's against the computer.

 

Thank you.

 

@Shidhi 

Glad to know that my script worked.

Please mark my response as correct and helpful to close the thread.

Regards
Ankur

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

@Shidhi 

Since it's list collector user can select multiple values and hence we are using IN operator as it can iterate over all the sysIds

Now to your next question yes we can add space after comma

var IP_addresses = Class.create();
IP_addresses.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getIpAddress: function(){
		var arr = [];
		var computer = this.getParameter('sysparm_ip');
		var gr = new GlideRecord('cmdb_ci_computer');
		gr.addQuery('sys_id', 'IN' ,computer);
		gr.query();
		while(gr.next()){
			arr.push(gr.getValue('ip_address'));
		}
		return arr.join(', ');
	},

	type: 'IP_addresses'
});

Regards
Ankur

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