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 Michael, as always thanks for quick reply but with this revamped onChange script I am now finding that when I initially set a Customer value no assignment group is being set as expected? Thanks.


I think it may be because of line: if (ag != '') ?



As mentioned the script was working fine with exception of when if you nulled out the Customer and entered another no assignment group would be set as expected - interestingly, if you just replaced the existing Customer value without nulling it first the Assignment group sets fine.



Thanks in advance Michael!


Realized I didn't fully take your entire requirement.   Try the following:


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


       


      if (isLoading) {  


              return;  


      }  


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


       


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


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


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


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


               


              var setGroup = false;


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


              if (ag) {  


                      // 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;


                      }


              } else {


                      // Assignment group is empty, replace it


                      setGroup = true;


              }


               


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


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


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


              }  


      }  


}


Hi Michael, many thanks for this update however I am observing the same behaviour as mentioned in my last post i.e. if I null out a Customer,


/delete and enter a new Customer name, the default Assignment group is not being set even when conditions are met to do this i.e. as long as



Assigned to blank


AND


Assignment group blank OR Assignment group contains 'CS-Service Desk'



As mentioned if you replace the Customer value without nulling and then tabbing out the field (which caues the current Assignment group value to null out as expected), and enter another Customer value the correct default Assignment group value will insert so it seems to specifically not like, nulling out Customer value and then entering a new Customer value.



Hope this makes sense / am being clear enough - please shout if not! Thanks again.


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


              }


      }


}