The CreatorCon Call for Content is officially open! Get started here.

script to auto populate fields!

SNnewbie2
Tera Expert

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

Capture74.PNG

Capture73.PNG

1 ACCEPTED SOLUTION

Ivano B
ServiceNow Employee
ServiceNow Employee

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


View solution in original post

12 REPLIES 12

Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Claudia



Quick question. The hostname field is on a service catalog form right ?



Cheers


R0b0


The hostname field is a reference table.


Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Claudia



Ok that was clear I need to understand if that field is a variable on a service catalog on a reference field on a table.


That's it



Cheers


R0b0


That field is a reference table in service name. If I want to go to the table I usually write cmdb_ci_server.list