logo

NJP

Community Code Snippets: Getting the User's View

Import · Aug 27, 2016 · article

NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.

DIFFICULTY LEVEL: INTERMEDIATE to ADVANCED
Assumes having taken the class SSNF and has good intermediate to advanced level of knowledge and/or familiarity with Scripting in ServiceNow.

I wanted a way to retrieve the current user's view. So, I did my usual digging:

  • Looked for an example in: Script Includes, Business Rules, Client Scripts, etc.
  • Looked on the ServiceNow Community
  • Looked on the Web
  • Desperately asked someone at Accenture for help!

Well maybe not the last one as this wasn't critical! image

Interestingly I found a couple of ways that ServiceNow's developers had adopted, and I found them in the Business Rules!

I thought I would pass these along; in case you were wondering what they were.

I found two.

Method 1: GlideTransaction

What info there is on this function (next to nothing):

GlideTransaction API reference

var viewGlideTransaction = GlideTransaction.get().getRequestParameter("sysparm_view");

However, note: https://support.servicenow.com/kb?id=kb%5Farticle%5Fview&sysparm%5Farticle=KB0716317

Method 2: getGlideURI

What info there is on this function (almost nothing):

Scoped GlideURI API Reference

What is "gs.action"?

var viewGlideURI = gs.action.getGlideURI().getMap().get('sysparm_view');

Both of these will return a null if it is the Default view, otherwise the string name of the view.

So I created a Business Rule to show the actual operation.

Name: View Check

Table: Incident

Active: Checked

Advanced: Checked

When: Before

Update: Checked

Script:

(function executeRule(current, previous /*null when async*/) {

    try {
        var viewGlideTransaction = GlideTransaction.get().getRequestParameter("sysparm_view");
        viewGlideTransaction = global.JSUtil.nil(viewGlideTransaction) ? 'default' : viewGlideTransaction;
        gs.info('---> viewGlideTransaction: ' + viewGlideTransaction);
    }
    catch (err) {
        gs.error('---> ERROR viewGlideTransaction: ' + err);
    }


    try {
        var viewGlideURI = gs.action.getGlideURI().getMap().get('sysparm_view');
        viewGlideURI = global.JSUtil.nil(viewGlideURI) ? 'default' : viewGlideURI;
        gs.info('---> viewGlideURI: ' + viewGlideURI);
    }
    catch (err) {
        gs.error('---> ERROR viewGlideURI: ' + err);
    }

})(current, previous);

Your Business Rule should look like this:

image

Click on the Submit button to save your work.

To test, I simply went into an open Incident, and updated the State (however, any field will do).

My initial results in the System Log were:

image

So the results were both default.

I then changed my Incident form view to Major Incident and updated the location to trigger the BR, and got the following results in the system log:

image

ServiceNow has cleaned it up since I first played with it and instead of null for both values in the first test (in 8/2016); default is correctly retrieved. Looks really nice. image

So, there you go! Two different methods for retrieving the User's current view. I would be interested if you find any others.

For extra interesting reading check this out:

Navigating by URL

Enjoy!

Steven Bell.

If you find this article helps you, don't forget to log in and mark it as "Helpful"!

image

Originally published on: 8-27-2016 04:56 PM

I updated the code and brought the article into alignment with my new formatting standard.

View original source

https://www.servicenow.com/community/developer-blog/community-code-snippets-getting-the-user-s-view/ba-p/2283366