Clear catalog variable field with a script

CCZMAX1
Mega Sage

Hi, I have a multi row variable set with a onChange Catalog client script that runs for a field in the variable set.

 

The script works if the user clicks in another field on the form or anywhere on the form before pressing the Add button.  If they press the Add button first the row it added before the script is fully executed to clear the new ‘New Service’ field if the script returns true. The message appears but the field is not cleared because the row has already been added.

 

I tried using onSubmit but that displays the error message but does not clear the field when the return is true.

 

Does anyone know how I could overcome this.  This is the code I have so far that calls a script includes.

 

Many thannks

Max

 

CCZMAX1_0-1686642382720.png

 

function onChange() 

{

  var new_service = g_form.getValue('new_service');  

    

  var ga = new GlideAjax ('service_and_offerings');

  ga.addParam('sysparm_name', 'check_service_name');

  ga.addParam('sysparm_new_service', new_service);

  

  ga.getXMLAnswer(callbackFunction);

 

function callbackFunction(serviceExists) 

{     

   serviceExists = Boolean(serviceExists);

 

    if(serviceExists === true)

    {

        g_form.clearValue('new_service');

        alert('This service name cannot be created as it already exists');       }

}

}

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

This should work with an onSubmit script.  Make sure you have a line after clearValue:

return false;

 

This does not work. The row is still added.  Need a way to not add the row to the mrv and display the message to the user.

Ideally, it would be great to run a script when the 'Add' button is pressed but I can see on way of doing this

 

Max

You need to use getXMLWait so that the form/dialog submission waits for the return from the server.

function onSubmit() {
	var new_service = g_form.getValue('new_service');  
	var ga = new GlideAjax ('service_and_offerings');
	ga.addParam('sysparm_name', 'check_service_name');
	ga.addParam('sysparm_new_service', new_service);
	ga.getXMLWait();
	var serviceExists = ga.getAnswer();
    if(serviceExists === 'true') {
		g_form.clearValue('new_service');
        alert('This service name cannot be created as it already exists'); //or use g_form.addErrorMessage 
		return false;
	}
}