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, ahh interesting to see it was setting 'undefined' - for my understanding what would have been the easiest way for me to see/test this? From what I can tell so far the updated script is working great so thank-you!



Note the initial 'AMD - ' condition is needed for the occasional user admin incident that gets auto-logged via email with short description starting 'AMD -'. With these we do not want the script running at all if this is found at the beginning of the Short description and from what I can see this is working as wanted. Please confirm if there is any concern with this though.



Out of interest, is this refined script still a lot 'worse' than using a GlideAjax or scripted REST method that you referred to before? I.e. when the dust settles a bit my end would you recommend me still looking into this to maximise performance?



Thanks again.


ahh interesting to see it was setting 'undefined' - for my understanding what would have been the easiest way for me to see/test this?


Since this is a client script, your debugging options are more limited than the server side.   But inserting alert() statements is the best way.   So for example, I recreated your fields in my demo instance and then put alert statements after each variable assignment to see the actual value such as:


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


alert("ag: " + ag);



In the scenario you described of nulling out all the fields and then setting a caller, 'ag' had an undefined value so I had to consider that in the script.   So either using the alert() function or console.log and using the browser console is the best way to troubleshoot client scripts.



Note the initial 'AMD - ' condition is needed for the occasional user admin incident that gets auto-logged via email with short description starting 'AMD -'. With these we do not want the script running at all if this is found at the beginning of the Short description and from what I can see this is working as wanted. Please confirm if there is any concern with this though.

OK thanks for the clarification here.   I wanted to make sure we had the right condition and using sd.indexOf('AMD -')!=0 is the best for your requirement since that is looking for 'AMD - ' to be at the beginning of the short description.   There are other Javascript functions like startsWith() that could be used in other scenarios.



Out of interest, is this refined script still a lot 'worse' than using a GlideAjax or scripted REST method that you referred to before? I.e. when the dust settles a bit my end would you recommend me still looking into this to maximise performance?

This new script is much more efficient than your original one.   Server side script calls are "expensive" on the client side so its best to minimize those and only call them when needed.   This is why I moved the getReference() function calls within the If statements to make sure we need to make that call.   In the latest version I broke up your condition of checking the assigned_to value along with the group name because I wanted to absolutely make sure the assigned_to wasn't filled in before making the getReference() function call.



For this use case using getReference() is fine for the desktop UI, though its not following best practice of using a callback function which is required to work on mobile and the Service Portal.   So if this functionality will only ever be needed in the desktop UI you should be fine but you could look into GlideAjax or my getReferenceAdvanced solution in the future.   The thing that pushed me to create the getReferenceAdvanced solution is that getReference() can only go one layer deep and not "dot-walk" into relationships, GlideAjax can be tricky to setup, you can end up with Script Include "sprawl", and GlideAjax can only work asynchronously.   My solution solves those issues.   But again my proposed script above should get you going and you can address things later.



Glad to hear its now working though.   Please mark the appropriate correct answer so others can benefit from the answer and this post gets removed form the unanswered list.


Hi Michael, apologies for the delay - things have been very hectic my end with new global system go-lives over the last couple of weeks.



As always, thanks for your help and advice, much appreciated (no doubt will ask for more when able to spend time properly reviewing potential getReferenceAdvanced solution!).



Cheers,


DS


Hi Pradeep, 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);




}


}


Dave Smith1
ServiceNow Employee
ServiceNow Employee

dasi wrote:



Hi all, if someone could someone please assist me with the following onChange client script



It is based on INC 'Customer' field and sets the Region and Assignment group


Isn't this what Assignment Rules are used for?



(admittedly, the ones you want are server-based, rather than client-based)