The CreatorCon Call for Content is officially open! Get started here.

Compare Login User with the Assigned To reference field in the UI Action button

BKSharma
Tera Contributor

Hi,

I'm working on some UI Action. In the RITM form I've a UI Action button.

My requirement is to when a user click on that button, I need to validate, if the logged in user is the user mentioned in the assigned_to field or anyother user.

How I can validate this?

1 ACCEPTED SOLUTION

YaswanthKurre
Tera Guru

Hi @BKSharma ,

 

You can use this script in UI action field.

 

var assignedTo = g_form.getValue('assigned_to');
var loggedInUser = g_user.userID;

if (assignedTo === loggedInUser) {
    alert("You are the assigned user.");
} else {
    alert("You are not the assigned user.");
}

 

Mark this as helpful and correct if this solves your question.

 

Thanks,

Yaswanth.

View solution in original post

12 REPLIES 12

Hi @Ankur Bawiskar ,

How I can integrate that in the same UI Action, as everything is working fine. I just need one more && statement in the Else part, where If the logged in user is part of Assignment group, then it'll go through the Else Part. 
Mainly, I'm checking if the Assigned to user is the logged in user, then it'll follow the If condition, and Else if the user is Part of the Assignment group then it'll follow the Else Part.

@BKSharma 

it seems your question is still unanswered as your above script is not working.

I shared approach below.

Please mark appropriate response as correct.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

ChallaR
Mega Guru

HI @BKSharma ,

 

option 1 -

you can use UI action (client side) -

current.assigned_to == gs.getUserID()

else option 2-

Validate in UI Action Script (Server-side) -

if (current.assigned_to != gs.getUserID()) {
gs.addErrorMessage("You are not authorized to perform this action. Only the assigned user can proceed.");
action.setRedirectURL(current); // Stay on the same record
return;
}

// Proceed with your logic here

 

 

Option 3: Client-side Validation (if using Client Script or GlideAjax) -

if (g_user.userID !== g_form.getValue('assigned_to')) {
alert("You are not authorized to perform this action.");
return false;
}

 

Please NOTE -

 

  • Use gs.getUserID() for server-side user ID.
  • Use g_user.userID for client-side user ID.
  • Use current.assigned_to or g_form.getValue('assigned_to') to get the assigned user.

Please make as correct and close the thread if this resolving  your query .

 

Thanks,

Rithika