Need of IsLoading in Onchange client script

Vinu1
Tera Expert

Hi,

I have a requirement a to populate the phone number when user name is changed. Used Onchange cllient script. Here is  script

if (isLoading || newValue == '') {
     return;
  }

 if (newValue){
var req = g_form.getReference('u_caller_id');
g_form.setValue('phone',req.phone);

but when form is loaded the phone number is not populating. so I have modified client script. My question is 

if (isLoading || newValue == '') {
     return;
  } is available in all the onChange Client script? Can anyone please explain the need of this? I am new to the Servicenow.

Thanks,

Vinu.

4 REPLIES 4

Premkumar1
Giga Expert
  • isLoading - identifies whether the change occurs as part of a form load or not.

Please refer this Isloading

simonbergstedt
Tera Guru

 

It checks if the change was done when the form loaded, technically every value on the form changes when it first loads, and you only want it to trigger after the initial loading.

 

In terms of the script I would suggest you put in g_form.addInfoMessage("Phone-script triggered trying to set the value to " + req.phone + " from caller + req); before the setValue-part just to see if the script triggers, if it finds the user and the phone number

 

Coleton
Kilo Guru

To get the phone number, you would want to do a GlideAjax call instead of the getReference function you're using. It'll make this a lot easier to throw user details into a JSON object, pass it back from the Script Include (Server Sided) to the GlideAjax callback function (Client Sided).

This Blog is very helpful in understanding GlideAjax if you have not used it before. The example also illustrates how to pass the information I mentioned above.

Hope this helps resolve the issue at hand, as the isLoading part is not causing the problem.

Shashank_singh9
Tera Expert

Hello Vinu1

 

Isloading is a boolean variable which checks if the changes occured due to form load as well, Because whenever a form is loaded values on the form are changed as the records are loaded into the form.

 

Even One more Interesting Thing I want to tell you,

Your onChange() Client Script can Potentially work as onLoad():-

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

 

Only if you use the isLoading boolean value then onchange client script can work as onLoad as well.

 

Thanks for reading the answer, please provide your inputs/suggestions if any.

Hope you find this article helpful. Don’t forget to Mark it Helpful, if it helps your query.
Thanks,
Shashank Singh