Usage of UI Scripts (with gliderecords) in widget client script

Vespertinus
Tera Expert

Hello SNow Community,

i have a complex UI script with using GlideRecords of user & attachment objects and called from UI Action. I now wanted to reuse functions of the UI script in ServicePortal with adding a button. I found this: portal widget custom button   that worked fine for me calling the client script function from a button, from this i tried to call the UI Script:

Widget HTML:

<button type="button" class="btn btn-default send-message" ng-click="exportfunction()">${export}</button>

Widget ClientScript:

$scope.exportfunction = function()

{

          doActionfromUIScript($scope.data.sysUserID);

}

UI Script:

function doActionfromUIScript(usersysid) {

  var gr_user = new GlideRecord("sys_user");

  gr_user.addQuery('sys_id',usersysid);

  gr_user.query();

  if(gr_user.next()){

  ....

  }

}

I also added the UI Script to Service Portal Theme JS Include.

The UI script is called correctly, but the gliderecord is not found (i marked it red). If i call doActionfromUIScript from a UI Action handing over same sys_id it works fine...

Any ideas? Is this a bug? Same user, same rights, same script is called, same parameter: but does not work in SP:

Thx and br

Vesp

3 REPLIES 3

jesseadams
ServiceNow Employee
ServiceNow Employee

Client scripts and ui actions have an isolated runtime from things like the widget's client controller.


Most of the client script apis (GlideRecord, g_form, etc...) are loaded in as part of the sp-model directive used in forms.


As a result those are defined in a completely different scope than your UI script will be if it's loaded with the theme.



Not sure if it will work but you might try passing GlideRecord into your UI script.


Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Vespertinus



Not sure if this can help and probably i'm wrong but in a UI Action you are executing server side code.


This means that GlideRecord will not cause any harm.


On the other hand on the widget client script you're executing a UI script.


UI script expects only pure client so i suppose it won't work.



In order to understand if this is the case I would suggest to provide an hard-coded sys_id first in the call to the function



doActionfromUIScript('your_ID_here');



and than directly inside the UI script.



I hope this will help/answer your question and if it does please mark it



R0b0


Hello R0b0,



thanks for fast reply. I already tried passing hardcoded sysid. It is the correct one, but i am wondering why theme related UI script cannot query the users GlideRecord. The script is executed fine and i can also fire an alert(usersysid); that is passed to the script giving the correct popup in ServicePortal. But after that it does not query the correct record... 😕 I want to avoid to copy all code of the UI scripts to the widget or anywhere else, because its complex and should be maintained in one place...



Thx and br


Vesp