Not allow form to submit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-25-2024 08:16 AM
My requirement is to not allow users to submit the record producer form if already same short description and description has been entered in previous requests. I have written on submit client script where i have made a glideajax call in order to receive data from server which is checking whether the incident is having same combination of short description and description so everything is working fine but I am not able to execute return false once conditions match from the existing data and hence in all cases the form is getting submitted.
client script -
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-25-2024 10:04 AM
Thank you surely will think about it
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-25-2024 09:17 AM
Hi @kaushiki berter ,
the form will get submitted since you are using the async call to get the data by the time server sends the data the next actions will take place.
1. Better use a before insert BR for this with setAbortAction.
2. You can directly use the GlideRecord inside the client script which is not recommended and might not work in scoped applications.
3. You can use the getXMLWait method (might not work in the portals)instead of getXMLAnswer
Regards,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-25-2024 10:08 AM
Thank You for your thoughts. Required to implement solution with best coding practices. Only issue lies with return false statement which is not working in the response handling function. Also, I cannot use Business rules on record producer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-25-2024 09:16 PM
Hi @kaushiki berter ,
You can use the before insert BR on the target record producer's task table (looking at your question I think it's incident table).
Alternative solution
you can create a new variable preferably checkbox or yes/no filed (found_duplicate) on the record producer with default value as (true/yes). On change of the variables that are mapped to short_desc and description changes create a client script with Ajax call to queries the task table to lookup duplicates if the duplicate is found leave the found_duplicate variable a true else update it as false.
and on the onsubmit client script return false if found_duplicate is true and allow them to submit if found_duplicate is false
Regards,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-25-2024 09:59 PM
Hello @kaushiki berter
- Since ga.getXMLAnswer is asynchronous, the return false inside it does not stop the form submission.
Synchronous GlideAjax (getXMLWait) ensures the client waits for the server's response before proceeding, eliminating asynchronous issues.
- Here’s how to fix it and simplify your script:
Client script:
function onSubmit() {
var shortDescription = g_form.getValue('short_description');
var description = g_form.getValue('description');
if (!shortDescription || !description) {
g_form.addErrorMessage('Short description and description are required.');
return false;
}
var ga = new GlideAjax('Inci');
ga.addParam('sysparm_name', 'fetch');
ga.addParam('sysparm_val', shortDescription);
ga.addParam('sysparm_valu', description);
var response = ga.getXMLWait();
if (response.documentElement.getAttribute('answer') === 'true') {
g_form.addErrorMessage('A record with the same short description and description already exists.');
return false;
}
return true;
}
Script include:
var Inci = Class.create();
Inci.prototype = Object.extendsObject(AbstractAjaxProcessor, {
fetch: function() {
var gr = new GlideRecord('incident');
gr.addQuery('short_description', this.getParameter('sysparm_val'));
gr.addQuery('description', this.getParameter('sysparm_valu'));
gr.query();
return gr.hasNext() ? 'true' : 'false';
}
});
Note: Make sure script include is client callable.
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar