When shoulod "if (isLoading) return" not be added to catalog client scripts

eileenhessmille
Kilo Expert

It is a recommended practice to have "if (isLoading) return;" as the first statement of the onChange functions.   It was suggested to me that there may be exceptions to that rule, but nothing specific was identified. What should I consider before I update 334 of our catalog client scripts?   Thanks!

1 ACCEPTED SOLUTION

Anurag Tripathi
Mega Patron
Mega Patron

Hi Eileen,



Client scripts by default run both on onload and onchange, now if you dont want youer client script to work onload of the catalog item then u can write if(isLoading) return; but if the behaviour demands to be checked onload and onchange both then you cannot.


-Anurag

View solution in original post

4 REPLIES 4

Anurag Tripathi
Mega Patron
Mega Patron

Hi Eileen,



Client scripts by default run both on onload and onchange, now if you dont want youer client script to work onload of the catalog item then u can write if(isLoading) return; but if the behaviour demands to be checked onload and onchange both then you cannot.


-Anurag

Kalaiarasan Pus
Giga Sage

If that is added in a onchange client script, it will not get executed on load. It depends whether you want your onchange client script to run on load of the form or not


Brad Tilton
ServiceNow Employee
ServiceNow Employee

If you include that code, the client script only runs onchange. If it is not there, it will run both when the form loads and the field values change. It works this way because all of the forms are blank and then populated with data when the form loads, so all of the fields are technically changing.



All of that being said, if all of your client scripts are currently working how you want them to I wouldn't update them. The best practice would be to include this statement and not run the onchange on load if you don't have to in order to cut down on load time.



Client Script Best Practices - ServiceNow Wiki


eileenhessmille
Kilo Expert

Thank you all!   I understand that I need to read each script to determine if it needs to be run onLoad. If so, I do not insert the 'isLoading Return' script.   Brad, your answer was extra clear...thanks!