Multiple fields with onChange

tbalestreri3
Mega Contributor

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

15 REPLIES 15

Hi Jim


What if I need to change many fields.. For example: I need to change 10 fields values and the change must reflect on one field. Will it be useful? I need to add 10 fields and reflect the result on one field. I want it OnChange.



Thanks


Bhargava


This doesn't work as-is in the Fuji release (I think I'm on patch 4), but can be achieved by setting the function to g_scratchpad in an onLoad client script:



function onLoad() {


  g_scratchpad.u_calculateDuration = u_calculateDuration;


}



function u_calculateDuration() {


    alert("This would calculate the duration.");


}



And then in the onChange script:



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading) {


      return;


  }



  g_scratchpad.u_calculateDuration();


}


Hi Jim,

I realize this is a very old post but I did use your onLoad trickery and it worked great in the Portal.  However, I cannot get it to work when a field is changed on the RITM or SCTASK.  Is there something else that needs to be set up for this to also run when a field is changed on the RITM or SCTASK in the UI interface?

Thanks,

Diana

kungfuu72
Giga Expert

So i've since solved this problem for good as far as my example with 2 fields that change. Example was a start date and an end date.



My solution included 2 onChange client scripts that were exactly the same. These 2 functions did a AJAX call and that was my solution:



Client Script:


function onChange(control, oldValue, newValue, isLoading) {


  if(isLoading) {


  return;


  }



  g_form.setValue('u_duration', calculateDuration(g_form.getValue('u_start_date'), g_form.getValue('u_end_date'), false));


}



Function calculateDuration:


/***** Calculation to set a duration field, or get the number of seconds between a duration. *****/


// Parameter [start] Required. Format: YYYY-MM-DD HH24:MM:SS (e.g. 2015-01-29 14:07:44).


// Parameter [end] Required. Format: YYYY-MM-DD HH24:MM:SS (e.g. 2015-01-29 14:07:44).


// Parameter [seconds] Required. Boolean.


////////// If "true", the return will be formatted in number of seconds.


////////// If "false", the return will be formatted ddd hh:mm:ss.


// Uses the Ajax Script Include "durCalc".


function calculateDuration(start, end, seconds) {


  var ajax = new GlideAjax('AjaxDurCalc');


  ajax.addParam('sysparm_name','durCalc');


  ajax.addParam('sysparm_start', start);


  ajax.addParam('sysparm_end', end);


  ajax.addParam('sysparm_seconds', seconds);


  ajax.getXMLWait();



  var answer = ajax.getAnswer();



  return answer;


}



AjaxdurCalc Script Include:


var AjaxDurCalc = Class.create();




AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  durCalc: function() {


  return gs.dateDiff(this.getParameter('sysparm_start'), this.getParameter('sysparm_end'), this.getParameter('sysparm_seconds'));


  }


});




* Of course, there are many different ways to do this, especially with the calculateDuration function. For my sake, I know exactly which format I will need and which timezone I will need, but you may have different needs.


Hi Daniel--


What exactly is the calculateDuration Function?   Is it also a client script?   A script include?


Thanks in advance,


Josh