The CreatorCon Call for Content is officially open! Get started here.

How to prevent user from submitting a form unless they are in the global group

HaniyaH
Tera Contributor

Hi,

I have a requirement that the user needs to not be able to submit a catalog item unless they are in the Global Domain group because they have to be in the group first to be able to submit it, if they are not in the Global group then they get an alert upon submission.

 

I have made an onSubmit catalog client script and a script include, but its not working. The alert keeps coming up even when the requester is in the global group. What am I doing wrong?

 

catalog client script

function onSubmit() {
    //get the value of requested for variable
    var requestedFor = g_form.getValue('requested_for');

    var ga = new GlideAjax('CheckGlobalAccount');
    ga.addParam('sysparm_name', 'userHasGlobal');
    ga.addParam('sysparm_user', requestedFor);

    ga.getXMLAnswer(function(answer) {
        if (answer != 'true') {
            alert('The "Requested for" user must have a global account before submitting this request');
            return false; //prevent submission
        } else {
            g_form.submit(); //continue submission
        }
});
	
	return false; //stop default submit
}

 

script include

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

userHasGlobal: function(){
	var userSysId = this.getParameter('sysparm_user');

	var grUser = new GlideRecord('sys_user_grmember');
	grUser.addQuery('user', userSysId);
	grUser.addQuery('group.name', 'GLOBAL Domain');
	grUser.query();

	if(gr.next()){
		return 'true';
	}
	return 'false';
}
    //type: 'CheckGlobalAccount'
});

What am I doing wrong here?

3 REPLIES 3

Bert_c1
Kilo Patron

Assuming you have a group named "GLOBAL domain' then the line:

	grUser.addQuery('group.name', 'GLOBAL Domain');

 needs to be

	grUser.addQuery('group', 'GLOBAL Domain');

in the script include, the column is named 'group'.

HaniyaH
Tera Contributor

Hi @Bert_c1  no this did not work. I did find something regarding using getXML and how you can't use that in onSubmit client scripts - How to do async validation in an onsubmit client script. - Support and Troubleshooting so I have amended both my client script and script include and it is working now for me. Thanks for looking into it 🙂

https://devXXXXXX.service-now.com/sys_script_client_list.do?sysparm_query=sys_class_name%3Dsys_script_client%5EscriptLIKEgetXMLAnswer%5Etype%3DonSubmit&sysparm_view= 

shows 22 OOB client scripts that use 'getXMLAnswer'

 

I am happy you found a solution.