"onchange" client script does not get triggered when updating the related field (string with 6 choice option)

andreasschroede
Kilo Contributor

Hi All,

I am a newbie with regard to ServiceNow (but not the first time doing software development ;o)

I am using the weekend to get my feet wet with client scripting for the new demand management application of my company. We have a brandnew instance, provided by ServiceNow a couple of weeks ago. So most of the scripts already work very well.

Where I have got stuck after hours of googeling and searching the community is the following: I would like to implement a client script, that updates in the table

[DEMAND] an integer field with an amount, translating the famous ServiceNow-T-Shirt-Size (string) into Euros, e.g.

if size = "small", the amount is < 100K Euro

if size = "medium", the amount is < 500K Euro ...

The trigger for the "onchange" client script is the field 'size' (out-of-the-box in the table [DEMAND], a string with 5 options, I have entered a 6th option (Extra-Small)).

The field to be updated is 'u_t_shirt_size_numeric', in the very same table [DEMAND].

My problem is, that I can edit in the form the T-Shirt-Size in any form, but the script just does not get called. Can this be linked to the fact that 'size' is a choice field. My scripts get called in all other field types I have tried so far.

And yes, I have checked 5 times, that the name of the field in the table is correctly selected in the script.

Thx a lot, Andreas

=====================================================================

And this is the code, but as I said, never called (thus possibly not correct scripted):

=====================================================================

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

     

      // Bugfixing: display of parameters of the function. However, these alerts never show up, thus the function is not called when editing the field "size"

      alert('control: ' + control);

      alert('old value: ' + oldValue);

      alert('new value: ' + newValue);

      alert('isLoading: ' + isLoading);

      alert('isTemplate: ' + isTemplate);

     

     

      // The usual "entry-check" for "newValue == ''" should not be relevant here, as there is no "undefined" or "empty" T-Shirt-Size in the choice list

      if (isLoading) {

              return;

      }

     

      var editedTShirtSize = newValue;

      var validTShirtSize = false;

     

      // This case of empty T-Shirt-Size should not exist if the field has been edited, as there is no "undefined" or "empty" T-Shirt-Size in the choice list

      if (editedTShirtSize == '') {

              g_form.setValue('u_t_shirt_size_numeric', '0');

              validTShirtSize = true;

      }

     

      if (editedTShirtSize == 'xsmall') {

              g_form.setValue('u_t_shirt_size_numeric', '50');

              validTShirtSize = true;

      }

     

      if (editedTShirtSize == 'small') {

              g_form.setValue('u_t_shirt_size_numeric', '100');

              validTShirtSize = true;

      }

     

      if (editedTShirtSize == 'medium') {

              g_form.setValue('u_t_shirt_size_numeric', '500');

              validTShirtSize = true;

      }

     

      if (editedTShirtSize == 'large') {

              g_form.setValue('u_t_shirt_size_numeric', '1000');

              validTShirtSize = true;

      }

     

      if (editedTShirtSize == 'xlarge') {

              g_form.setValue('u_t_shirt_size_numeric', '1500');

              validTShirtSize = true;

      }

     

      if (editedTShirtSize == 'xxlarge') {

              g_form.setValue('u_t_shirt_size_numeric', '2500');

              validTShirtSize = true;

      }

     

      // Safety measure: if another choice gets added to the T-Shirt-Size, an error message should occur, as no numeric value has been defined

      if (validTShirtSize == false) {

              alert('For your selected T-Shirt-Size so far no numeric values has been defined. The field "T-Shirt-Size numeric (Euro)" was not updated. Please contact Technology area');

              //would be good to also trigger an email to all admins / members of Technology Group

      }

     

}

2 REPLIES 2

Michael Fry1
Kilo Patron

I took your script and put it into a Client Script, on Demand table and it triggers when I open the form, and when I change the t-shirt size. Here's how I set it up, can you compare to how you set it up?



Screen Shot 2016-10-17 at 8.31.39 AM.png


Hallo Michael,



this was it. Thanks a lot.



And this is what happend: not very intuitive, but in the demand-table two columns relate to size:


- the column label "size" is related to the column name "score_size"


- the column label "T-Shirt-Size" is related to the column name "size"



So the concept "size" is distributed over two columns.



In the script the field that was asked for was labelled "field name", not "column label" nor "column name". This is why i picked "column name", i.e. "size".



So THANK YOU again, Andreas


find_real_file.png