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

Hi, thank-you for the quick responses guys. To test initially working I have got the group setting based on, if Assigned to AND Assignment group is blank however as mentioned I ideally need it as:



'Assigned to' is blank


AND


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



So a couple of queries, how would I user GlideAjax instead of getReference as recommended by Pradeep in this scenario and if having to use getReference can I use the display names instead (as will be easier as there are multiple groups with Service Desk in the name).



Many 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') == '' && g_form.getValue('assignment_group') == '')  



{


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




}


}


In that case, the if statement should be



if (g_form.getValue('assigned_to') == '' && g_form.getDisplayBox('assignment_group').value == "PASS SERVICE DESK GROUP NAME HERE" || g_form.getValue('assignment_group') == ''))      



In this case, getDisplayBox will use display value and not sys_id.


He said contains ServiceDesk so the condition should be



var aGroup = g_form.getDisplayBox('assignment_group').toString();



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



Thanks,


Nitin



Hit like, Helpful or Correct depending on the impact of the response


Thanks a lot guys, I literally just found out about getDisplayBox and now it is working as expected with the below script (note I had to move the var vreg) however I am wary of using getReference given Pradeep's comments. Is anyone please able to expand on how GlideAjax would be used in this situation and confirm why this would be preferable? Thanks again.




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');


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


if (g_form.getValue('assigned_to') == '' && ((g_form.getValue('assignment_group') == '') || ag.indexOf('CS-Service Desk')>=0))



{


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



}


}


Here you go.. For more info on GlideAjax.


How to Write Smart GlideAjax Quickly