Onsubmit client script

surya123
Mega Guru

HiAll

I am writing a onsubit client script where i want to check if the fields are populated with correct values or not before submitting the form.

Reason - we have onchange client script but we saw that sometimes the value gets corrupted or beomes undfined on submit and it creates an issue..

function onSubmit() {

var user = g_form.getValue('user');   //variable name. I am fetching the values on user table to populate 2 fields on catalog item.

var scriptInclude = new GlideAjax("UserData");

scriptInclude.addParam('sysparm_name','data');

scriptInclude.addParam('sysparm_id',user);

scriptInclude.getXMLWait(retrieveValues);

    //Type appropriate comment here, and begin script below

   

}

function retrieveValues(response){

var answer = response.responseXML.documentElement.getAttribute("answer");

var Value = answer.split(',');

var m,n,s;

m =g_form.getValue('userd');

n =g_form.getValue('manager');

s =g_form.getValue('department');

alert(m +Value[0].toString());

alert(Value[1].toString());

alert(Value[2].toString());

if(m == Value[0].toString() && n ==Value[1].toString() && s==Value[2].toString())

{

return true;

}

else{

alert("Please re-enter the User again");

return false;

}

}

This submits and i dnt see any alert. how can i match what was entered on form and the user value fetched.

1 ACCEPTED SOLUTION

bernyalvarado
Mega Sage

Hi Surya,



That looks like asynchronous call instead of a synchronous one, which is what you need at a onSubmit script.



getXMLWait shouldn't be taking a callback method as a parameter



Thanks,


Berny


View solution in original post

7 REPLIES 7

You're welcome Surya. I'm glad it was helpful


bernyalvarado
Mega Sage

Also, take a look to the very popular way to do a hybrid client + server side code in one ui action. That's great for doing validations when needed on the client side



https://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/



Thanks,


Berny


bernyalvarado
Mega Sage

I hope this helps!