We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

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

13 REPLIES 13

Swarup Patra
Kilo Guru

Yes, you can get the current user's ID in a client script using g_user.userID. This is equivalent to gs.getUserID() in server-side scripting. For the confirm() function, you can use gs.addInfoMessage() or gs.addErrorMessage() in a business rule to display a confirmation or error message to the user. However, these functions do not stop the execution of the script or wait for user input like the confirm() function does in client-side scripting. Here is a summary: - To get the current user's ID in a client script, use g_user.userID. - To display a message to the user in a business rule, use gs.addInfoMessage() or gs.addErrorMessage(). - Note that gs.addInfoMessage() and gs.addErrorMessage() do not stop the execution of the script or wait for user input like the confirm() function does in client-side scripting. nowKB.com

Ramesh Lohar
Kilo Guru

Yes, you can achieve this by using the following methods:

1. To get the current user's ID in a client script, you can use the GlideUser (g_user) object. Here is 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 set a flag in the business rule and then check this flag in a UI action or client script to show the confirmation dialog. Here is a sample code:

In Business Rule:

javascript
current.u_confirm_flag = true;
current.update();


In Client Script:

javascript
if(current.u_confirm_flag == true){
var answer = confirm("Are you sure you want to proceed?");
if (answer){
// proceed with the operation
}
else{
// cancel the operation
}
}


Please note that you need to replace 'u_confirm_flag' with your actual field name.

To summarize:

- Use g_user.userID to get the current user's ID in a client script.
- Set a flag in the business rule and check this flag in a client script to show a confirmation dialog.


nowKB.com

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