Client script g_form if condition assistance needed please

jas101
Tera Expert

Hi all, if someone could someone please assist me with the following onChange client script that would be terrific. It is based on INC 'Customer' field and sets the Region and Assignment group (which is based on the Customer's Region) however I would like this Assignment group value to only be set where:

'Assigned to' is blank

AND

'Assignment group' is blank OR 'Assignment group' contains 'Service Desk'

In terms of just setting based on 'Assigned to' is blank I had tried the below (lines 13 and 14) without any success.

Note Region needs to continue to update regardless (as it currently is - unless Short description contains 'AMD -' similarly this condition needs to remain in place).

As always any help/input appreciated. Thanks!

DS

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

  if (isLoading) {

          return;

}

var sd = g_form.getValue('short_description');

var usr = g_form.getReference('caller_id');

if (sd.indexOf('AMD -')!=0)

{

g_form.setValue('u_region', usr.u_region);

//g_form.setValue('location', usr.location);

var vreg = g_form.getReference('u_region');

//if (g_form.getValue('assigned_to') == '') TEST CODE NOT WORKING

//if (g_form.assigned_to=='') TEST CODE NOT WORKING

// { Needed for line 13 or 14

g_form.setValue('assignment_group', vreg.u_assignment_group);

//g_form.setValue('assigned_to', ''); //WORKING OK - TO BLANK OUT ASSIGNED TO IF SETTING ASSIGNMENT GROUP

//} Needed for line 13 or 14

}

}

//Replaced by above on 15/08/2017

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

//   if (isLoading) {

  //       return;

/

1 ACCEPTED SOLUTION

OK recreated your setup somewhat in my demo instance and I found that the assignment group value was showing a value of "undefined" in your described scenario.   I adjusted the code accordingly and also added a little more logic to make this script more efficient.



The one question I have for you that tripped me up is the indexOf('AMD - ') !=0 portion.   Where is that in the short description?   In my case I just pasted that into my short description at the beginning and the script never ran because "AMD - " had an index of 0.   Will those characters always be in the middle of your short description?   If not we may need to adjust that.



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


     


      if (isLoading) {


              return;


      }


      var sd = g_form.getValue('short_description');


     


      if (sd.indexOf('AMD -')!=0) {


              if (newValue) {


                      // caller_id is filled in, set region and check assignment group


                      var usr = g_form.getReference('caller_id');


                      g_form.setValue('u_region', usr.u_region);


                      //g_form.setValue('location', usr.location);


                     


                      // Only evaluate assignment group if assigned to is empty


                      if (g_form.getValue('assigned_to') == '') {


                              var setGroup = false;


                              var ag = g_form.getValue('assignment_group');


                              if (ag == '' || ag == 'undefined') {


                                      // Assignment group is empty, replace it


                                      setGroup = true;


                              } else {


                                      // Have an assignment group value, check if CS-Service


                                      ag = g_form.getDisplayBox('assignment_group').value;


                                      if (ag.indexOf('CS-Service Desk') > -1) {


                                              setGroup = true;


                                      }


                              }


                             


                              if (setGroup) {


                                      var vreg = g_form.getReference('u_region');


                                      g_form.setValue('assignment_group', vreg.u_assignment_group);


                              }


                      }


              } else {


                      // Since caller_id is blank, blank out region


                      g_form.setValue('u_region', '');


              }


      }


}


View solution in original post

26 REPLIES 26

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Dasi,



Below is the updated code.


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


  if (isLoading) {  


          return;  


}  


var sd = g_form.getValue('short_description');  


var usr = g_form.getReference('caller_id');  


if (sd.indexOf('AMD -')!=0)  


{  


g_form.setValue('u_region', usr.u_region);  


//g_form.setValue('location', usr.location);  



}


if (g_form.getValue('assigned_to') == '' || g_form.getValue('assignment_group') == "PASS SYSID OF Service Desk GROUP HERE" || g_form.getValue('assignment_group') == ''))  




{


g_form.setValue('assignment_group', vreg.u_assignment_group);  


}


}


 


 






P.S: As a best practice don't hardcode the sys_id values. Reference:


Coding Best Practices - ServiceNow Wiki


nitin_kumar
Mega Guru

Hi Dasi,



Add the following code after line 11 of your code



var aGroup = g_form.getReference('assignment_group');


var n = aGroup.name.toString();



if((g_form.getValue('assigned_to') == '') &&(g_form.getValue('assignment_group') == '' || n.indexOf('Service Desk') >=0){



g_form.setValue('assignment_group', vreg.u_assignment_group);  



}



As Pradeep mentioned it's best practice to use GlideAjax instead of g_form.getReference() as it returns unnecessary information and reduces the performance.



Thanks,


Nitin.



Hit Like, Helpful or Correct based in the impact of the response


As a best practice avoid using getReference and instead use GlideAjax. Also, the value assignment_to should be assigned_to


Thanks for correcting me. I have edited my post