Catalog Client Script to Prevent Submission of a Catalog Item

jmiskey
Kilo Sage

I have a Group Maintenance Form Catalog Item that I created that users can use to request the set up of a new group or change the name of an existing group.  I am trying to prevent them from creating a new group with the same name as an existing group, or to change the name of a group to a name that is already being used.

At first, I just used an "OnChange" Catalog Client Script to check/verify the name.  And those work well. 

 

However, I discovered a loophole.  If the new name field is the last field that they fill out, and they go right from there and click on the Submit button without moving off of that new name field first, it will not clear the value and submit it with the bad name.  So I tried also creating an "onSubmit" script that performs the same actions.  But, really bizarrely, it seems to run, clear out the value from the screen (and this field is a required field), but then it STILL submits the requests with the bad name value. 

 

I am not sure what I am doing wrong.  Can someone look at my code below, and see what the problem is?

jmiskey_0-1679420264628.png

Script:

 

function onSubmit() {

	var chk = true;
	var msg = '';
	
	var grpName1= g_form.getValue('new_group_name'); //get change group name
	var grpName2= g_form.getValue('change_group_name'); //get change group name

	//see if New Group Name has value
	if(grpName1!=''){
		var ajax = new GlideAjax('global.GroupUtils');  
		ajax.addParam('sysparm_name', 'doesGroupNameExist');  
		ajax.addParam('sysparm_groupname', grpName1);
		ajax.getXML(checkGroupName1);  
	}

	//see if Change Group Name has value
	if(grpName2!=''){
		var ajax1 = new GlideAjax('global.GroupUtils');  
		ajax1.addParam('sysparm_name', 'doesGroupNameExist');  
		ajax1.addParam('sysparm_groupname', grpName2);
		ajax1.getXML(checkGroupName2);  
	}

	function checkGroupName1(response){  
		var answer = response.responseXML.documentElement.getAttribute("answer"); 
		if(answer=='true'){
			//clear value and alert user
			g_form.clearValue('new_group_name');
			msg = grpName1 + ' is already use, please choose a different Group name!';
			chk = false;
		}
	}

	function checkGroupName2(response){  
		var answer = response.responseXML.documentElement.getAttribute("answer"); 
		if(answer=='true'){
			//clear value and alert user
			g_form.clearValue('change_group_name');
			msg =  grpName2 + ' is already use, please choose a different Group name!';
			chk = false;
		}
	}

	//Return message if there is one
	if(chk == false){
		alert(msg);	
		return chk;
	}
	
}

 

I know the code is running, I get the alert, and it appears to remove the value from the screen, but it still submits the request with the bad value. It should prevent the request from being submitted (especially since the field it is clearing is a required field).

 

1 ACCEPTED SOLUTION

Unfortunately, I could not get it to work.  I started to receive errors on the Console when I tried changing "getXML()" to "getXMLWait".

 

So we went with a different approach.  I added a verification check box that before Mandatory once the New Group Name field is filled out (it is read-only before then). So they check this field until they have already entered in the Group Name.  So that, in effect, forces the "onChange" script to run before they can Submit the request.

 

Kind of a round-about manner, but it works!

 

Thanks for your efforts.

View solution in original post

6 REPLIES 6

bharathigur
Tera Contributor

Can you explain me in detail how did you solve that issue

 

I added a check box (named chk_new_group_name_change).  I then created a Catalog UI Policy so that it only becomes visible when the Change Type field is set to "Change Group Name".  Though it becomes visible then, it is set to Read-Only so they cannot update it just yet:

jmiskey_0-1733232778722.png

I then added another Catalog UI Policy that makes it required once they fill out the Mandatory New Group Name field:

jmiskey_1-1733232923655.png

So these steps ensure that they have to do something after entering the new name that they want to give the Group, which allows the "onChange" Catalog Client Script for the new group name field to run and verify it is not already being used.