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

Auto-populate values from CMDB

carlocsa
Kilo Expert

Hi SN Gurus!

I have a Service Catalog Form that contains the CMDB Name and Owner Group fields. The CMDB Name references to the CMDB table.   I want to auto-populate the Owner Group field based on the CMDB Name selected by the user.

Please help!

Carlo

1 ACCEPTED SOLUTION

Here you go.


Create an OnChange catalog client script on CMDB name field with the script as below. Please make sure to update line no 16 i.e Pass the owner field variable.


function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {


          return;


    }



var ga = new GlideAjax('CmdbDetails');


ga.addParam('sysparm_name','cmdbdetails');


ga.addParam('sysparam_id',newValue);


ga.getXML(Process);


   


}




function Process(response) {


    var answer = response.responseXML.documentElement.getAttribute("answer");


    g_form.setValue("PASS OWNER FIELD VARIABLE HERE",answer);


}




create a script include


Name : CmdbDetails


Client Callable : True


Script :


var CmdbDetails = Class.create();


CmdbDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {


cmdbdetail: function() {


  var gr=new GlideRecord("cmdb_ci"); //Pass the table name here


  gr.addQuery("sys_id",this.getParameter('sysparam_id'));


  gr.query();


  gr.next();


 


  if(gr.owner!=''){



  return gr.owner;


  }



       


    },


      type: 'CmdbDetails'


});



View solution in original post

8 REPLIES 8

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Carlo,



As a best practice, you can do this via Client Script + GlideAjax approach. The wiki link mentioned here has a sample code.


http://wiki.servicenow.com/index.php?title=GlideAjax#gsc.tab=0

You can also refer the below thread for reference and adjust your code accordingly.


How to autopopulate department name of an user into a text field in an item variable by using catalo...




sachin_namjoshi
Kilo Patron
Kilo Patron

This might help


Adding Dependent Variables - ServiceNow Wiki



Or you can create catalog client script to populate second value.



Regards,


Sachin


I was thinking of using a catalog client script to populate the Owner Group field but no idea how to start. Is there a sample code that I could use?


I am a novice in scripting and have a hard time completing tasks as simple as this!



Regards,


Carlo


Here you go.


Create an OnChange catalog client script on CMDB name field with the script as below. Please make sure to update line no 16 i.e Pass the owner field variable.


function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {


          return;


    }



var ga = new GlideAjax('CmdbDetails');


ga.addParam('sysparm_name','cmdbdetails');


ga.addParam('sysparam_id',newValue);


ga.getXML(Process);


   


}




function Process(response) {


    var answer = response.responseXML.documentElement.getAttribute("answer");


    g_form.setValue("PASS OWNER FIELD VARIABLE HERE",answer);


}




create a script include


Name : CmdbDetails


Client Callable : True


Script :


var CmdbDetails = Class.create();


CmdbDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {


cmdbdetail: function() {


  var gr=new GlideRecord("cmdb_ci"); //Pass the table name here


  gr.addQuery("sys_id",this.getParameter('sysparam_id'));


  gr.query();


  gr.next();


 


  if(gr.owner!=''){



  return gr.owner;


  }



       


    },


      type: 'CmdbDetails'


});