g_user object is not defined error

manikandanvelu
Kilo Explorer

I am trying to get the current user name in my widget's client script, When I try to use 'g_user.firstName' I'm getting this error g_user not defined in the console.

Can anyone share a sample code to get the current username in client script.

Thanks in advance.

6 REPLIES 6

anurag92
Kilo Sage

is it a Portal widget code? or jelly based?


apurvasharma
Kilo Guru

Hi,



If you working on portal widget, then you can fetch the logged in user value such as:



Server Side:- ///Just server side code works and print on HTML such as {{data.sysUserID}}, but if you want to do some work on Client side, then client side code is mentioned


data.sysUserID = gs.getUser().getFullName();


Client Side:-


var c = this;


  c.server.get().then(function(r) {                      


                      c.data.user=r.data.sysUserID});



HTML:- <p>{{c.data.user}}</p>


Dave Smith1
ServiceNow Employee
ServiceNow Employee

Manikandan Velu wrote:


Can anyone share a sample code to get the current username in client script.


Could you share the code you're writing?   Perhaps then people can identify what's causing the error message.


Shahed Shah1
Tera Guru

I've not seen the g_user object work in Service Portal widgets. What I did find is that some user info can be obtained from an object called NOW in the $window service.



There's useful attributes like:


$window.NOW.user_avatar


$window.NOW.user_display_name


$window.NOW.user_id


$window.NOW.user_initials


$window.NOW.user_name



Hopefully this helps you achieve what you need.