Pass data object from server side to client controller in a custom widget
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2019 10:48 PM
I have a requirement to query a table by sys id and the retrieved record should be passed to client controller.
server side:
var rec1 = new GlideRecord('incident');
if(rec1.get(sysid)){
data.sysid = rec1.getUniqueValue();
var abc = rec1;
console.log('record:'+JSON.stringify(rec1)+'number:'+rec1.number);
}
I am able to print individual record values like number, sys_id, status etc.m however I couldn't print entire glide record. It is giving null values.
record: number:{},sys_id{},state:{}
number: 123456.
Is there any way we can retrieve the record?
- Labels:
-
Service Portal Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2019 11:00 PM
Try this
In server script
var data.rec1 = new GlideRecord('incident');
In client script something like this:
$scope.number = c.data.rec1.number;
variables in "data.xxx" setup in server script can be referenced in the client script as "c.data.xxx"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2019 11:00 PM
Hi,
These links should help you; I don't think Gliderecord object you can print
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2019 05:12 AM
I'm not sure how to explain why if you send over the GlideRecord it ends up being empty but I think it has to do with it being a constructor (class) type object.
Also when passing data around many feel that it's best to pass as little data as possible to keep things optimized. So, unless you're trying to bring over the meta data and every thing that deals with that record (even data that the end user doesn't need) then it's best to just capture the fields that usually display value.
Service Portal has a few methods that help minimize the burden of trying to list all the fields that may be necessary.
For instance lets take the example of the incident record. We can take advantage of some things we know about it like baseline (OOB) it has a "Major incident" view and a "self-service" (ess) list view.
If I wanted to get an incident record with the same fields for the ess view I could grab the fields that are configured to display native side and use that in Service Portal.
var essFields = $sp.getListColumns('incident', 'ess'); // brings back a list of viewable fields for ess users on incident
Now lets build our object with the help of your GlideRecord script
var essFields = $sp.getListColumns('incident', 'ess');
var rec1 = new GlideRecord('incident');
if(rec1.get(sysid)){
data.essInc = {};
$sp.getRecordValues(data.essInc, rec1, essFields);
console.log(data.essInc)
data.essInc.sys_id = rec1.getUniqueValue(); //add fields that aren't in the view
console.log(data.essInc) //now sys_id is included with object
}
Or what if I had a condition to display the major incident view when it's a major incident I could either one without having to manually list different fields/columns
var fields = "";
var rec1 = new GlideRecord('incident');
if(rec1.get(sysid)){
//if priority 1 use major incidents view else use ess view
fields = rec1.getValue('priority') == 1 ? $sp.getListColumns("incident", "Major incidents") : $sp.getListColumns("incident", "ess");
//create empty object literal
data.inc = {};
//use GlideRecord and list of fields to populate the empty object
$sp.getRecordValues(data.inc, rec1, fields);
console.log(data.inc)
//add extra fields if needed
data.inc.sys_id = rec1.getUniqueValue();
console.log(data.inc) //now sys_id is included with object
}
In the client controller it can now be accessed via: c.data.essInc, this.data.essInc, c.data.inc or this.data.inc respective to the scripts above and then dot walk out to the property
For example
c.data.essInc.number
Since the examples above are leveraging the server data object it's also available in $scope and therefore accessible straight from the HTML template as well:
<div> Number: { { data.essInc.number } } </div>
Note: In case they don't display there are double curly braces around "data.essInc.number" in the HTML template example. The forum sometimes strips them out
New views could be made as well to support your needs.
