User without a manager should not be able to submit a catalog form.

rohitreddy
Tera Contributor

Hi,

 

I have two fields on a catalog form:

Requested for - which auto populates for logged in user

request on behalf -  this is used to submit request for some other person.

 

Requirement: If either of them doesn't have a active manager, then a error message should be poped up while submitting the form.

I tried onSubmit with GlideAjax, but it did not.

 

Can you help with code.

2 ACCEPTED SOLUTIONS

HI @rohitreddy ,

try this

Client script

    function onSubmit() {
        if (g_scratchpad.isFormValid)
            return true;
        var actionName = g_form.getActionName();

        var userID = g_form.getValue('requested_for') || g_form.getValue('request_on_behalf_of') || g_user.userID;

        var ga = new GlideAjax('CheckUserManager');
        ga.addParam('sysparm_name', 'hasManager');
        ga.addParam('sysparm_user_id', userID);
        ga.getXMLAnswer(callBackFunc);
        callBackFunc(function(answer) {
            if (answer == 'false' || answer == false) {
                alert("This request requires manager approval, but no manager is currently assigned to the user profile. Please contact the Service Desk team before submitting this request.");
                return false;
            }
            g_scratchpad.isFormValid = true;
            g_form.submit(actionName);
        });
        return false;
    }

 

script include (you have mentioned active manager you are not checking active or not)

var CheckUserManager = Class.create();
CheckUserManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    hasManager: function() {
        var userID = this.getParameter('sysparm_user_id');
        if (!userID) return false;

        var userGR = new GlideRecord('sys_user');
        if (userGR.get(userID)) {
            return userGR.manager && userGR.manager.active; //check manager is active or not
        }
        return false;
    }
});

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

 

View solution in original post

Pranesh072
Mega Sage
Mega Sage
function onSubmit() {
    // Validation for manager presence
    if (g_scratchpad.isFormValid)
        return true;
    var actionName = g_form.getActionName();
    var userID = g_form.getValue('requested_for') || g_form.getValue('request_on_behalf_of') || g_user.userID;

    var ga = new GlideAjax('CheckUserManager');
    ga.addParam('sysparm_name', 'hasManager');
    ga.addParam('sysparm_user_id', userID);
    ga.getXMLAnswer(callBack);

    function callBack(answer) {

        var hasManager = answer;

        if (hasManager == 'false') {
            alert("This request requires manager approval, but no manager is currently assigned to the user profile. Please contact the Service Desk team before submitting this request.");
            return false;
        }

        g_scratchpad.isFormValid = true;
        g_form.submit(actionName);
    }
    return false;
}

View solution in original post

8 REPLIES 8

Hi @Nishant8 

I am trying to validate if they have a active manager or not.

 

Hello @rohitreddy, oh yeah, I misunderstood the ask, my bad. Others have already addressed it, which should work fine. please try.

If you didn't want your code to run in Portal then, even with getXMLWait() too you can get this done. may be a small problem in your SI.

 

Regards,

Nishant

Pranesh072
Mega Sage
Mega Sage
function onSubmit() {
    // Validation for manager presence
    if (g_scratchpad.isFormValid)
        return true;
    var actionName = g_form.getActionName();
    var userID = g_form.getValue('requested_for') || g_form.getValue('request_on_behalf_of') || g_user.userID;

    var ga = new GlideAjax('CheckUserManager');
    ga.addParam('sysparm_name', 'hasManager');
    ga.addParam('sysparm_user_id', userID);
    ga.getXMLAnswer(callBack);

    function callBack(answer) {

        var hasManager = answer;

        if (hasManager == 'false') {
            alert("This request requires manager approval, but no manager is currently assigned to the user profile. Please contact the Service Desk team before submitting this request.");
            return false;
        }

        g_scratchpad.isFormValid = true;
        g_form.submit(actionName);
    }
    return false;
}

rohitreddy
Tera Contributor

t worked. Thank you.