Clear catalog variable field with a script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2023 12:48 AM
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
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'); }
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2023 06:33 AM
This should work with an onSubmit script. Make sure you have a line after clearValue:
return false;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2023 08:31 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2023 08:48 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2023 05:51 AM
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;
}
}