$sp.getRecord() returns null in widget server side script

Niklas6
Tera Expert

I am trying to get hold of the record the widget is related to when the widget loads. I.e. no input will be available. 

 

It is a scoped environment but I think $sp should work anyway (since GlideSPScriptable is available as a scoped API). $sp exist since (typeof $sp) prints 'object'. 

 

Server Script: 

(function() {
gs.error("$sp? " + typeof $sp);

gs.error("record? " + $sp.getRecord());
})();

 

Output:

 

$sp? object
record? null

 

Is there something I need to think about to get this working which I, apparently, cannot find in any docs?

 

Update: This is to be loaded when clicking to see my To Dos in the portal. 

1 ACCEPTED SOLUTION

James Chun
Kilo Patron

Hi @Niklas6,

 

According to the document, 'getRecord' returns the following:

 

If parameters are provided, returns the record identified by the provided table and Sys ID.

If no parameters are provided, returns the record identified by the current URL.

Returns null if the widget is embedded by another widget, or if the record for the provided parameters is not found.

 

https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server/no-namespace/c_GlideSPSc...

 

With the 'hrm_todos_page' (I assume this is the page you are talking about), I don't see the table nor the sys_id of the selected task in the URL hence it's returning null. If you can elaborate on what you are trying to do, maybe we can assist you further.

 

Cheers

View solution in original post

4 REPLIES 4

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @Niklas6 

 

Try getting the sysid from URL & get its gliderecord. 

The $sp.getParameter() method retrieves the sys_id from the request querystring. You may require to use $sp.getParameter("sys_id") in this case.


 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

No functions work. But I fixed it if you look at my other comments. Thanks'! 

James Chun
Kilo Patron

Hi @Niklas6,

 

According to the document, 'getRecord' returns the following:

 

If parameters are provided, returns the record identified by the provided table and Sys ID.

If no parameters are provided, returns the record identified by the current URL.

Returns null if the widget is embedded by another widget, or if the record for the provided parameters is not found.

 

https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server/no-namespace/c_GlideSPSc...

 

With the 'hrm_todos_page' (I assume this is the page you are talking about), I don't see the table nor the sys_id of the selected task in the URL hence it's returning null. If you can elaborate on what you are trying to do, maybe we can assist you further.

 

Cheers

The third line of the spec you provided is the one related to this scenario. It is embedded and, hence, null is returned. I solve the problem at hand by accessing the options object which had been populated.