Record view count - Best Approach
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2011 01:09 PM
I'm looking to have an integer that shows how many times a record has been viewed. What's the most efficient way of achieving this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2011 09:37 PM
So I put this inside a display BR:
current.u_view_count +='1';
current.update();
The result was that the form would not load at all. Am I doing something wrong?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-30-2011 05:47 PM
Here's what I ended up doing:
Business Rule
When: Display
Condition: current.isValidRecord()
Script:
var usr = gs.getUserDisplayName();
var time = gs.nowDateTime();
var incID = current.sys_id;
var incREC = new GlideRecord('incident');
incREC.addQuery('sys_id',incID);
incREC.query();
if(incREC.next()){
incREC.u_view_count+=1;
incREC.u_internal_notes =usr+" viewed this record at "+time;
incREC.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-30-2011 09:40 PM
Interesting that you had to get the GlideRecord that way vs. just using current. Maybe something about updating current in a "display" rule is bad.
Anyway, if you're intsrested, you might do the record "get" with fewer lines as follows:
var incREC = new GlideRecord('incident');
if (incREC.get(current.sys_id))
incREC.u_view_count += 1;
incREC.u_internal_notes = gs.getUserDisplayName() + " viewed this record at " + gs.nowDateTime();
incREC.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-30-2011 11:22 PM
This is very interesting, I've never seen the "get" option anywhere before... Is this on the wiki anywhere? Does this reduce the query time a normal GlideRecord does?
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2011 08:50 AM
I think we have an API page on GlideRecord, but I've not looked in awhile. gr.get(id) is useful if you're going after one record. It also returns a boolean, so you can do the following in a function to get out early if there's no record:
if (!gr.get(id))
return;
And it's PROBABLY doing less work than using addQuery, query, next - though unless you're talking about something happening millions of times a day, I doubt the difference is of any significance.