The Zurich release has arrived! Interested in new features and functionalities? Click here for more

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

BKSharma
Giga 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?

3 REPLIES 3

YaswanthKurre
Giga 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.

Ankur Bawiskar
Tera Patron
Tera Patron

@BKSharma 

your UI action is client side or server side?

Also why not show that UI action only to the user present in Assigned to field

You can add condition in UI action condition field

current.assigned_to == gs.getUserID()

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

ChallaR
Tera Contributor

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