- 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
‎09-16-2020 06:48 AM
does this work on catalog client scripts as well? I am trying to use this solution but it's not working for me (on New York).
I am trying to do a simple alert to see if it works, but nothing happens
my onload:
function onLoad() {
//Type appropriate comment here, and begin script below
}
function checkPrimaryRole() {
alert('this is working');
}
My onChange
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
checkPrimaryRole();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2025 12:13 PM - edited ‎07-17-2025 12:14 PM
You need to change your onLoad client script to the following so the function is an expression in order to work on Service Portal:
function onLoad() {
// Usual onload stuff here.....
}
//This function is called within the various onChange methods
checkPrimaryRole = function(){
//Run Shared code here!
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-20-2021 04:44 AM
Hi Mark,
I'm having the same scenario like, Onchange of following fields title, country and department another filed(role field need to set).it works fine in catalog view but in service portal not working any idea?
function onLoad() {
var tit = g_form.getControl('title');
tit.onchange = setRole;
var cou = g_form.getControl('country');
cou.onchange = setRole;
var dep = g_form.getControl('department');
dep.onchange = setRole;
}
function setRole() {
var title = g_form.getValue('title');
var department = g_form.getValue('department');
var country = g_form.getValue('country');
if (department == "Bailiffs") {
g_form.setValue('role','Role_DE_LIT_Officer' );
}
onChange(this.name);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2025 12:18 PM
The function is onLoad Client Script needs to be as expression to work on the Service Portal:
// Replace function setRole() with setRole = function()
setRole = function() {
var title = g_form.getValue('title');
var department = g_form.getValue('department');
var country = g_form.getValue('country');
if (department == "Bailiffs") {
g_form.setValue('role','Role_DE_LIT_Officer' );
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2025 12:28 PM - edited ‎07-17-2025 12:29 PM
Thank you for the information, it is really helpful even 7 years later.
However, few things to add as of Yokohama release:
1) For the code to work on the ServicePortal, the function needs to be written as an expression:
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
calculateContractDates = function() {
var renewable_value = g_form.getValue('renewable');
alert(renewable_value);
}
2) Article written by Vinod Paul Fredrick explains how to replace multiple onChange Client Scripts with one onLoad Client Script
Using one onLoad Client Script instead of multiple onChange ones