- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2022 07:17 AM
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.
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-07-2022 03:46 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-07-2022 03:46 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-07-2022 08:10 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-07-2022 08:16 AM
Glad to know that my script worked.
Please mark my response as correct and helpful to close the thread.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-07-2022 08:17 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader