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

How to configure an on submit script include to check if user is in a group

Chris17
Tera Contributor

Hello,

I currently have a requirement where if a catalog item is submitted, a script needs to run to test if a selected user is already in another group. If the user is not in the group, the submit should fail and an error message should appear. I tried to configure it using a glide record, but I understand that is not best practice and am unsure how to configure it as a script include. 

Thank you!

1 ACCEPTED SOLUTION

Sai Kumar B
Mega Sage

@Chris 

do the below onSubmit() script if you wanna try static

function onSubmit(){
if(!g_user.isMemberOf('group name')){ //If logged-in user is not member of the group
g_form.addErrorMessage('Member is not part of the group');
return false;
}
return true;

 

 

 

If you wanna try dynamic do the below approach with GlideAjax()

onSubmit() Client Script

function onSubmit() {
	if (!g_form.ajaxComplete) {
		ajaxCall();
		return false;
	}
}

function ajaxCall() {
	var ga = new GlideAjax('script_include');
	ga.addParam("sysparm_name", 'function_name');
	getXMLAnswer(function(a){
		// Do whatever you want to with the answer from script include
		// Once you've handle the answer, run these two lines:
		g_form.ajaxComplete = true;
		g_form.submit();
	});
}

Script Include

//Script include function

function_name : function(){
var checkUserGroup = new GlideRecord('sys_user_grmember');
//Do your logic

return 'your response';

},

View solution in original post

2 REPLIES 2

Sai Kumar B
Mega Sage

@Chris 

do the below onSubmit() script if you wanna try static

function onSubmit(){
if(!g_user.isMemberOf('group name')){ //If logged-in user is not member of the group
g_form.addErrorMessage('Member is not part of the group');
return false;
}
return true;

 

 

 

If you wanna try dynamic do the below approach with GlideAjax()

onSubmit() Client Script

function onSubmit() {
	if (!g_form.ajaxComplete) {
		ajaxCall();
		return false;
	}
}

function ajaxCall() {
	var ga = new GlideAjax('script_include');
	ga.addParam("sysparm_name", 'function_name');
	getXMLAnswer(function(a){
		// Do whatever you want to with the answer from script include
		// Once you've handle the answer, run these two lines:
		g_form.ajaxComplete = true;
		g_form.submit();
	});
}

Script Include

//Script include function

function_name : function(){
var checkUserGroup = new GlideRecord('sys_user_grmember');
//Do your logic

return 'your response';

},

jonmulherin
Giga Expert

The reply marked as correct works pretty well from Service Portal but not so well from Service Catalog. There it spits out errors about g_form.submit() being useless in a client script.  I found the following works well from Service Catalog:

 

    g_form.clearMessages();
    var answer = '';
    var gajax = new GlideAjax("[Your Script Include]");
    gajax.addParam("sysparm_name", "[Function within script include]");
    gajax.addParam("[Optional other parameter to pass]",[Value to pass]);
    gajax.getXMLWait();  // I know; latency
    answer = gajax.getAnswer();
    if (answer [Your Condition]) {
        g_form.addErrorMessage("[Your Error Message if needed]");
    }
    if (answer [Your Condition])
        return false;
    return true;