- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2017 09:16 AM
Hi,
I'm fixing some ACE report codes, most of them the used wrong the getReference method without a callback function... I've noticed that on service now wiki they use in this way(they removed the isLoading condition):
I've been using this on my scripts to fix the ace report bad practice issue, however I would like to know if keeping the following statement will affect the script, does this do any change?
if (isLoading){
return;
}
Solved! Go to Solution.
- Labels:
-
Best Practices
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2017 09:27 AM
you absolutly can remove that and the if newvalue == '' line depending on what the code is doing/for...
the onloading section prevents the code from running just because the page loaded <technically the value DID change on load>
the newvalue test stops the code if the value is blanked out...
again examine what you WANT the code to do.. if it SHOULD run on load before they have a chance to touch the value take it out... however normally it is best to leave that in so the page loads quicker.. same with the new value test.. if a blank value requires an action remove it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2017 09:30 AM
It depends on case to case, Basically onchange script runs onload and aon change both, hence the extra load on form load time. But in the script if there is nothing that you need to run onload then you can return on isLoading to save the loadtime.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2017 06:58 AM
Thank you all.
I have another doubt, I've noticed that on some of the ACE report issues there are onLoad client scripts that uses the same parameters as OnChange.... this for me is kind of weird. If you check the following code the call back function is inside the Onload so... my first thought was to place it out however noticed that the "onLoad" function contains a isLoading and the same paremeters as an Onchage...that being said I'd like to now if keeping as standard servicenow will not affect the actual behavior or generate an ACE report flag when we re run the report.
CURRENT CODE:
function onLoad(control, oldValue, newValue, isLoading) {
if (isLoading){
return;
}
var who = g_form.getReference('ref_who', refRegion);
function refRegion(who){
g_form.setValue('my_region', who.u_region);
}
}
What I think it should be...
function onLoad() {
var who = g_form.getReference('ref_who', refRegion);
}
function refRegion(who) {
g_form.setValue('my_region', who.u_region);
}
As far as I understand this code is used to auto populate a region when the form loads depending on the value of "ref_who" variable wich is autopopulated by other script when form loads.
I know the second option works as planned, but would like to understand why previous DEV did isloading and other OnChange type code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2017 06:59 AM
You are right, this is how it should be
- function onLoad() {
- var who = g_form.getReference('ref_who', refRegion);
- }
- function refRegion(who) {
- g_form.setValue('my_region', who.u_region);
- }