Logged in user should be a part of a group then only he can submit the form, other users cannot

Mishu
Tera Expert

Hello Experts,

 

I am trying to achieve this requirement - User should not be able to submit a catalog request if he is not a part of the software group.
Tried with below On submit Client script and Script Include, but its not giving result, Can u plz help whats wrong here ?

 

 

On Submit Client Script :

 

 

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var part = g_form.getValue('name_of_the_partition');
    var reqTyp = g_form.getValue('request_type');


    if ((part == 'major || 'minor') && reqTyp == "software") {
        var ga = new GlideAjax('UserUtils');
        ga.addParam('sysparm_name', 'checkGroupUser');
        ga.getXML(checkGroupUser);
    }

function checkGroupUser(response) {
    var result = response.responseXML.documentElement.getAttribute("answer");
    if (result == 'true') {
        return true;
    } else {
        g_form.addErrorMessage("User not eligible to submit the form");
        return false;
    }
}
}

 

 

Script Include:

 

 

var UserUtils = Class.create();
UserUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkGroupUser: function() {
        
      return gs.getUser().isMemberOf('TMG Catalog Users');
    },
    type: 'UserUtils'
});

 

 

9 REPLIES 9

Sid_Takali
Kilo Patron
Kilo Patron

Hi @Mishu Try below code

function onSubmit() {
    var part = g_form.getValue('name_of_the_partition');
    var reqTyp = g_form.getValue('request_type');

    if ((part == 'major' || part == 'minor') && reqTyp == "software") {
        var ga = new GlideAjax('UserUtils');
        ga.addParam('sysparm_name', 'checkGroupUser');
        ga.getXMLAnswer(function(response) {
            if (response == 'true') {
                return true;
            } else {
                g_form.addErrorMessage("User not eligible to submit the form");
                return false;
            }
        });
        return false; 
    }
    return true;
}

 

var UserUtils = Class.create();
UserUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkGroupUser: function() {
        return gs.getUser().isMemberOf('TMG Catalog Users') ? 'true' : 'false';
    },
    
    type: 'UserUtils'
});

Its not working in the case where user is part of the group, it should allow him to submit.

For users who are not part of the group, its working fine

Amit Verma
Kilo Patron
Kilo Patron

Hi @Mishu 

 

Instead of complicating this requirement with a Script Include, you can create a user criteria for the catalog item. Refer https://www.servicenow.com/community/developer-forum/catalog-form-should-be-submitted-only-by-a-part...

https://www.servicenow.com/docs/bundle/xanadu-servicenow-platform/page/product/service-catalog-manag...

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Yes but I have condition along with that, when request type is software and part is minor or major then only user out of group should not be able to submit it