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

Gaspy
Tera Contributor

I think the problem might be with your client script. Unless there is another need, you do not need to be setting any variable of "Flag" as true or false. You could do the following

 

 

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 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(canSubmit) 

function canSubmit(answer){
        if (answer) {
            alert("An incident has already been created with this description and short description);
            return false;
        } 
    }};  

 

 

Thank you for your help. Unfortunately, it is not working.

I do agree with some of the other posts - it's not the best requirement as it can lead to a lot of missed errors etc. That said, if you still need to move forward, one way to potentially combat the issue you were running into for the onSubmit client script is to instead do an onChange client script. Would be a little bit of a better experience for the end user and wouldn't allow the submission until fixed. You'd need to create two client scripts and two methods within the script include but it would look something like this (Specific to the description field)

CLIENT SCRIPT

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   // var shortDescription = g_form.getValue('short_description');
    var description = newValue;

    var ga = new GlideAjax('getUserDetails');
    ga.addParam('sysparm_name', 'fetch');
    ga.addParam('sysparm_valu', description);
    
    ga.getXMLAnswer(canSubmit); 

function canSubmit(answer){
        if (answer == "true") {
            alert("An incident has already been created with this description");
            return false;
        } 
}  
}

SCRIPT INCLUDE 
   fetch: function() {
        var k = this.getParameter('sysparm_valu');

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

 

Najmuddin Mohd
Mega Sage

Hi @kaushiki berter ,
I believe there could be a better approach than this. Even a comma, space or a full stop difference will make the form to Submit.

Push back the requirement to the clients.

Regards,
Najmuddin.