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

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

sumanta pal
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. Here is a sample code: javascript var userID = g_user.userID; alert("User ID is: " + userID); As for the confirm() function, it is a client-side function and cannot be used directly in a business rule which is server-side. However, you can use GlideDialogWindow or GlideModal to create a confirmation dialog in a business rule. Here is a sample code: javascript var gd = new GlideDialogWindow('glide_confirm_basic'); gd.setTitle('Confirmation'); gd.setPreference('question', 'Are you sure you want to proceed?'); gd.render(); In summary: - To get the current user's ID in a client script, use g_user.userID. - To create a confirmation dialog in a business rule, use GlideDialogWindow or GlideModal.

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

sumanta pal
Kilo Guru

You can use the GlideUser (g_user) object to get the current user's ID in a client script. Here is a sample code:

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


- You cannot directly use the confirm() function in a business rule 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
}
}


- Remember to replace 'u_confirm_flag' with your actual field name.

In summary:

- 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

sumanta pal
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() in a business rule to display a confirmation message to the user. However, it's not exactly the same as confirm() because it doesn't stop the execution of the script and wait for user input.

Here's how you can use these functions:

Client Script:
javascript
var userID = g_user.userID;
alert('Your user ID is: ' + userID);


Business Rule:
javascript
gs.addInfoMessage('Are you sure you want to proceed?');


To summarize:

- Use g_user.userID in a client script to get the current user's ID.
- Use gs.addInfoMessage() in a business rule to display a confirmation message.
- Note that gs.addInfoMessage() doesn't stop the script execution like confirm() does.


nowKB.com