<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: Getting a user object in a UI script in Developer forum</title>
    <link>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695849#M352775</link>
    <description>&lt;P&gt;You can use&amp;nbsp;&lt;STRONG&gt;top.window.NOW&lt;/STRONG&gt; object which has many different useful attribute as below:&lt;/P&gt;
&lt;P&gt;top.window.NOW.instance_name&lt;/P&gt;
&lt;P&gt;top.window.NOW.page_id&lt;/P&gt;
&lt;P&gt;top.window.NOW.portal_id&lt;/P&gt;
&lt;P&gt;top.window.NOW.portal_url_suffix&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="name"&gt;top.window.NOW.user_display_name&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="name"&gt;top.window.NOW.user_id&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="name"&gt;top.window.NOW.user_id_hashed&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="name"&gt;top.window.NOW.user_impersonating&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="name"&gt;top.window.NOW.user_initials&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="name"&gt;top.window.NOW.user_name&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers&lt;/P&gt;
&lt;P&gt;Syed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV id="mstr_highlight_precard" style="position: absolute; left: 0px; top: 0px;"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
    <pubDate>Sun, 14 Mar 2021 22:55:31 GMT</pubDate>
    <dc:creator>syed_faheem</dc:creator>
    <dc:date>2021-03-14T22:55:31Z</dc:date>
    <item>
      <title>Getting a user object in a UI script</title>
      <link>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695837#M352763</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I understand that UI scripts are not *recommended* at this point, however, for what I need to accomplish I believe it's the only way to get it done.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Basically, I need to detect a user's groups in a UI script and hide certain elements on any given page based on the user's groups. These elements are html objects contained within content blocks. Therefore, attaching roles to content blocks will not accomplish what I need to.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It appears that in my UI script the g_user object is not yet defined. Even when calling getActiveUser() it doesn't appear to create the object. g_user.userID returns undefined at that point.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The end goal is for me to be able to call g_user.hasRole() - or at the very least query sys_user_grmember using g_user.userID. I am aware of the possible overhead here, however, I feel like user object/data should be baseline available to us even when using deprecated UI scripts.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is there any workaround for this issue? I am running Eureka.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks &lt;SPAN __jive_emoticon_name="happy" __jive_macro_name="emoticon" class="jive_macro jive_emote" src="https://www.servicenow.com/6.0.3.0/images/emoticons/happy.png"&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Feb 2015 00:48:04 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695837#M352763</guid>
      <dc:creator>Kyle Ketterer1</dc:creator>
      <dc:date>2015-02-18T00:48:04Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a user object in a UI script</title>
      <link>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695838#M352764</link>
      <description>&lt;P&gt;I would also like to add that *strangely enough* when using the code:&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;var g_user = ((g_user == null ) ? getActiveUser() : g_user);&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;alert ( g_user.hasRole('custom_role') );&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;Returns true/false ONLY when this UI script runs from the admin interface. Even stranger, g_user.userID returns undefined!&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;When this UI script runs from the ESS, even calling getActiveUser() does not populate the g_user object. hasRole() is undefined when running on the ESS along with all other properties/methods.&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;Just figured I'd add that in there...&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 18 Feb 2015 01:00:50 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695838#M352764</guid>
      <dc:creator>Kyle Ketterer1</dc:creator>
      <dc:date>2015-02-18T01:00:50Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a user object in a UI script</title>
      <link>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695839#M352765</link>
      <description>&lt;P&gt;I think as a workaround you could use a GlideAjax call and return whatever you need. &lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;Have not personally tried it but should be possible&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 18 Feb 2015 01:33:44 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695839#M352765</guid>
      <dc:creator>ahaz86</dc:creator>
      <dc:date>2015-02-18T01:33:44Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a user object in a UI script</title>
      <link>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695840#M352766</link>
      <description>&lt;P&gt;Ok, tested this in a demo instance... &lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;SPAN style="font-size: 14pt;"&gt;UI Script&lt;/SPAN&gt;&lt;/P&gt;&lt;BR /&gt;&lt;PRE __default_attr="javascript" __jive_macro_name="code" class="jive_text_macro jive_macro_code _jivemacro_uid_14242248052953657" jivemacro_uid="_14242248052953657"&gt;
&lt;P&gt;userHasRole('admin');&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt;function userHasRole(role){&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt; &amp;nbsp; var ga = new GlideAjax('CheckUserRole');&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt; &amp;nbsp; ga.addParam('sysparm_name','u_hasRole');&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt; &amp;nbsp; ga.addParam('sysparm_role',role);&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt; &amp;nbsp; ga.getXMLWait();&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt; &amp;nbsp; alert(ga.getAnswer());&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt;}&lt;/P&gt;&lt;BR /&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;SPAN style="font-size: 14pt;"&gt;Script Include&lt;/SPAN&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;Name: &lt;STRONG&gt;CheckUserRole&lt;/STRONG&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;Client Callable: &lt;STRONG&gt;Checked&lt;/STRONG&gt;&lt;/P&gt;&lt;BR /&gt;&lt;PRE __default_attr="javascript" __jive_macro_name="code" class="_jivemacro_uid_14242248586191676 jive_text_macro jive_macro_code" jivemacro_uid="_14242248586191676"&gt;
&lt;P&gt;var CheckUserRole = Class.create();&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt;CheckUserRole.prototype = Object.extendsObject(AbstractAjaxProcessor, {&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt; &amp;nbsp; u_hasRole: function() {&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt; &amp;nbsp; var role = this.getParameter("sysparm_role");&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt; &amp;nbsp; return gs.hasRole(role);&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt; &amp;nbsp; }&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt;});&lt;/P&gt;&lt;BR /&gt;
&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Feb 2015 02:01:49 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695840#M352766</guid>
      <dc:creator>ahaz86</dc:creator>
      <dc:date>2015-02-18T02:01:49Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a user object in a UI script</title>
      <link>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695841#M352767</link>
      <description>&lt;P&gt;g_user object doesn't seem to work in ui script....&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 18 Feb 2015 06:26:11 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695841#M352767</guid>
      <dc:creator>Kalaiarasan Pus</dc:creator>
      <dc:date>2015-02-18T06:26:11Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a user object in a UI script</title>
      <link>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695842#M352768</link>
      <description>&lt;P&gt;Alexander,&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;I would like to thank you for your solution - it works perfectly. &lt;SPAN __jive_emoticon_name="happy" __jive_macro_name="emoticon" class="jive_macro_emoticon jive_macro jive_emote" src="https://www.servicenow.com/6.0.3.0/images/emoticons/happy.png"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;One caveat to anybody who needs to use this, if you are 'impersonating' a user - this will not work. This will pull roles directly related to your session only.&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;The script can probably be modified to account for this somehow - but it works as it is now.&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;Thanks for the help!&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 18 Feb 2015 16:05:44 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695842#M352768</guid>
      <dc:creator>Kyle Ketterer1</dc:creator>
      <dc:date>2015-02-18T16:05:44Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a user object in a UI script</title>
      <link>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695843#M352769</link>
      <description>&lt;P&gt;EDIT:&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;This works fine while impersonating. Please disregard that statement.&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 23 Mar 2015 18:41:36 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695843#M352769</guid>
      <dc:creator>Kyle Ketterer1</dc:creator>
      <dc:date>2015-03-23T18:41:36Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a user object in a UI script</title>
      <link>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695844#M352770</link>
      <description>&lt;P&gt;Well, my alert always returns "null". &amp;nbsp; &amp;nbsp; Yes, I verified I have the role. &amp;nbsp; Any ideas?&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 10 Jul 2015 14:51:22 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695844#M352770</guid>
      <dc:creator>xiaix</dc:creator>
      <dc:date>2015-07-10T14:51:22Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a user object in a UI script</title>
      <link>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695845#M352771</link>
      <description>&lt;P&gt;I recently encountered the same issue in trying to implement custom dimensions for Google Analytics. I wanted to add a dimension based on user roles. There is a way to do it without a GlideAjax call, though I must say this is about as far into the weeds as you can get from a platform perspective.&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;View the page source within ServiceNow, and about 60 lines down you'll find:&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;PRE __default_attr="javascript" __jive_macro_name="code" class="jive_text_macro jive_macro_code _jivemacro_uid_14406334394463164" jivemacro_uid="_14406334394463164" modifiedtitle="true"&gt;
&lt;P&gt;window.NOW.user = {};&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt;window.NOW.user.preferences = [];&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt;window.NOW.user.roles = 'admin';&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt;window.NOW.user.id = '6816f79cc0a8016401c5a33be04be441';&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt;window.NOW.user.departmentID = 'a581ab703710200044e0bfc8bcbe5de8';&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt;window.NOW.user.firstName = 'System';&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt;window.NOW.user.lastName = 'Administrator';&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt;window.NOW.user.name = 'admin';&lt;/P&gt;&lt;BR /&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;Within your UI Script, you can reference these variables, in particular the &lt;SPAN style="font-family: 'andale mono', times;"&gt;window.NOW.user.roles&lt;/SPAN&gt; variable, to get the list of roles for the current user.&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 27 Aug 2015 00:01:18 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695845#M352771</guid>
      <dc:creator>sshall</dc:creator>
      <dc:date>2015-08-27T00:01:18Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a user object in a UI script</title>
      <link>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695846#M352772</link>
      <description>&lt;P&gt;Hey Scott,&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;It looks like cheat code. But it helped me. Thank you for sharing this.&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 16 Mar 2016 20:07:48 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695846#M352772</guid>
      <dc:creator>jpavanaryan</dc:creator>
      <dc:date>2016-03-16T20:07:48Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a user object in a UI script</title>
      <link>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695847#M352773</link>
      <description>&lt;P&gt;Hey Scott,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;window.NOW.user.roles doesn't work in portal, any guidance?&lt;/P&gt;</description>
      <pubDate>Sat, 30 Jan 2021 07:41:13 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695847#M352773</guid>
      <dc:creator>Rushikesh Mandh</dc:creator>
      <dc:date>2021-01-30T07:41:13Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a user object in a UI script</title>
      <link>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695848#M352774</link>
      <description>&lt;P&gt;try &lt;STRONG&gt;this&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;from &lt;A href="https://snprotips.com/blog/2017/7/21/how-to-enable-dom-manipulation-in-the-servicenow-service-portal" target="_blank"&gt;snprotips&lt;/A&gt;:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="find_real_file.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/201555i3DF67FF1A9294C85/image-size/large?v=v2&amp;amp;px=999" srcset="/community/image/serverpage/image-id/201555i3DF67FF1A9294C85/image-size/large?v=v2&amp;amp;px=999 1.0x" role="button" title="find_real_file.png" alt="find_real_file.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 30 Jan 2021 20:11:47 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695848#M352774</guid>
      <dc:creator>xiaix</dc:creator>
      <dc:date>2021-01-30T20:11:47Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a user object in a UI script</title>
      <link>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695849#M352775</link>
      <description>&lt;P&gt;You can use&amp;nbsp;&lt;STRONG&gt;top.window.NOW&lt;/STRONG&gt; object which has many different useful attribute as below:&lt;/P&gt;
&lt;P&gt;top.window.NOW.instance_name&lt;/P&gt;
&lt;P&gt;top.window.NOW.page_id&lt;/P&gt;
&lt;P&gt;top.window.NOW.portal_id&lt;/P&gt;
&lt;P&gt;top.window.NOW.portal_url_suffix&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="name"&gt;top.window.NOW.user_display_name&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="name"&gt;top.window.NOW.user_id&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="name"&gt;top.window.NOW.user_id_hashed&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="name"&gt;top.window.NOW.user_impersonating&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="name"&gt;top.window.NOW.user_initials&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="name"&gt;top.window.NOW.user_name&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers&lt;/P&gt;
&lt;P&gt;Syed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV id="mstr_highlight_precard" style="position: absolute; left: 0px; top: 0px;"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Sun, 14 Mar 2021 22:55:31 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/1695849#M352775</guid>
      <dc:creator>syed_faheem</dc:creator>
      <dc:date>2021-03-14T22:55:31Z</dc:date>
    </item>
  </channel>
</rss>

