How to check current user details from a UI page

kamal24
Kilo Explorer

Hi,

In a UI page, I need to check whether any user is currently logged in or not. I tried the below things to retrieve the details for checking,

1) I tried using g_form.userName in the client section of the UI Page. But i didn't get the user name.

2) I also tried in the Processing Script section by using the gs.getUserID() and tried to log the result. However here too i'm unable to see user name.

Can anyone please suggest, how should i try to retrieve current user details from a UI page?


Regards,

Kamal

1 ACCEPTED SOLUTION

For future reference if you need to actually get the username of the logged in user, one way to do it is use ${gs.getUser().name} or other properties like:


${gs.getUser().name} // same as username


${gs.getUser().firstName}


${gs.getUser().lastName}


${gs.getUser().fullName}


${gs.getUserID()}



Putting this in your example you gave earlier you would replace "g_user.userName" with "${gs.getUser().name}". Make sure you include the quotes.:



<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">




<html>


<head>


<script>


function myFunction()


{


alert('Hi');


var usr = "${gs.getUser().name}"; //remember quotes are still needed


alert('Bye ' + usr);


document.getElementById('user').innerHTML = usr;


}


</script>


</head>


<body onload="myFunction()">


<div id='user'></div>




</body>


</html>


</j:jelly>


View solution in original post

20 REPLIES 20

Harish Murikina
Tera Guru

Property Summary


PropertyDescription
userNameUser name of the current user.
userIDSys_id of the current user.
firstNameFirst name of the current user.
lastNameLast name of the current user.



Click below link for more info.




GlideUser (g user) - ServiceNow Wiki


Harish Murikina
Tera Guru

1) g_user.userName;


2) g_user.userID;


3) g_user.firstName;


4) g_user.lastName



g_user is client side object we can user details in client side using g_user. client side script are   like ui polcies, clientscript


gs is server side object we can details in server side scripts like Business rule , includescript



Regards,


Harish.


Hello Harish,



Thanks for your response. But the issue which i'm facing is that, In the "Client Script section of the UI page" i already tried using g_user.userName but its not returning any value.



When i tried using g_user.userName in a separate client script, it works fine.


I'm not sure why it is not working in UI page client script section.




Regards,


Kamal


Please send your ui page code.