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 Pradeep, thanks for the link - it has made for some very interesting reading but I fear I will need to do a lot more to comprehend GlideAjax. In a nutshell are we saying that any script include I create should pull back any information I am currently using getReference for?



I have also read that if using getReference I should be using a 'call-back' - can someone please confirm in this context what it is doing and if this is appropriate for the above script I last posted?



michael.ritchie   - this sounds like your bag I think, any pointers on using GlideAjax instead of the above script much appreciated!



Many thanks!


Please see my getReferenceAdvanced blog post:


getReferenceAdvanced, g_form.getReference and GlideAjax Alternatives



I would recommend downloading the code from Share and installing in your instance since this doesn't use getReference nor GlideAjax, utilizing scripted REST instead.



Then you code would look something like the following.   Notice I moved the call to get the User's record inside the if versus doing it before since you appear to only need to run it IF the short description contains "AMD".   I also moved the region code within that IF too since it is being set by the user.



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


     


      if (isLoading) {


              return;


      }


     


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


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


              var usr = getReferenceAdvancedDesktop('caller_id', 'u_location;u_region;u_region.u_assignment_group;u_region.u_assignment_group.name');


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


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


              var usrGroupName = usr.u_region_u_assignment_group_name;


              if (g_form.getValue('assigned_to') == '' && (g_form.getValue('assignment_group') == '') || usrGroupName.indexOf('Service Desk') > -1) {


                      g_form.setValue('assignment_group', usr.u_region_u_assignment_group);


              }


      }


}



If you need this to also work on the Service Portal then you need to follow the instructions in the blog post by creating another client script and use the getReferenceAdvancedPortal function instead.   Everything else should remain the same.


Michael many thanks for the reply - am hoping to have time to read through properly tomorrow and give it a go! Will update accordingly.


Hi all, so I still unfortunately haven't had a chance to review Michael's alternative unfortunately (things manic with various go-lives) but because I had to update the original defective client script it is currently as per my last 'working' update i.e. the script below.



This said one thing I have noticed is now, if a user creates an INC and selects a Customer, then as expected an Assignment group is populated however if you now remove this Customer entry (which nulls out the Assignment group value as expected) and then enter a new Customer an Assignment group value is not set (I would expect it to)?



I do not understand why this is the case when the script says to set it if Assignment group blank? Note at no point was an Assigned to value set.



Any ideas why and how I can update this? Thanks as always,


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


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




}


}


Try the below script instead.   As I had done earlier I moved some of your getReference() calls within your If statements so they aren't executed unless need be.   For example there is no point in running the getReference() on the caller_id field If the short description doesn't start with "AMD - ".   I also simplified your last if by first gathering the assignment group details.   Hopefully this will work better for you.



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


              if (ag != '') {


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


              }


           


              if (g_form.getValue('assigned_to') == '' && ag.indexOf('CS-Service Desk') > -1) {


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


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


              }


      }


}