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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Is this for catalog form or normal form

IP address field/variable is of what type

Also since there could be multiple computers selected then you would get multiple IP addresses

Regards
Ankur

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

Hi Ankur,

It is for catalog form.

The IP address field is a single-line text (also the user must have the feasibility to enter manually in case if IP address doesn't exist)

Yes, since the computer field is a list collector we can get multiple IP addresses.

 

Thank you.

 

Hi,

I believe 1 computer will have only 1 IP address.

Can you share the script you started with and where are you stuck?

Regards
Ankur

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

Shidhi
Tera Contributor

Hello Ankur,

Yes, 1 computer will have only one IP address since we are using List Collector, multiple computers can be selected after that in the IP addresses field the corresponding IP address must be auto-populated(if 1 computer is selected one IP address or if two computers 2 IP address separated with comma ). Please have a look at the attachment.

The difficulty I'm facing is since we are using List collector to get the computers(which is dynamic) which I have no idea. Below is the snippet of code which works fine if we select only one computer it is displaying IP address of it.

 

Catalog Client Script:-

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



if (newValue == '') {


g_form.setValue(ipa_rdp, " ");


return;


}



if(newValue != oldValue || isLoading){


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);


}


}

 

Script Include:-

 

var IP_addresses = Class.create();


IP_addresses.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getIpAddress: function(){


var computer = this.getParameter('sysparm_ip');

var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('sys_id',computer);


gr.query();


if(gr.next())


{

var i = gr.ip_address;


}


return i;


},

type: 'IP_addresses'
});

 

 Now it needs to work if multiple computers are selected(i.e., those corresponding IP addresses must be populated with comma separation).

Any help, please.