Not allow form to submit

kaushiki berter
Tera Expert

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. 

kaushikiberter_0-1732550955177.png

client script -

function onSubmit() {
    //Type appropriate comment here, and begin script below

    var shortDescription = g_form.getValue('short_description');
    var description = g_form.getValue('description');

    var flag; // Default to false to prevent submission

    alert(shortDescription + " " + description);

    var ga = new GlideAjax('inci');
    ga.addParam('sysparm_name', 'fetch');
    ga.addParam('sysparm_val', shortDescription);
    ga.addParam('sysparm_valu', description);
    if ((!shortDescription) && (!description)) {
        return false;
    }
    ga.getXMLAnswer(function(answer) {
        alert(answer);
        if (answer === 'true') {
            flag = "yes";
            alert(flag);

            return false;

            //return false;
        } else {
            flag = "no";
        }

    });  
Script Include
fetch: function() {
        var o = this.getParameter('sysparm_val');
        var k = this.getParameter('sysparm_valu');
        gs.log('o' + o);
        gs.log('inxxxx' + k);

        var gr = new GlideRecord('incident');
        gr.addQuery('short_description', o);
        gr.addQuery('description', k);
        gr.query();
        if (gr.next()) {
            kt = 'true';
        } else {
            kt = 'false';
        }
        gs.log("true" + kt);
        return kt;


    },
If there is any simpler approach, then kindly let me know else please help me with above client script.
11 REPLIES 11

Thank you surely will think about it

Chaitanya ILCR
Kilo Patron

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

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.

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).

ChaitanyaILCR_0-1732596834124.pngChaitanyaILCR_1-1732596854754.png

ChaitanyaILCR_2-1732596871333.png

 


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

Juhi Poddar
Kilo Patron

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