Determine current user in a Client Script?

dstuart
Kilo Expert

I have need to confirm an operation with the current user, and need to know the current user's ID.

I am familiar with gs.getUserID(), but that appears to be a server-side function, usable only in a business rule.

I also would like to use the confirm() function, which appears a client-side function, usable only in a client script.

So, is any one aware of something similar to gs.getUserID() that will work in a client script? Or something similar to confirm() that will work in a business rule?

Thanks,
Dave

16 REPLIES 16

Rajdeep Ganguly
Mega Guru


Sure, Dave. Here's how you can achieve this:

1. To get the current user's ID in a client script, you can use the GlideUser (g_user) object. Here's a sample code:

javascript
var userID = g_user.userID;
alert("User ID is: " + userID);


2. To use a confirmation dialog in a business rule, you can't directly use the confirm() function as it's a client-side function. However, you can achieve similar functionality by setting up a UI Action with a client-side script that calls the confirm() function and then calls the server-side script based on the user's response. Here's a sample code:

javascript
// Client-side script
if (confirm('Are you sure you want to proceed?')) {
// If user clicked 'OK', call the server-side script
gsftSubmit(null, g_form.getFormElement(), 'proceed');
} else {
// If user clicked 'Cancel', do nothing
return false;
}

// Server-side script
if (typeof window == 'undefined') {
proceed();
}

function proceed() {
// Your business rule code here
}


Remember to check the 'Client' checkbox for this UI Action to make it run on the client-side.

To summarize:

- Use g_user.userID to get the current user's ID in a client script.
- Use a UI Action with a client-side script to create a confirmation dialog in a business rule. The client-side script should call the confirm() function and then call the server-side script based on the user's response.


nowKB.com

Swarup Patra
Kilo Guru

Sure, you can determine the current user in a Client Script in ServiceNow by using the 'g_user' object. Here is a sample code: javascript var user = g_user.userName; alert('Current user is: ' + user); In this code: - 'g_user' is a global object that is available in client-side scripting. - 'userName' is a property of the 'g_user' object that holds the user ID of the current user. So, to summarize: - Use the 'g_user' object in client-side scripting to determine the current user. - Access the 'userName' property of the 'g_user' object to get the user ID of the current user. - You can display the user ID in an alert box or use it in your script as needed. nowKB.com