- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2018 01:56 PM
I found this article on how to attach a common onchange function to multiple fields in an onload client script. I want to calculate a date field (end date) but I need to check multiple fields (start date, length of term, renewable, etc) first to ensure they have values or specific values so the calculation can be done, if it should be done.
http://www.snc-blog.com/2012/06/04/client-script-for-multiple-fields/
The problem I am running into is this. When manually changing the date value of start date, it works fine. When the datepicker is used to set the field value of start date, my onchange does not fire. Simplified code below:
function onLoad() {
try {
var start_field = g_form.getControl('starts');
start_field.onchange = calculateContractDates;
} catch (err) { }
}
function calculateContractDates() {
var renewable_value = g_form.getValue('renewable');
alert(renewable_value);
}
Any insight would be appreciated.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2018 02:45 PM
I can confirm the same thing, which means that there's likely another element in the HTML that's storing that value for a date field. In any case, this type of coding is likely to cause you some sort of issue down the road because it's dependent on ServiceNow keeping their field structure on their forms relatively consistent. It might be worth it if there were no other way, but in this case there are several. I've done something similar quite a bit in the past still requires you to have a single 'onChange' client script associated to each field you're listening to a change on, but those 'onChange' scripts just make a call to your global function so you can manage the code for all of them in one place. In order to do this, you will need 2 things.
1) An 'onLoad' client script similar to what you've shown above, but with an empty 'onLoad' function like this...
function onLoad() {
// Blank since we're just creating a shared function for this table
}
// Shared function that can be accessed by any other client script for this table
function calculateContractDates() {
var renewable_value = g_form.getValue('renewable');
alert(renewable_value);
}
2) An individual 'onChange' client script to watch for each field change. Your client script would just need to call your shared function like this.
calculateContractDates();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2018 02:09 PM
I would add onChange script for each field instead of using onLoad
Please mark this response as correct or helpful if it assisted you with your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2018 02:19 PM
I would suggest to create 1 business rule for doing this validation.
You can run business rule on insert, update and you can include validations for all fields in 1 business rule.
This will simplify your code and it will be easy for maintenance.
Regards,
Sachin

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2018 02:45 PM
I can confirm the same thing, which means that there's likely another element in the HTML that's storing that value for a date field. In any case, this type of coding is likely to cause you some sort of issue down the road because it's dependent on ServiceNow keeping their field structure on their forms relatively consistent. It might be worth it if there were no other way, but in this case there are several. I've done something similar quite a bit in the past still requires you to have a single 'onChange' client script associated to each field you're listening to a change on, but those 'onChange' scripts just make a call to your global function so you can manage the code for all of them in one place. In order to do this, you will need 2 things.
1) An 'onLoad' client script similar to what you've shown above, but with an empty 'onLoad' function like this...
function onLoad() {
// Blank since we're just creating a shared function for this table
}
// Shared function that can be accessed by any other client script for this table
function calculateContractDates() {
var renewable_value = g_form.getValue('renewable');
alert(renewable_value);
}
2) An individual 'onChange' client script to watch for each field change. Your client script would just need to call your shared function like this.
calculateContractDates();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2018 06:48 AM
I was trying to avoid the numerous client scripts I will require, but I guess there is not a better option. Thanks everyone for your input.