Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

why ans is returning null?

user_20
Tera Contributor
Script include:
function onSubmit() {
   var g = new GlideAjax('validation');
    g.addParam('sysparm_name','checkLength');
    g.addParam('sd',g_form.getValue('short_description'));
    g.getXMLAnswer(getResponse);
    function getResponse(response){
        var ans = JSON.parse(response);
        alert(ans);    //returning null
        if(ans == "false"){
            g_form.setValue('short_description','');
            return false;
        }
        return true;
    }
}
 
client script:
var validation = Class.create();
validation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkLength: function(){
        var short_d = this.getParameter('sd');
        var flag = true;
        if(short_d.length!=20){
            flag = false;
        }
        return JSON.stringify(flag);
    },
    type: 'validation'
});
3 REPLIES 3

Anil Nemmikanti
Tera Guru
Tera Guru

Hi @user_20 ,

 

GlideAjax will not work with OnSubmit client script. By the time scriptinclude returns record may already submitted. It is the reason.

PrashantLearnIT
Giga Sage

HI @user_20 

 

The reason GlideAjax isn’t suitable for an onSubmit client script is that the Script Include response may arrive after the record has already been submitted.

********************************************************************************************************
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.

********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect


Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************

Sarthak Kashyap
Kilo Sage

Hi @user_20 ,

 

I check your code and I found 2 issues 

1. You can not pass parameter directly sd you have to pass like 

ga.addParam('sysparm_short_desc',"Your desc");

 

2. GlideAjax will not work on OnSubmit Client script.

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak