Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get current catalog item's category name or id?

poornima2
Mega Expert

Is there a way to get the current catalog item's category using catalog client script?.   Thanks in advance.

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Poornima,



Here is the script.


You have to create a client script.


var sys = g_form.getParameter("sysparm_id");


var ga = new GlideAjax('x_13241_smart_city.HelloWorld');//Replace the parameter with the API name of the script include you will create in the next step.


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


ga.addParam('sysparm_item_id',sys);


ga.getXML(HelloWorldParse);



function HelloWorldParse(response) {


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


    alert(answer);


}



Create a script include


Name : HelloWorld


Client callable ; Checked


API Name : x_13241_smart_city.HelloWorld // Make sure you replace the API name with exactly as yours in the client script in the 2 nd line of code.


Script :


var HelloWorld = Class.create();


HelloWorld.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {


  helloWorld: function() {


  var sysId = this.getParameter('sysparm_item_id')


  var gr = new GlideRecord('sc_cat_item');


  gr.addQuery('sys_id', sysId);


  gr.query();


  while(gr.next())


  {


  return gr.name;


  }


  },



  _privateFunction: function() { // this function is not client callable



}




});


});



I hope this helps


View solution in original post

5 REPLIES 5

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Poornima,



You can get the catalog item sys_id with the below line of code.


var a = g_form.getParameter("sysparm_id");


Then you can do the GlideAjax and query the category and return from the server.



I hope this helps


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Poornima,



Here is the script.


You have to create a client script.


var sys = g_form.getParameter("sysparm_id");


var ga = new GlideAjax('x_13241_smart_city.HelloWorld');//Replace the parameter with the API name of the script include you will create in the next step.


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


ga.addParam('sysparm_item_id',sys);


ga.getXML(HelloWorldParse);



function HelloWorldParse(response) {


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


    alert(answer);


}



Create a script include


Name : HelloWorld


Client callable ; Checked


API Name : x_13241_smart_city.HelloWorld // Make sure you replace the API name with exactly as yours in the client script in the 2 nd line of code.


Script :


var HelloWorld = Class.create();


HelloWorld.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {


  helloWorld: function() {


  var sysId = this.getParameter('sysparm_item_id')


  var gr = new GlideRecord('sc_cat_item');


  gr.addQuery('sys_id', sysId);


  gr.query();


  while(gr.next())


  {


  return gr.name;


  }


  },



  _privateFunction: function() { // this function is not client callable



}




});


});



I hope this helps


Awesome. It worked. Thanks Pradeep


Perfect..


I am Glad I can be helpful


dan.bruhn...Can you please help to close this thread.