Here's How to Write a Scripted REST API to Get Catalog Item Variables in ServiceNow via REST!

Tim Woodruff
Mega Guru

Howdy folks!

I recently wrote an article on how to create a scripted REST API to retrieve catalog item variables via REST (something you couldn't do previously, in Geneva and earlier), so I thought I'd share.

In helsinki, there's the sn_sc API that might make this possible, but this is still a quick and easy way to accomplish that, and for anyone still on Geneva or earlier, something like this is your only option for getting catalog item variables via web services.

SN Pro Tips — ServiceNow Scripted REST APIs & Retrieving Catalog Item Variables

Let me know your thoughts, and if you enjoy the article, I humbly welcome you to subscribe for more. 🙂

-Tim

9 REPLIES 9

Hi Jace... I am looking to pull the additional data for the GET to populate a custom CMS on Geneva. I was able to add the additional attributes to the list of objToPush items but it seems my data only appears on two of the 50+ variables I am am pulling back.



I also noticed the additional data only comes back for fields that have a default value specified.. if I add a default value to any of them the additional info does appear, is there a reason why that would be?



Here were my edits:



                              objToPush = {};


                              if (vars.name.toString() !== '') {


                                      objToPush.name = vars.name.toString();


                              }


                              //if (vars.order.toString() !== '') {


                              objToPush.order = vars.order.toString();


                              //}


  if (vars.default_value.toString() !== '') {


                                      objToPush.sys_id = vars.sys_id.toString();


                              }


                              if (vars.question_text.toString() !== '') {


                                      objToPush.label = vars.question_text.toString();


                              }


                              if (vars.type.toString() !== '') {


                                      objToPush.type = vars.type.getDisplayValue();


                              }


                              if (link.toString() !== '') {


                                      objToPush.link = link.toString();


                              }


                              if (options.toString() !== '') {


                                      objToPush.options = options;


                              }


                              if (expected_value.toString() !== '') {


                                      objToPush.reference_field_name = expected_value.toString();


                              }


                              if (display_value.toString() !== '') {


                                      objToPush.reference_display_field_name = display_value.toString();


                              }


                              if (vars.default_value.toString() !== '') {


                                      objToPush.default_value = vars.default_value.toString();


                              }


                              if (vars.default_value.toString() !== '') {


                                      objToPush.show_help = vars.show_help.toString();


                              }


                              if (vars.default_value.toString() !== '') {


                                      objToPush.help_text = vars.help_text.toString();


                              }


                              if (vars.default_value.toString() !== '') {


                                      objToPush.tool_tip = vars.tool_tip.toString();


                              }


                              this.retVal.vars.push(objToPush);


                              val = '';


                              if (options[0]) {


                                      val = options[0].value;


                              } else {


                                      val = '';


                              }


                              this.retVal.examplePost[0].vars[vars.name.toString()] = val;


                      }




              } else {


                      this.retVal.error = 'Cannot find Catalog Item';


                      this.retVal.suggestion = 'Try /service.do?item=SYS_ID';


              }


              return this.retVal;


      },


robduca
Giga Contributor

also noticed I had the tool tip field specifed wrong... thats since been correct 'tooltip' instead of 'tool_tip'


robduca
Giga Contributor

I figured it out.. my adds were not properly constructed... I needed to remove the default_value from them.. and replace with my new feild name


robduca
Giga Contributor

When querying select boxes how can the script include be altered to provide the value and as well the text that is displayed for each option? Currently the only thing being reported back via the processor is the value which comes back the same for display and value. getDIsplayValue() does not seem to work on select box options.


Reach out to me on slack


Invite here: http://snowslack.herokuapp.com/