Require the Catalog Item Submission to Only be done by a Manager

Eric145
Tera Expert

Hello

 

I have a catalog item where managers are supposed to request a PIN number for their employees.

We have an 'Employee' variable.  This variable is the Employee that needs the PIN number.

We also have a 'Submitter' variable.  This variable is automatically filled in based on the user who opened the form using:  javascript:gs.getUser().getDisplayName()

 

We also have three manager fields, that are essentially looking thru the 'employees' info in the sys_user table and dot walking back to see each manager up the leadership chain for that employee (Example: manager > directory > vice pres).

'manager' variable = manager.employee

'manager2' variable = manager.manager.employee

'manager3' variable = manager.manager.manager.employee

 

With all this information, we want to only allow managers for that employee (manager, manager2, manager3) to be able to submit for a PIN for employee.  And if that is not the case pop up an error message and do not allow the form to be submitted.

 

Scenario:

Employee = John Smith

Manager = Joseph Williams (manager)

Manager2 = Jane Doe (director)

Manager3 = David Duck (vp)

 

John Smith needs a new PIN.

John should NOT be allowed to submit for one for himself or others.  And he should be shown an error message and not allowed to submit at all when he attempts to submit one.

John's direct manager is Joseph Williams.  Joseph can submit for a PIN for John Smith.  But Joseph CANNOT submit for other employees who are not under him.

Jane Doe and David Duck can submit a PIN for John Smith as well.  Even though they are higher up in the leadership chain, they are still technically John Smith's boss.

 

Any ideas would be greatly appreciated.

Thanks

1 ACCEPTED SOLUTION

Thanks for your thoughts and ideas Nishant8.  I really appreciate it.

I did end up landing on a simple catalog client script that seems to be doing the trick when I run thru my testing.  

 

All, 

feel free to review and let me know if you see any issues with it.  thanks!

 

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

   //Type appropriate comment here, and begin script below
	var s1 = g_form.getValue('submitter');
	var m1 = g_form.getValue('manager');
	var m2 = g_form.getValue('manager2');
	var m3 = g_form.getValue('manager3');

	if(s1 != m1 && s1 != m2 && s1 != m3)
		{
			g_form.addErrorMessage('You are not the employees manager, so you can not submit the request');
			
			return false;
		}
}

View solution in original post

10 REPLIES 10

priyatam_pvp
Tera Guru

Refer to the below script Include , which can be used for your requirement

var ValidatePINRequester = Class.create();
ValidatePINRequester.prototype = Object.extendsObject(AbstractAjaxProcessor, {

isManagerAllowed: function() {
var employeeSysId = this.getParameter('sysparm_employee'); // Employee selected
var submitterSysId = gs.getUserID(); // Current logged-in user

if (!employeeSysId || !submitterSysId) {
return false; // Prevent errors
}

var userGR = new GlideRecord('sys_user');
if (userGR.get(employeeSysId)) {
var manager1 = userGR.getValue('manager'); // Direct Manager
var manager2 = "";
var manager3 = "";

if (manager1) {
var mgrRecord = new GlideRecord('sys_user');
if (mgrRecord.get(manager1)) {
manager2 = mgrRecord.getValue('manager'); // Director
if (manager2) {
var dirRecord = new GlideRecord('sys_user');
if (dirRecord.get(manager2)) {
manager3 = dirRecord.getValue('manager'); // VP
}
}
}
}

// Check if submitter is in the allowed list
if (submitterSysId == manager1 || submitterSysId == manager2 || submitterSysId == manager3) {
return true;
}
}

return false;
}
});

Please Mark it helful
Regards
Priyatam

Thanks for the reply.

I am sort of new to script includes inside catalog items specifically.

Whats the most appropriate way to launch a script include from a catalog item?  launch from a client script?

Call it from the client script using Glide AJAX

Brian Lancaster
Tera Sage

Sounds like you need an onSubmit script script to check if the Employee variable matches the submitter variables. Something like.

function onSubmit() {
    //Type appropriate comment here, and begin script below
    if (g_form.getValue("submitter") == g_form.getValue("employee")) { //note you need to make sure these match your variables name
        alert("Your manager must submit this request");
        return false; //this will stop the submission
    }
}