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.

On Change Catalog client script not working

kartikey
Tera Contributor

Hi Everyone,

i've created an OnChange client script on a variable set, the script should return false if the age (through DOB is more than 21 )and should not allow the submission of the record producer, but return false; is not working.

the script throws alert if age is more than 21 , but it still submits the form, i want to prevent form submission.

 

var dob = g_form.getValue('date_of_birth');
  // alert(dob);

   var ga=new GlideAjax('CallerDeatils');
    ga.addParam('sysparm_name','getAge');
    ga.addParam('sysparm_dob',dob);
    ga.getXML(callBackFunction);

    function callBackFunction(response){
        var answer=response.responseXML.documentElement.getAttribute('answer');
// alert(answer);
if(answer>21){
    alert(" example alert");
    //g_form.clearValue('date_of_birth');
    return false;
}
   
}
 
 
if i use g_form.clearValue('date_of_birth');
it works but if i select 'add'  after manually typing the date,
kartikey_0-1707738209923.png

 

it throws the error but takes in the value under the variable set.

any support would be much appreciated.

1 ACCEPTED SOLUTION

Hi @kartikey,

 

I've tested below script as onSubmit script on the variable set as working in my instance:

So this will work for every row submitted in your MRVS separately. It will not work in this form on the catalog item.

 

 

function onSubmit() {

    if (g_scratchpad.isFormValid)
        return true;

        var dob = g_form.getValue('date_of_birth');
        alert(dob);

      var ga = new GlideAjax('CallerDeatils');
        ga.addParam('sysparm_name', 'getAge');
        ga.addParam('sysparm_dob', dob);
        ga.getXML(callBackFunction);
	    return false;

	}

        function callBackFunction(response) {
           var answer = response.responseXML.documentElement.getAttribute('answer');
               if (answer > 21) {
                g_form.addErrorMessage('test');
                return false;
            }
            var actionName = g_form.getActionName();
            g_scratchpad.isFormValid = true;
            g_form.submit(actionName);
        }

 


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

7 REPLIES 7

Hi @kartikey,

 

I've tested below script as onSubmit script on the variable set as working in my instance:

So this will work for every row submitted in your MRVS separately. It will not work in this form on the catalog item.

 

 

function onSubmit() {

    if (g_scratchpad.isFormValid)
        return true;

        var dob = g_form.getValue('date_of_birth');
        alert(dob);

      var ga = new GlideAjax('CallerDeatils');
        ga.addParam('sysparm_name', 'getAge');
        ga.addParam('sysparm_dob', dob);
        ga.getXML(callBackFunction);
	    return false;

	}

        function callBackFunction(response) {
           var answer = response.responseXML.documentElement.getAttribute('answer');
               if (answer > 21) {
                g_form.addErrorMessage('test');
                return false;
            }
            var actionName = g_form.getActionName();
            g_scratchpad.isFormValid = true;
            g_form.submit(actionName);
        }

 


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

perfect! Works like a charm! ðŸ˜„
but i observed that we need to click 'add' twice in order to add the date of birth variable , not sure why.

SunilKumar_P
Giga Sage

Hi @kartikey, as per my knowledge return false works only on OnSubmit client script but not on the OnChange client script. Also, the GlideAjax (async) may not work with OnSubmit client script.

 

Ref: https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0783579 

 

Regards,

Sunil