Restricting form submission for set of users

sanvi
Tera Expert

Hi All,

I have a requirement on a form when the 'Requested For' user tries submitting the form if he has records in a custom table (i.e requested for = assigned to in custom table) then the user should not be able to submit the form.

I have configured a catalog 'On submit' client script and a 'Script include' for the same. 

But here in both the scenarios the user is not able to submit. can someone pls help

1) client script

var user = g_form.getValue('requested_for_M');
var ajax = new GlideAjax('Utils');
ajax.addParam('sysparm_name', 'User');
ajax.addParam('sysparm_reqfor', user);
ajax.getXML(ajaxResponse);
return false;
}
function ajaxResponse(serverResponse){
var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
alert(answer);
if(answer == 'valid'){
alert("action not allowed");
}
}
 

2)Script Include

User: function() {
var add={};
var gr = new GlideRecord('CustomTable');
var usr = this.getParameter('sysparm_reqfor');
gr.addQuery('u_assigned_to', usr);
gr.query();
if(gr.next()){
return 'valid';
}
else{
return 'invalid';
}
},

8 REPLIES 8

AnveshKumar M
Tera Sage
Tera Sage

Hi @sanvi 

If you are using this code in onSubmit client script, it will not work as the ajax.getXML() is Async. You need to modify your code to suit onSubmit client script.

Thanks,
Anvesh

AnveshKumar M
Tera Sage
Tera Sage

@sanvi Change your onSubmit client script the one below and try testing from portal, it should work.

 

function onSubmit() {
    if (g_scratchpad.isFormValidAjax) {
        g_scratchpad.isFormValidAjax = null;
        return true;
    }

    g_scratchpad.isFormValidAjax = false;
    var user = g_form.getValue('requested_for_M');
    var ajax = new GlideAjax('Utils');
    ajax.addParam('sysparm_name', 'User');
    ajax.addParam('sysparm_reqfor', user);
    ajax.getXML(ajaxResponse);
    return false;
}

function ajaxResponse(ajaxResponse) {
    if (answer) {
        if (answer == 'valid') {
            alert("action not allowed");
            g_form.addErrorMessage("action not allowed");
            return false;
        } else {
            var actionName = g_form.getActionName();
            g_scratchpad.isFormValidAjax = true;
            g_form.submit(actionName);
        }
    }
}

 

Please mark my answer helpful and accept as solution if it helped 👍✔️ 

Thanks,
Anvesh

Hi @AnveshKumar M 

 

I am getting error for this line "

g_scratchpad.isFormValidAjax = false;

onSubmit script error: ReferenceError: g_scratchpad is not defined:
function () { [native code] }

 

HI @sanvi 

Are you trying to submit from Portal or Platform UI? If it platform UI we need to change the code, please let me know.

Thanks,
Anvesh