- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-03-2018 05:12 AM
Hi,
I have a catalog client script of type onChange. This script makes check in if condition and then throws a field message if check evaluates to true. Issue I am facing is that this field message is occurring when page loads.
Scenario:
I have a date field that pre-populates when page loads. This date is set to some date in future. I am maintaining this same date in a hidden date/time field as well. When user changes this date to even one day beyond the pre-populated date, user gets a field message info.
Basically when user changes the date, an onChange script on this date/time field executes and I do a comparison between this changed date and the original date which is also stored in hidden date/time field. If changed date is more than date in hidden field then the user sees the field message info. This scenario works fine.
But this field message info is also appearing when the form loads and sets the pre-populated date in the visible date/time field and hidden date/time field from back end table.
I referred to some solutions shared on community but no luck. I do not want this onChnage script to run when form loads.
Below are the articles I referred:
https://community.servicenow.com/community?id=community_question&sys_id=82964f25db1cdbc01dcaf3231f961901
https://community.servicenow.com/community?id=community_question&sys_id=67c88b61db5cdbc01dcaf3231f9619da
https://community.servicenow.com/community?id=community_question&sys_id=6e501729dbdcdbc01dcaf3231f961936
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2018 01:00 AM
Hmm, ok i see the problem; the oldValue is always going to be the value of the field when the form loaded so that condition will always match.
You could try this, i have no idea if it will work but worth a try!
if(oldValue == ''){
oldValue = newValue;
return;
}
If that doesn't work (and i suspect it won't) you can add a hidden true/false field to the form, default it to false and in the onLoad script that calls the nowDateTime function, after the line that populates the date/time field add another line to change the true/false field to true. The in your onChange you can say:
if(g_form.getValue('u_true_false_field') == false){
return;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-03-2018 05:14 AM
Standard client script syntax comes with the below statement which should prevent the rule from triggering if the form is loading:
if (isLoading || newValue === '') {
return;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2018 05:18 AM
Hi,
As the default syntax did not work in my case I tried below options but no luck:
#1. if (isLoading || newValue == '') {
return;
}
//My code that shows field message
-----
#2.
if(isLoading){
return;
}
//My code that shows field message
-----
#3.
if(i!sLoading){
//My code that shows field message
}
-----
#4. if(isLoading){
//My code that shows field message
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2018 07:10 AM
How is this field being populated when the form loads? Is there a value in there already or is there some script running that populates the field with a value as the form loads? if the latter then that will be why !isLoading isn't working as it's not the form load process that is changing the value of the field.
If the field is blank when the form loads and is then populated with a value then you could use:
if(oldValue == ''){
return;
}
If there is a value when the form loads but the field is then populated with a new value then you'll have to work around it. You could create a new true/false field and hide it on the form, then in the script that populates your field mark that field as true, then you can use that field as a condition in your client script:
if(g_form.u_new_true_false_field == false){
return;
}
If you do this you'll then need a business rule to revert it from true to false when you update the form. It's a bit long but it might be your only option.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2018 11:57 PM
Hi,
Below is the explanation of sequence of actions that happen:
I have a onLoad script that calls a script include function 'nowDateTime' without passing any parameter to this function (just a function call). This function 'nowDateTime' reads the current system date, adds 365 days to this date and sends this incremented date back to the calling client script. In client script I set this returned date in date/time field. Along with this field the returned date is also set in one hidden date/time field variable.
So to answer your question, this date/time value is getting calculated when the form is loading and once the form is completely loaded user sees this calculated date in date/time field. Hence this date/time field is blank only when form is loading and once the form is loaded completely this field contains calculated value.
When I put debug logs in both, onLoad and onChange scripts, the behavior I noticed is as below:
1. onLoad script is called and date is calculated as mentioned above.
2. When the onLoad script is setting this calculated date value in the date/time field, the onChange script is called. This onChange script is called since initially the date/time field is empty and the value is getting set in onLoad script i.e. since the value in date/time field is changing from empty to calculated date, the onChange script is called which contains the code that shows field message and this is how the field message is shown.
NOTE: Field message will only be shown if the visible date/time field value is compared with hidden date/time field value and the user selected date goes beyond the date in hidden date/time field. But after the form loads both these date/time fields have same date.
When I use the below option in onChaneg script, the field message is not shown when form finishes loading and the date/time field is populated with a value.
if(oldValue == ''){
return;
}
//My code that shows field message
With this if condition I do not see field message when form loads completely which solves the primary issue but when I change the date i.e. when onChange script is called on interaction with date/time field, I want to trigger the rest of the code where comparison is done and based on this comparison outcome, field message will either be shown or not.