getReference() call back function

vai
Kilo Contributor

I have a scenario where i need to get the values from a reference field popup in clientscript during onsubmit of form.

below is my code

the issue is before the ValidateCus function completed its execution, the if condition of if(validationFailed == true) is executed and as the initial value of variable 'validationFailed' is false the form is submitted.

var x = g_form.getReference('inv_customer', validateCus);

var validationFailed = false;

function validateCus(customerInfo)

{

if(g_form.getValue('fm_cst') == customerInfo.cst)

{

validationFailed = true;

}

}

if(validationFailed == true)
{
return false;
}
else
{
return true;
}

 

1 ACCEPTED SOLUTION

Gosia Urbanska
Giga Guru

From wiki

https://developer.servicenow.com/app.do#!/api_doc?v=kingston&id=r_GlideFormGetReference_String_Funct...

getReference(String fieldName, Function callBack)

Returns the GlideRecord for a specified field.

If a callback function is present, this routine runs asynchronously, and browser (and script) processing will continue normally until the server returns the reference value, at which time the callback function will be invoked. If a callback function is not present, this routine runs synchronously and processing will halt (causing the browser to appear to hang) while waiting on a server response.

 

In your case since this is asynchronous call onSubmit script will not work; Try to move your validation to onChange

 

 

View solution in original post

6 REPLIES 6

Bharath40
Giga Guru

Change it to On change or try below link if you want it as asynchronous

https://snprotips.com/blog/2018/10/19/synchronous-lite-onsubmit-catalogclient-scripts 

Gosia Urbanska
Giga Guru

From wiki

https://developer.servicenow.com/app.do#!/api_doc?v=kingston&id=r_GlideFormGetReference_String_Funct...

getReference(String fieldName, Function callBack)

Returns the GlideRecord for a specified field.

If a callback function is present, this routine runs asynchronously, and browser (and script) processing will continue normally until the server returns the reference value, at which time the callback function will be invoked. If a callback function is not present, this routine runs synchronously and processing will halt (causing the browser to appear to hang) while waiting on a server response.

 

In your case since this is asynchronous call onSubmit script will not work; Try to move your validation to onChange

 

 

my concern is i have 6 text fields on which the validation needs to be performed, do I need to have 6 on change client scripts ?

Unfortunately onSubmit and Ajax/getReference with a callback do not really work together because the answer usually would come back after the script is completed.