Burnout



Continuing with the theme of best practices around the dot walking, next best practice is to save the values locally. If you have multiple checks to do or if you want a particular dot-walked value in multiple checks/calculations, it is a good idea to fetch that value once and store it in a variable.

Reasoning is simple, our goal is to reduce the database calls to an absolute minimum. Each time we get values using dot walked fields, we are essentially doing multiple database queries to get the information. So if the fetched value is used multiple times, rather than doing a database query for every single check, use a variable to store that value once and use this variable for further checks or calculations.

Suppose you want to trigger an event if the incident belongs to a server in Mumbai, Pune, Hyderabad or Chennai locations. First attempt to get it to work could look like this
if(current.cmdb_ci.location.getDisplayValue() === "Mumbai"
|| current.cmdb_ci.location.getDisplayValue() === "Pune"
|| current.cmdb_ci.location.getDisplayValue() === "Hyderabad"
|| current.cmdb_ci.location.getDisplayValue() === "Chennai") {
//trigger your event here
}
To avoid the multiple server calls an immediate better solution would be a code like below 
var serverLocation = current.cmdb_ci.location.getDisplayValue();

if(serverLocation === "Mumbai" || 
  serverLocation === "Pune" || 
  serverLocation === "Hyderabad" || 
  serverLocation === "Chennai") {
  //trigger your event here
}
This reduces the number of serverside calls. However, imagine a scenario where we are asked to add/modify the locations. In such cases, it could be cumbersome to keep updating conditions, so an even better way to write above script would be to write a script like below.
var serverLocation = current.cmdb_ci.location.getDisplayValue();

var locationsToCheck = ["Mumbai", "Chennai", "Pune", "Hyderabad"];

if(locationsToCheck.indexOf(serverLocation) !== -1) {
  //trigger your event here
}
Any new addition would go to the array, logical part of your script would remain the same. And if the changes are very frequent, you can create a system property which stores the location names, separated by commas (let's say). Read from that property to get the names of locations and use those to check your condition. So your final code would look something like below
var serverLocation = current.cmdb_ci.location.getDisplayValue();
var locationsToCheck = gs.getProperty("your.property.name").split(","); //assuming values are separated by a comma

if(locationsToCheck.indexOf(serverLocation) !== -1) {
//trigger your event here;
}


P.S. - For the uninitiated "Utha le re baba" is a famous dialogue from a bollywood classic - Herapheri.










Creative Commons License

Comments