when i select host name , associated ip address and environment auto populate in comma separate
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 11:13 PM
Hi All,
kindly help me on one requirement ,
when i select host name , associated ip address and environment auto populate in comma separate . currently its populating single values .
please help me code for this.
function onChange(control, oldValue, newValue, isLoading) {
var host = g_form.getReference('hostname', doAlert);
}
function doAlert(host) {
g_form.setValue('environment', host.u_environment);
g_form.setValue('ip_address', host.u_primary_ip_address);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 11:19 PM
Hi @Servicenow58,
Is your IP address and environment fields on form are list collector(multi-select) or string field?
Also, make sure that host name records have multiple values.
Can you share screenshots of form and host name records?
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 11:37 PM
IP address, environment single line text
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2022 05:31 AM
Hi @Servicenow58,
In this case, you have Hostname is list-collector(multi-select) variable on form. Hence getReference with callback function will not work for multiple hostnames.
You have to convert it into Glide-Ajax call. split the hostname Ids and get the details for the same.
Try this sample scrips & modify it as per need.
Client scripts:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ajax = new GlideAjax('global.GetHostnameData');
ajax.addParam('sysparm_name', 'getHostnameDetails');
ajax.addParam('sysparm_hostname_Ids', newValue);
ajax.getXML(callBackFunction);
function callBackFunction(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var host = JSON.parse(answer);
for (var key in host) {
var envIds = host[key].host_env.toString().split(','); // u_environment
var ipAddress = host[key].primary_ip.toString().split(","); // u_primary_ip_address
for (var count = 0; count < envIds.length; count++) {
g_form.setValue('environment', envIds[count].host_env);
g_form.setValue('ip_address', ipAddress[count].primary_ip);
}
}
}
}
Script Include:
var GetHostnameData = Class.create();
GetHostnameData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getHostnameDetails :function() {
var hostname_Ids = this.getParameter('sysparm_hostname_Ids');
var str = hostname_Ids.toString().split(",");
var hostEnv = [];
var primaryIP = [];
var finalArray = [];
for(var i = 0; i < str.length; i++){
var records = new GlideRecord('add_table_Name');
records.addEncodedQuery('sys_id=' + str[i]);
records.query();
if(records.next()) {
hostEnv.push(records.u_environment.toString());
primaryIP.push(records.u_primary_ip_address.toString());
var obj = {
host_env:hostEnv,
primary_ip:primaryIP
};
finalArray.push(obj);
}
}
return JSON.stringify(finalArray);
},
type: 'GetHostnameData'
});
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2022 02:24 AM - edited 11-05-2022 02:25 AM
Please find the code below:
onChange Client Script on Hostname field:
function onChange(control, oldValue, newValue, isLoading) {
var host = g_form.getValue('hostname');
var ga = new GlideAjax('HostnameIPandEnvironment');
ga.addParam('sysparm_name','getIPAddrressEnv');
ga.addParam('sysparm_host,host);
ga.getXML(setIPAddEnv);
}
function setIPAddEnv(response){
var answer = response.responseXML.documentElement.getAttribute('answer');
var result = JSON.parse(answer);
g_form.setValue('environment', result.environment);
g_form.setValue('ip_address', result.ipAddress);
}
Client Callable Script Include "HostnameIPandEnvironment":
var HostnameIPandEnvironment = Class.create();
HostnameIPandEnvironment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getIPAddrressEnv: function() {
var hostName = this.getParameter('sysparm_host');
hostName = hostName.split(','); // if code does not work, remove this line and try again
var envs= '';
var ips = '';
var hostName = new GlideRecord('<hostNameTable>'); // Put your Hostname table here
hostName.addEncodedQuery('sys_idIN'+hostName);
hostName.query();
while(hostName.next())
{
envs = envs + ',' + hostName.u_environment.toString();
ips = ips + ',' + hostName.u_primary_ip_address.toString();
}
var hostObj = {};
hostObj.environment = envs.slice(1);
hostObj.ipAddress = ips.slice(1);
return JSON.stringify(hostObj);
},
type: 'HostnameIPandEnvironment'
});
Please check if any spelling mistakes also please modify Script Include GlideRecord part with your HostName table name as mentioned in comment also.
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023