Get record on a Service Portal Widget via client script

jotasolano
Kilo Contributor

Hello,

I am new to SN and have been trying to get a GlideRecord to display as an alert using an HTML Widget on a Service Portal widget.

I was successfully able to load the record, but I get an error message on my console that tells me that I need to use a callback function. I have not been able to correctly create this callback function.

This is my script:

function text() {

  var res = new GlideRecord('incident');

  res.addQuery('number', 'INC0000001');

  res.query(getCallback);

}

function getCallback(res) {

  if(res.next()){

  var shortDescription = res.short_description.getDisplayValue();

  alert(shortDescription);

  } else {

  console.log("not working")

  }

}

text();

I get this error:

Uncaught (in promise) TypeError: Cannot read property 'records' of undefined(…)

at js_includes_sp.jsx?v=04-21-2016_1541&lp=Thu_May_19_12_55_41_PDT_2016&c=2_42:64344

What am I doing wrong?

Thanks for the help!

1 ACCEPTED SOLUTION

nathanfirth
Tera Guru

I would suggest you do your GlideRecord query using server script. Widgets in Service Portal are run as an Angular directive (scoped) so I'm not sure if some of the typical ServiceNow client scripts will run as expected.



Take a look at the widget "Hello World 3", it shows how to call the server script from the client script.


View solution in original post

6 REPLIES 6

nathanfirth
Tera Guru

I would suggest you do your GlideRecord query using server script. Widgets in Service Portal are run as an Angular directive (scoped) so I'm not sure if some of the typical ServiceNow client scripts will run as expected.



Take a look at the widget "Hello World 3", it shows how to call the server script from the client script.


Thank you. This worked!



We want to load data from our own applications into a SP widget. I haven't been able to find any info on how to do this on the SP documentation. However, the unofficial SP documentation that I believe you wrote discusses the use of Dependencies documentation/widget_dependencies.md at master · service-portal/documentation · GitHub  



Would this be a viable approach to loading our own application data?



Thank you again.


Actually, to be more precise, we would like to know if there's a way to develop widgets using an Application scope, rather than the global scope that we see on the widget examples.




Thanks again!


Yes, you can develop a widget inside an application scope. Not all API's work but the majority of them do, so for your case you should be fine.