Multiple fields with onChange
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2011 03:30 PM
I am using client scripts to set the incident state on my forms. This is working great however I do have one question.
One of my conditions is based off of if several fields change. I cannot use the modified function because there are some fields I do not want this to apply to. Is there a way you could apply onChange to multiple fields without making a separate client script?
If am example helps this is a single client script of mine...
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
//If the page isn't loading
if (!isLoading) {
//If the new value isn't blank
if(newValue != '') {
//Type appropriate comment here, and begin script below
var state;
state = g_form.getValue('incident_state');
//alert("state = " + state);
if(state == '210' || state == '220'){
g_form.setValue('incident_state', '230');
}
}
}
}
Thanks,
Tony
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2011 03:43 PM
I found a solution on servicenowguru
http://www.servicenowguru.com/scripting/business-rules-scripting/checking-modified-fields-script/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2014 04:15 PM
I'm not sure that current article really answers the question. I think I understand what he is asking because I also have the same issue.
I would like to have the same onChange client script called that can be ran if 2 specific fields change.
For example: You have a client script that calculates the duration based on 2 fields: start_date & end_date.
If the start_date field changes, you want to recalculate the duration with the script and show the user the new duration. If the end_date changes, you want to recalculate duration with the script and show the user the new duration.
Currently, with the user interface, it seems like you are only allow to pick one field for Field name. So in order for this to work, you effectively have to created 2 entries for the same client script: one for onChange end_date, and one for onChange start_date.
Any solution? Or am I just stuck creating 2 client script entries?
And to be honest, since its the same code running twice anyways, its not to say that one way is more efficient than the other as far as server calls, or script processing because they are the same script that must be ran twice regardless.
BUT - It would be cleaner on the administration end and easier on maintenance (for sure).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2014 11:15 PM
You can accomplish that with a little trickery. If you do not already have an onLoad client script, create a new one with the following code:
function onLoad() {
//just a place holder, does nothing
}
//define the custom function
function u_calculateDuration() {
alert("This would calculate the duration.");
//add whatever code you need here
}
The "onLoad" function is just used as a place holder and does not actually do anything but allows us to define the custom function. To call it, add the following onChange scripts for both fields:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
//run the custom function
u_calculateDuration();
}
You can see it running in demo022 at the moment on the Change Request form. Change either of the Planned start date or Planned end date fields and you will get the alert message.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2014 09:04 AM
Thank you! I was having the same problem and this worked perfectly for me.