How to set value for priority based on another field (i.e., reference field) using client script

Nitesh Chandana
Kilo Expert

Hello,

I am not able to set value for the priority field based on the reference field(department). I created two fields department and priority. department is reference and priority is choice.

If i select customer support. priority should be changed to critical. if we select IT, priority should be changed to High.

My script is

var ge= g_form.getReference('group');

  if(ge.name == 'IT')

  {

  g_form.setValue('priority','2');

  g_form.setMandatory('emergency');

  }

Can anyone help me out??

Thanks,

NItesh

1 ACCEPTED SOLUTION

nthumma
Giga Guru

You have to get reference like below


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


    var caller = g_form.getReference('group', doAlert); // doAlert is our callback function


}


function doAlert(caller) { //reference is passed into callback as first arguments


  if (caller.name == 'IT')


    {


  g_form.setValue('priority','2');


  g_form.setMandatory('emergency');


}


}



View solution in original post

5 REPLIES 5

nthumma
Giga Guru

You have to get reference like below


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


    var caller = g_form.getReference('group', doAlert); // doAlert is our callback function


}


function doAlert(caller) { //reference is passed into callback as first arguments


  if (caller.name == 'IT')


    {


  g_form.setValue('priority','2');


  g_form.setMandatory('emergency');


}


}



The SN Nerd
Giga Sage
Giga Sage

Does this solution need to be scalable?



For example, are you using IT as one example of a department that requires this behaviour, or is there more?



It is also important to note that ServiceNow do not recommend the use of getReference anymore, even with a callback.



The recommended approach is to use GlideAjax.


Consider refactoring.



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Chuck Tomasi
Tera Patron

From a maintenance perspective, I highly recommend taking a look at doing this with Data Lookups to define the rules in data rather than code. It also prevents you from colliding with client scripts that may already be trying to set the value based on the data lookup rules.



Data Lookup and Record Matching Support - ServiceNow Wiki


Yeah, that's where I was going with the 'scalable' part



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022