I’ve mentioned that I’m lazy, and I’m not ashamed of that – in fact, I’m sure there’s a joke out there about finding the easiest way to do something, by asking a developer, but I am too lazy to go find the joke <insert rimshot here>.
I’ll be here all night folks…
So I want to talk about a concept, that even before I knew what it was called (officially), I had my own version of it. It’s a concept that I think some people will understand easy enough, especially parents.
The DRY Principle
Don’t Repeat Yourself – I use to call it the 2 Times rule, if I had to write code that did something twice, it was rewritten into a function and centralized so I didn’t have to write it again. I like my code to make my life easier, just like I would like my kids to do what I ask the first time around – why can’t they be obedient little 1s and 0s?
When I was a LAMP stack developer, and I was building out full back-office management systems, one thing that could get quite repetitive was including files that contained functions I wanted to use. I would end up with a lengthy list of includes, and it was annoying. So I hit upon an idea to just loop through all the files (in my functions director) and include them that way, then I didn’t have to worry about missing something I needed. Now I had access to all my fab functions when I needed them. This simplified my life, and kept me from typing the same stuff over and over – DRY.
I’m going to try and relate this to something I ran into the other day – not necessarily repeating myself, but in a sense, doing menial work that could be automated – thereby avoiding repetitious work and saving some time (eventually) for myself and others down the road.
Parallel Parking a 1976 Buick LeSabre
I’ll be honest here, I can’t parallel park very well. They taught me, and I must have done alright to pass, but I can’t do it very well now. Back then, even though it was well past 1976, our school’s Driver’s Ed class had a bunch of old Buicks for driving cars. Have you ever tried to park a ~19ft land yacht in a 20ft spot? Now image you had to park another one, or two or three in there. Never gonna happen, right?
So that’s my dilemma, I need to fit more 1976 Buick LeSabre’s in a single parking spot.
What I really need is a magical way to increase my parking spot size, but that is so time consuming, I mean there’s like 100 of them already (with cars), and I need to squeeze in another 10, right in the middle.
Now I could do this all by hand, pull out each car and repaint all the spots to add 10, but maybe in the future I’ll need to add more – I don’t want to do this all over again.
Maybe I could build a machine that would create a bigger space and move the car to the front of that space and I just start it and let it go, in fact I could just tell it how big I want the spaces to be, let it figure out how many of those space I need, and go to the coffee shop while it works.
I think we all know where I’m going with this.
To put this in a ServiceNow perspective I need to add variables to a Record Producer (this will also work on variables for a Catalog Item too). But the variables are too close to each other via Ordering. So I need to add some spacing in there.
I need to add 10 new variables between 1 and 3, but how do I do it? Granted this is a simple example and I could just double click into each one and change the value – but let’s say you had 100, easy you say – ok, let’s say you had 100 catalog item/record producers and each had 100 variables, still seem like a simple task? What would take you longer, to fix them all, or to write a script that does it for you?
Loop de Loop
The basis for this hot little function is a couple of loops. The trick to understand is that in most Javascript arrays, they are 0 based. they start at 0 and go from there when you iterate through them. This initially stalled my script, because you can’t multiple 0 by and anything and get a number, you’ll just get 0. Granted, you could start with 0, but for some reason I don’t like that idea, I like a number to start with. Let’s say, I want to set each value by 100, so it would be 100, 200, 300 in the image above. This gives me some breathing room, in case I need to add more in the future. If you’re really paranoid, you could even set them by 1000.
Pro-tip
Get in the habit of ordering your variables but 100, this gives plenty of room for future additions and others from having to reorder the variables down the road.
The needed ingredients for this are as follows:
1. sys_id of the catalog item/record producer: this will allow us to loop through the variables that belong to it and set ordering.
2. the iterator: this will be the amount between the numbers, so for this example, 100.
Round 1
This is going to query the item_option_new table to get the variables that belong to the catalog item/record producer.
What we want to do is put those records into an array, so let’s add that in.
Round 2
So now I should have an array with 3 elements in it, matching the 3 variables in the above image. Now comes a little math, and this is where I got hung up for a minute, trying to get my new counts.
Round 3
There a bit of trick here, you see the n++ has to come before the assignment of x because I need it to count up to 1 first, so that it’s not 0 when I times it by the iterator. Otherwise I get 0, and I want 100. then I push those into their own array, so I should have matching arrays, well I mean in length at least.
Round 4
So here you can see I’ve added a second loop to update the variable records with the new values, which should be 100, 200, 300 respectively.
The way to run this, is through Background – Scripts and if you happen to be running London, you get something new when it’s finished running.
Final Results
As you can see I now have new ordering on my variables, they are 100 apart and I can easily add new variables. I mean it’s so nice, I can now add more, then reset the order again after I’m done, there by maintaining a 100 difference between variables at all times.
This is the new result page from London, granted the logs below the line are from my version of the script. You can see the first 3 lines are looping through the variables, and then the array of my new numbering. The next 3 lines are from London, and the last line is what my final array looked like as I was updating the records.
Our team of Emmy Award Winning UX /and UI designers, along with business, technology and implementation experts, completely transform the way people work every day. With a Design Thinking mindset, we focus on key drivers of Digital Transformation to ensure your success now and in the future. Contact a Cask expert today.