The Now Platform® Washington DC release is live. Watch now!
NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.
DIFFICULTY LEVEL: BEGINNER to INTERMEDIATE
Assumes having taken the class SSNF and has good beginner to intermediate level of knowledge and/or familiarity with Scripting in ServiceNow. Don't be fooled, even though the examples are simple the concepts require knowledge of ServiceNow scripting.
Do you ever develop code snippets to prove a concept? I know I do, but it always leads to the question: How can you test those snippets to see if they function correctly in ServiceNow? There are actually three methods: Scripts - Background, Scheduled Jobs, and Fix Scripts. When I originally wrote this article in May of 2016 there really was only one shining, outstanding way to do complex script testing, and that was with Fix Scripts. Since then ServiceNow has ruined the functionality of Fix Scripts by taking away the Active flag (it was there, but was busted and eventually it just went away), and, if you are in Scope, they now auto-run on deployment (with absolutely no way to turn that off other than to delete it from the scope). Also, since 2016, others have stepped in to help improve the functionality of Scripts - Background. In this article I will update my previous notes and observations and introduce you to a nifty browser-based tool that allows for some serious functionality to be present in Scripts - Background. I will also point you to a couple of articles where you can use an external editor to work with scripts. So let's get started!
The Scripts - Background method includes three steps:
Pros:
Cons:
gs.info("---> Hello world!");
NOTE: This can be ANY valid server-side script. This can be multi-line, include functions, glide script, GlideRecords, eval, etc. Note the lack of line-numbers or any sort of editing features. The Scripts - Background application was created only to allow for the literal quick-and-dirty execution of simple scripts.
NOTE: I added the ---> at the start to make it easy to find this entry in the System Log.
5. Click the "Run Script" button to execute your script
6. Result:
7. Navigate to System Logs > All
8. Filter on Message *---> to view the "Audit" log (you have to include the Created by field to get the full affect).
The Fix Scripts method includes seven steps:
Pros:
Cons:
1. Navigate to System Definition > Fix Scripts
2. Click the "New" button
3. Fill in the following:
gs.info("Hello world!");
4. Bring up the context menu and click on Save
5. Click on Run Fix Script button. The Run Fix Script pop-up form appears.
6. Click on the Proceed button.
7. Your results will appear in the Run Fix Script pop-up form.
8. View your results and click the "Close" button to close out the results form and return to the Fix Scripts form.
9. Repeat steps five through eight, and instead of clicking the "Proceed" button, click the "Proceed in Background" button to bring up the Fix Script in the background form
10. Click the "Close" button to return to the Fix Scripts form
11. Navigate to System Diagnostics > Progress Workers.
12. Order by Updated descending. You should see your most recent Fix Script execution at the top.
13. Note the result of your code execution in the Message field
This is the Fix Script work-around.
Pros:
Cons:
1. Navigate to System Definition > Scheduled Jobs
2. Click on the New button
3. Choose Automatically run a script of your choosing
4. Fill out the form with the following:
gs.info("Hello world!");
5. Bring up the context menu and Save your work.
6. Click the Execute Now button.
7. Navigate to System Scheduler > Today's Scheduled Jobs and search for Two Methods for Code Development
- You may need to add the Updated field to the list view.
8. Navigate to System Logs > All and search for Hello World in the message field.
I have used Scripts - Background for years, and it is still my first choice for smaller scripts. I used to rely heavily on Fix Scripts until ServiceNow broke them. Now I work mostly with Scheduled Jobs, but really, they are not for modelling. Just a work-around. All run server side, and both methods do have their uses. Anyway, all are super tools for the tool box.
Scripts - Background |
Fixed Scripts / Scheduled Jobs |
|
Easy to use | Yes | Yes |
Allows for running in scope | Yes | Yes |
Audit | System Log only | Yes |
Instant Return | Yes | Yes, when run in foreground |
Can run in background | No | Yes |
Full editing and syntax checking | No | Yes |
Versioning | No | Yes |
Can be added to update sets | No | Yes |
Easy to make changes | Sort of, clunky | Yes |
Save scripts | No | Yes |
Export to XML | No | Yes |
Okay, so that was all of the out-of-the-box stuff. Let me introduce you to a couple of things that improve the Scripts - Background usage significantly!
SN Utils by Arnoud Kooi
This is an awesome tool for working in Scripts - Background. It brings in some excellent functionality. It is a browser plug-in that installs on the most common browsers. Here I will show it in Microsoft Edge.
It provides:
All on the same form!
Let us revisit our earlier Scripts - Background example.
Very nice! If you want this functionality you can install it from here: https://www.arnoudkooi.com/
Check out Chuck Tomasi's (@Chuck Tomasi) interview of Arnoud here: https://developer.servicenow.com/blog.do?p=/post/dyk-snutils/
VS Code Extensions
ServiceNow has introduced a very nice extension for Microsoft Visual Studio Code. This provides the robustness of a true code editing and versioning environment melded with Scripts - Background. This comes really close to providing a true IDE (integrated development environment) for the ServiceNow platform. Check it out! It is a bit involved to set up at first, but well work the final results. https://docs.servicenow.com/bundle/vancouver-application-development/page/build/applications/concept...
The key to the VS Code Extensions resides here: Being able to run Scripts - Background from the VS Code environment! Read more about it here: https://docs.servicenow.com/bundle/vancouver-application-development/page/build/applications/task/vs...
Download VS Code from here: https://code.visualstudio.com/download
So that shows some of the things I have explored concerning Scripts - Background and Fix Scripts/Scheduled Jobs. I hope that helps you in your development walk.
Enjoy!
Steven Bell.
If you find this article helps you, don't forget to log in and mark it as "Helpful"!
Originally published on: 5-7-2016 06:00 AM
I updated the code and brought the article into alignment with my new formatting standard.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.