Restricting submission of a form to managers

Community Alums
Not applicable

Hi,

 

I have a catalog item that users should only be able to submit if they are a manager of the user. I want to enable a pop-up if the opened by is not the requested for's manager to tell them they can't, and cancel submission.

 

Can anyone help me with how to go about this?

 

Many thanks

 

 

1 ACCEPTED SOLUTION

Hi @Community Alums  , please update the script as below.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   //Type appropriate comment here, and begin script below
	var requestedFor=g_form.getValue('requested_for');
	var loggedInUser = g_user.userID;
	var user = g_form.getReference('requested_for',doAlert);
	function doAlert(user)
	{
		var manager =user.manager;
		
	if(loggedInUser != manager)
		{
			g_form.addErrorMessage('You are not user manager so can not submit the request');
		}
	}
}

Thanks,

Pooja M

View solution in original post

8 REPLIES 8

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

They way you suggested is more reactive, person will fill the whole form and when they hit submit you tell them they cant, that is not a very good User Experience.

 

I would suggest you do it this way.

When someone opens the form, they only see the people for who the logged in person is the manager

You can add a reference qualified in the Requested For field to only show the users who have logged in User as their manager. If you want you can add a note also that the field will show your team only.

Here is a simple Variable and reference qualifier to help you achieve the same.

AnuragTripathi_0-1676567925890.png

 

 

 

-Anurag

Community Alums
Not applicable

thanks, I get what you mean, but the requirement is to allow them to see the users and select, rather than restricting and then they don't know why they don't see the users and don't know why. It's the first field on the form so won't affect User experience too much, hopefully!

Prasad Dhumal
Mega Sage
Mega Sage

 

Hello,

You can configure OnSubmit Catalog Client Script for this.

function onSubmit() {
  var requestedFor = g_form.getValue('variables.requested_for');//Please check the name of variable
  var requestedForManager = new EfficientGlideRecord('sys_user');
  requestedForManager.addQuery('user_name', requestedFor);
  requestedForManager.query();

  if (requestedForManager.next()) {
    var manager = requestedForManager.getValue('manager');
    var openedBy = g_form.getValue('opened_by');

    if (openedBy != manager) {
      alert('You can only submit this request if you are the manager of ' + requestedFor);
      return false; // cancel submission
    }
  }

  return true; // allow submission
}

 

 

Community Alums
Not applicable

Thanks Prasad, unfortunately this doesn't work, the EfficientGlideRecord throws an error.