There is a great github repository full of code snippets – here. It contains community samples and is maintained actively by ServiceNow representatives. It’s definitely a great addition to daily work.
The only downside is that you can’t actually use them from within the instance. Or can you…? Of course you can, I wouldn’t be writing this otherwise! I will explain the idea and explain the preparations in this post. Next one will be more technical and will dive deeper into the implementation.
The idea
The idea is simple:
- Right click on code editor
- Open a popup with a repository listing
- Pick a snippet you like
- Have it inserted in your script field
As I’ve already shown here, adding right mouse button option to the Monaco Code Editor within ServiceNow is possible. If we can easily do that, we can also open a popup with GlideModal
. Now the problem is with the content. How can we get the repository content? Maybe there is an API for GitHub?
And yes, there is one! Even better, the documentation has examples using JavaScript
to make REST calls. Excited already? I was too.
Issues with JavaScript
Well, maube not the issues but rather some doubts I had. As you can read in the GitHub API documentation, you can call it from browser (because that’s what we want to do, call it from a UI Page, I hope you remember 😉 ) but it uses some mysterious type="module"
syntax. What’s that? A modern way of adding scripts to your page (read more here if you are interested). And that’s where I had first doubts – modern JavaScript and ServiceNow do not always go well together. But let’s try, why not? As I said, implementation details will be explained later. So far, let me just tell you – it’s working!
Using the GitHub API
Although the examples are written using ES2022 (still not supported in ServiceNow), working with GitHub API documentation was a really great experience. With some general JavaScript knowledge and console logging you can easily make it work with ES6. I haven’t tried with globally scoped app, and I won’t. I prefer working with latest available technology.
The most important thing is the user and access management:
- You need a GitHub user
- When using the API, you have to authenticate with a PassKey
- Don’t worry, great documentation makes the process of creation it really simple
- You can set the level of access for the PassKey – read only to selected repositories, for example
- And, lastly, when you fork a repo and don’t have any lagging commits, it will be synchronized with the original one
Future development
Although I don’t use code snippets (never learned to, never needed to), I know that most developers use them. It’s a great addition to the product to have dedicated community creating those samples. Now that it is also possible in ServiceNow, imagine what you could do? Attach samples from your own repository? From previous projects? From corporate repo? It’s easy, and with some additional configuration you can make every developer connect to different repo!