Why does g_user not work on UI Page client scripts?

awright
Kilo Expert

I have been working on something to do with a UI Page and I am curious as to why the g_user commands (for example "g_user.userID") are not working in the UI Page Client Script box.

Is there any particular reason for this? I think that knowing the answer to why it works on some client scripts and not others will greatly enhance my knowledge of the platform.

9 REPLIES 9

CapaJC
ServiceNow Employee
ServiceNow Employee

When you view the form for a record, the script that generates the form sets the g_user variable. In a UI Page, there is no such code to do that.

I believe if you add the following to your UI Page, it might add that support to your page (I found this in the personalize_all UI Page):



<!-- emit g_user support -->
<g2:client_script type="user" />


geoffcox
Giga Guru

If you are opening the UI page from a client side script or UI action, you can pass in whatever variables you need.

Here's an example of one of my UI actions that passes several variables to the UI page:



var comment_block = g_form.getValue('u_comments');
var owner_ref = g_form.getReference('assigned_to');
var target_ref = g_form.getReference('u_targeted_release');
var owner_id = owner_ref.sys_id;
var target_id = target_ref.sys_id;
var owner_name = owner_ref.name || '';
var target_name = target_ref.u_name || '';
var gDialog = new GlideDialogWindow("Open Issue");
gDialog.setTitle('Open Issue');
gDialog.setPreference('table','Issue_Open');
gDialog.setPreference('owner_id',owner_id + '' || '');
gDialog.setPreference('owner_name',owner_name + '' || '');
gDialog.setPreference('comment_block',comment_block + '' || '');
gDialog.setPreference('target_id',target_id + '' || '');
gDialog.setPreference('target_name',target_name + '' || '');
gDialog.render();


Then to retrieve these on the UI Page:



<g:evaluate var="jvar_user_sysid" expression="RP.getWindowProperties().get('owner_id')" />
<g:evaluate var="jvar_user_name" expression="RP.getWindowProperties().get('owner_name')" />
<g:evaluate var="jvar_comment_text" expression="RP.getWindowProperties().get('comment_block')" />
<g:evaluate var="jvar_target_name" expression="RP.getWindowProperties().get('target_name')" />
<g:evaluate var="jvar_target_id" expression="RP.getWindowProperties().get('target_id')" />


Then, in the html, you will have variables available to you as shown in the following line:



<tr>
<td>
<g2:ui_reference name="user" table="sys_user" value="${jvar_user_sysid}" displayvalue="${jvar_user_name}" />
</td>
</tr>




awright
Kilo Expert

Hi guys,

Thanks so much for helping me so quickly, not only have you explained exactly what is missing but your solutions are exactly what I need!

In the end I went with CapaJC's option and it worked, the only addition I would make to your info is that the client_script tag should be placed right before the final jelly tag.

I find it interesting how many incredibly useful features like this simply aren't included in the wiki, it would make my day so much easier if it were comprehensive.


geoffcox
Giga Guru

I've tried this solution and it's not working for me and I can't for the life of me see why. Others in our company are doing it successfully, so what the heck?!

Here's the HTML:



<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<!-- emit g_user support -->
<g2:client_script type="user" />
<div id="theResult"></div>
</j:jelly>


And here's the client script:


alert(g_user.userID);


The result is "g_user is not defined".