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

Peter Bodelier
Giga Sage

Hi @kartikey 

 

You will have to abort the form submission, while te async function runs, and trigger form submission again after it has validated correctly.

 

 

 

var dob = g_form.getValue('date_of_birth');
if (g_scratchpad.isFormValid)
	return true;

var ga=new GlideAjax('CallerDeatils');
ga.addParam('sysparm_name','getAge');
ga.addParam('sysparm_dob',dob);
ga.getXML(callBackFunction);
//then return false to halt submission (for now)
return false;

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;
}
	   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.

Hi Peter,


This is still not working, tried after making the above modification. the form is still submitting.

Hi @Peter Bodelier ,

it does work with onSumit to stop the submission of the form BUT only if greater than 21 age it submitted first via the variable set 

kartikey_0-1707742961823.png

but if i input less than 21 i.e. test 2 record submitted first, it does not work
iterating only once on OnSubmit script

function onSubmit() {


    if (g_scratchpad.isFormValid)
    return true;

   //Type appropriate comment here, and begin script below
   var multiRowVariableSet = JSON.parse(g_form.getValue('dependants_please_include_all_information_required'));
for(var i = 0 ; i < multiRowVariableSet.length ; i ++){
    //alert(multiRowVariableSet[i].date_of_birth);

  var dob = multiRowVariableSet[i].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);
    }

}
}

 

 

Hi @kartikey 

 

I'll try to rebuild your exact use case, and will get back to you.


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