Script include to set the Group

Praveen75
Tera Contributor

Hi Guys, I have a scenario where I need to set the group value to "ABclient" whenever the custom field 'ID' of choice list changes. For this I can go with client script. But for the script include, I need a generic solution  if 'location' match is found then need to return the group. Please suggest how this can be done. Thanks.

 

 

2 REPLIES 2

Sandeep Rajput
Tera Patron
Tera Patron

@Praveen75 do you have location and group mapping which can be used for returning group?

If it is there is can be used as follows.

 getFields : function() {  // Not copied above lines FYI
   var stdChange = this.getParameter('sysparm_location');
   var grL = new GlideRecord('cmn_location");
   grL.get(stdChange);
   var group = '';
  switch (grL.getDisplayVlaue('u_parent_loc')){ 
  case 'UK':
  group = '<sys_id of group>';
  break;
 }
 case 'US:
  group = '<sys_id of group>';
  break;
 }
};

 

Jim Coyne
Kilo Patron

So there's a lot to unpack here.  Not sure where you are currently with the implementation, what's working, what's not, but there are a number of typos that would definitely make your code fail right from the start:

  • var grL = new GlideRecord('cmn_location");  --> you have a mix of single and double-quotes
  • if (grL.getDisplayVlaue('u_parent_loc') --> should be getDisplayValue
  • return gs.getproperty --> should be gs.getProperty and there's another mix of single/double quotes on that line

Remember, JavaScript is case-sensitive.  Also, just the naming of the function and variables in your code could be improved to make things more readable and more obvious as to what is happening for someone reading and maintaining your code.  Your "getFields" name should probably be more something like, depending on your class name, "getGroup", or "getCountryGroup".  Depends on the class name and what all it does.  It's important to be clear and concise instead of just "getting it to work".

 

Now setting all that aside, you want to think about the solution a bit more because, as you say, you do not want to hard-code things.  The problem with System Properties is that they are kinda hard-coded values as well.  They are obviously handy to use where appropriate and make things easier in certain cases, but in your particular case, it may actually make more sense to use a truly data-driven approach.  What I mean is you could probably add a field on the Location table to hold the Group data you are seeking.  So you would query for the Location record and then retrieve the Group as it is defined on that record.  Much cleaner and simpler.

 

That way you don't have to maintain any code or System Properties if there are changes, just updating the data on the records does it.  No more adding of "if" or "switch" conditions.