Access GlideRecord from within service portal widget.

Patrick69
Tera Contributor

How can I run a Glide query from in a widget? (Service Portal)

This code runs fine in the script-background editor, but it doesn't work in the Server-script section of my widget:

var grTask = new GlideRecord('task');
grTask.get('number', "REQ0323232");  // hardcoded good sample
destination_sys_id = grTask.sys_id;

When I run the code in Scripts, I get:

*** Script: sys_id: 0f4d...905


When I run it in the widget, I get:

{}

 

 

To elaborate on my Widget code:

data.test={ {data.test} }
 
Server script
(function(){
  var destination_sys_id = "initialized";
  var grTask = new GlideRecord('task');
  grTask.get('number', "REQ0323232");
  destination_sys_id = grTask.sys_id;
  data.test = destination_sys_id;
})()
 
1 ACCEPTED SOLUTION

nataliya_b
Tera Guru

instead of

destination_sys_id = grTask.sys_id; 

 

try this:

destination_sys_id = grTask.sys_id.toString();

View solution in original post

3 REPLIES 3

Brad Tilton
ServiceNow Employee
ServiceNow Employee

You should be able to do access GlideRecord from your service portal widget. When you say it doesn't work, what you do mean? It looks like you're assigned the sys_id of that record to an already declared server side JS variable. What are you doing with that variable that makes you think it doesn't work?

nataliya_b
Tera Guru

instead of

destination_sys_id = grTask.sys_id; 

 

try this:

destination_sys_id = grTask.sys_id.toString();

Generally, you want to use getters and setters when working with GlideRecord, so I would do grTask.getValue('sys_id') rather than grTask.sys_id.toString().