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

Prince Arora
Tera Sage
Tera Sage

@jmiskey ,

 

As you know GlideAjax(getXML) performs a async call to server side, so for making the call and getting the response it is time taking process.

Till the time it is getting response from Server end, form has been submit and we are getting the response after the form submission. 

 

Can you try with  "ga.getXMLWait()" instead of getXML because as per the logic ga.getXMLWait() is working synchronously.

 

Please find the link here for more information.


If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

I would LOVE to be able to handle it directly in the onChange script!  But I don't see how we it can be done, to avoid the scenario I talked about.  Can you tell me how we can avoid that scenario, where they go right from the Group Name field to the Submit button?  I am guessing it is the same issue, in which the Submit button is submitting before the onChange is getting fired (the "onChange" also uses a Glide Ajax call - not sure there is any way to look up the Group table from a Catalog Client Script without one).

 

Is there a way to put a delay in the submission, or something like that, so that we can ensure that the onChange scripts runs before the record is submitted?

@jmiskey ,

 

Have you tried "ga.getXMLWait()" instead of getXML,

 

Please check the logic of "ga.getXMLWait()" from official docs and give it a try!

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

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.