
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2017 12:46 PM
I have a reference field called servers. This table contains information ip address and description. I want to be able to autopopulate ip address and decription.
For example, in the hostname field I will select a server name, and after I select the server name it will autopopulate the IP Address and Description.
How do I do that? Thank you.
Table name : cmdb_ci_server
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2017 01:28 PM
Ok let me try to help you without the context because it seems that i can't explain what i need.
First of all you need to create a client script on change.
Set the basic information such as table (the table where the hostname reference field is placed) and field name (e.g. u_hostname).
Supposing the fields you want to populate are named u_ip and u_desc
Than create something like this one
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var serverId = g_form.getValue('u_server');
var ga = new GlideAjax('myCmdbUtils');
ga.addParam('sysparm_name', 'getServerInfo');
ga.addParam('sysparm_server_id', serverId);
ga.getXML(ajaxResponse);
function ajaxResponse(serverResponse) {
// get result element and attributes
var result = serverResponse.responseXML.getElementsByTagName("result");
// get favorite elements
var info = serverResponse.responseXML.getElementsByTagName("info");
for(var i = 0; i < info.length; i++) {
var name = info[i].getAttribute("name");
var value = info[i].getAttribute("value");
//This is just for cotrol you need to comment later
alert('name :: ' + name + ' - value ::' + value);
if(name == 'ip'){
g_form.setValue('u_ip', value);
}
if(name == 'desc'){
g_form.setValue('u_desc', value);
}
}
}
}
Now you need to create a new script include named 'myCmdbUtils'
The script include must have the 'client callable' tick box ticked.
The script should be something like this
var myCmdbUtils = Class.create();
myCmdbUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getServerInfo : function() {
var serverId = this.getParameter('sysparm_server_id');
var gr = new GlideRecord('cmdb_ci_server');
gr.addQuery('sys_id', serverId);
gr.query();
if(gr.next()){
var result = this.newItem("result");
this._addInfo("desc", gr.description);
var netAdpt = new GlideRecord('cmdb_ci_network_adapter');
netAdpt.addQuery('cmdb_ci', gr.sys_id);
netAdpt.query();
if(netAdpt.next()){
this._addInfo("ip", netAdpt.ip_address);
}
}
},
_addInfo : function(name, value) {
var info = this.newItem("info");
info.setAttribute("name", name);
info.setAttribute("value", value);
},
type: 'myCmdbUtils'
});
I hope this will help/answer your question and if it does please mark it
Cheers
R0b0

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2017 02:56 PM
Thank you so much. Like I said I am new in SN, and I'm trying to learn as much as possible. I created a client script and a include script but they are not working. It doesn't auto-populate the IP address and the reference field.
Client Script :
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var user = g_form.getValue('requested_for');
var ga = new GlideAjax('PenTestCatalogTaskAjax ');
ga.addParam('sysparm_name','getServerInfo');
ga.addParam('sysparm_server_id', serverID);
ga.getXML(getServerParse);
function getServerParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
//Split Answer string to array on the commas
var returnedArray = answer.split(',');
//IP Address
var IP = returnedArray[0];
//Description
var DES = returnedArray[1];
//Set Display Values
g_form.setValue('ip_address_var', IP);
g_form.setValue('description_var', DES);
}
}
Script Include :
var PenTestCatalogTaskAjax = Class.create();
PenTestCatalogTaskAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getServerInfo : function() {
var serverID = this.getParameter('sysparm_server_id'); //get server ID
var gr = new GlideRecord('cmdb_ci_server'); //Go to the server table
gr.addQuery('sys_id', serverID); // 'what you are querying' , ' what the value is'
gr.query();
gr.next();
var description = gr.short_description; //get short description
var arrayIP = []; //array that will contain all IP addresses
var ipAddress = new GlideRecord ('cmdb_ci_network_adapter'); //go to network table
ipAddress.addQuery('cmdb_ci', serverID); // 'what you are querying' , ' what the value is'
ipAddress.query();
while (ipAddress.next()){
arrayIP.push(ipAddress.ip_adrress.toString()); //push ip addess to my array
}
return description + ','+ arrayIP;
},
type: 'PenTestCatalogTaskAjax'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2017 07:14 PM
Quick Question! I went to mt cmdb_ci_server table and there is a field called name Name. Why are you passing sys_id and server Id? The problem that I am having is that I do not know what to pass. How do you know is sys_id and serverId?
- var serverId = this.getParameter('sysparm_server_id');
- var gr = new GlideRecord('cmdb_ci_server');
- gr.addQuery('sys_id', serverId);
- gr.query();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2017 03:28 AM
Hi Claudia
The variable named serverId is populated with the value from the client script.
Check line 10 of your last client script. You are doing exactly the same.
There is only one thing missing. Your variable named hostname_var must be used as well.
The client script is created on change against that variable right ?
On the other hand you need the value stored in that field to execute the script include.
This means that line 10 of your client script must changed as
ga.addParam('sysparm_server_id', g_form.getValue('hostname_var'));
OR
var user = g_form.getValue('requested_for');
var serverID = g_form.getValue('hostname_var');
var ga = new GlideAjax('PenTestCatalogTaskAjax ');
ga.addParam('sysparm_name','getServerInfo');
ga.addParam('sysparm_server_id', serverID);
ga.getXML(getServerParse);
In theory this should be enough to fix your code.
About your question. How do you know is sys_id and serverId?
Here's the explanation. Locally on the service catalog form each time you populate the field named hostname_var you are referencing a specific server.
Using g_form.getValue('hostname_var') will provide you the unique identifier related with the server record.
When using a reference field in a client script you can't obtain directly any other information of the referenced record other than the SYS_ID.
For this reason we are using this value on the script include to query the server table and have access to all the necessary attributes (e.g. description, name etc)
I hope this help/answer your question and if it does please mark it
Cheers
R0b0