The CreatorCon Call for Content is officially open! Get started here.

Grant Hulbert
ServiceNow Employee
ServiceNow Employee

In this article, you will learn how to create, build, and most importantly: deploy a VueJS application on a ServiceNow instance.

Credit goes to Andrew Pishchulin for his React application in ServiceNow, which provided the clues I needed to write a similar article about VueJS

1. Create VueJS application

Open your favorite command line, and follow the official Vue CLI instructions here to create your sample app. Shortcut: if you're like me, and you don't want to read a long article, here's the important command line:

vue create hello-world

The vue CLI will ask you some questions; just press return to accept the default choices

Now let's confirm the simple app you just created is working:

cd hello-world
npm run serve

Open a browser and navigate to http://localhost:8080. Your boilerplate VueJS app should appear. It's currently hosted on your local computer, but we want to serve it from your ServiceNow instance, so that's what we'll do next.

find_real_file.png

2. Build the VueJS application for distribution, generating as few files as possible

A sophisticated VueJS application can contain hundreds of Javascript and CSS files once it's built for distribution. Our goal is to reduce the number of files we need to copy to your ServiceNow instance, in order to make it as easy as possible to deploy updates. By default, Vue splits apps into separate files, in order to improve loading times. The following configuration (specifically, splitChunks: false) instructs the vue compiler not to break apart your app into as many files as it normally would.

Create a file named vue.config.js in your hello-world folder, and paste the following into it:

const path = require('path')

module.exports = {
  css: {
    extract: true,
  },
  configureWebpack: {
    optimization: {
      splitChunks: false
    },
    resolve: {
      alias: {
        'vue$': path.resolve('./node_modules/vue/dist/vue.common.js'),
      },
    },
  },
}

Now go back to the command line (if npm run serve is still running, press ctrl-C to kill it) and build the VueJS app for distribution

npm run build

Notice a new folder named "dist" has been created inside your hello-world folder. We'll need a couple files from there in the next section.

find_real_file.png

3. Copy CSS and Javascript assets to your ServiceNow instance

  1. In ServiceNow, navigate to Content Management > Style Sheets
  2. Create two style sheet records (the names don't really matter, but for simplicity I will name them):
    1. Name the first one "vuejs_tutorial_main"
    2. In your local hello-world folder, open the file dist/js/app.SOME_RANDOM_TEXT.js and copy its contents into the ServiceNow Style field
    3. Name the second style sheet "vuejs_tutorial_css"
    4. In your local hello-world folder, open the file dist/css/app.SOME_RANDOM_TEXT.css and copy its contents into the ServiceNow Style field
  3. Important: copy the sys_id of both style sheet records you just created. For example, on my instance, the sys_ids were
    1. b4b944bcdb7dbb00bbdbff971d9619bb -> sys_id of vuejs_tutorial_main
    2. 2a49ccbcdb7dbb00bbdbff971d9619cc -> sys_id of vuejs_tutorial_css

find_real_file.png

You will need these two sys_ids for the next step. Don't worry, you'll only need to do this once!

4. One-time prepare your instance for deployment

Every VueJS app needs a core HTML file, which is basically the equivalent of index.html starting point. For this one-time setup, we will create a UI page, and link its Javascript and CSS to the sys_ids you created in the previous step.

  1. Create a UI Page, name it "vueindex" and set Direct = true
  2. Replace the entire contents of the UI Page's HTML field with the following HTML
  3. Edit the HTML you just pasted:
  4. Replace SYS_ID_OF_YOUR_CSS_STYLESHEET with the sys_id of your vuejs_tutorial_css style sheet record
  5. Replace SYS_ID_OF_YOUR_MAIN_JS with the sys_id of your vuejs_tutorial_main record
  6. Important: each file is referenced twice, so make sure you replace both occurrences
<html lang="en">
  <head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width,initial-scale=1"/>
    <title>hello-world</title>
    <link href="SYS_ID_OF_YOUR_CSS_STYLESHEET.cssdbx" rel="preload" as="style"/>
    <link href="SYS_ID_OF_YOUR_MAIN_JS.cssdbx" rel="preload" as="script"/>
    <link href="SYS_ID_OF_YOUR_CSS_STYLESHEET.cssdbx" rel="stylesheet"/>
  </head>
  <body>
    <noscript>
      <strong>We're sorry but hello-world doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <s cript src="SYS_ID_OF_YOUR_MAIN_JS.cssdbx"></script>
  </body>
</html>

find_real_file.png

5. Test your VueJS app

Click the [Try It] button on your UI Page record. Note that the Vue logo image link is broken; let's rectify that in the next step.

6. Upload static images

We do have one image (VueJS logo) in our application. The most appropriate way to host static images is to store them as Image records in ServiceNow instance:

  1. Navigate to System UI/Images
  2. Create a new record, upload the image
  3. Provide the appropriate name for your image. This is very important: the image name should be the entire path to your image in the hello-world dist/img folder. Your image name should end up something like img/logo.82b9c7a5.png

7. Iterate your VueJS development

Now that you have everything set up on your instance, it's much quicker to iterate new builds of your VueJS app each time you want to deploy it to your instance. After running npm run build from the command line, you only need to copy/paste the contents of the generated js and css files into each of your style sheet records. No need to update anything else, because the sys_ids will remain the same.

Congratulations! You've built and deployed your first VueJS app onto ServiceNow -- enjoy!

8. <<<Under construction>>> Extra credit: make REST calls to ServiceNow using axios

Because you're a sophisticated VueJS coder, you probably already know how to do this...but just in case you're looking for a quick tip to get you started, here's how to make REST calls to your instance, and display the returned data in VueJS.

You'll need to add the excellent axios library to your VueJS project, and while you're at it, add a Material Design library called Vuetify. Use npm to do that:

Go back to the command line (if npm run serve is still running, press ctrl-C to kill it), and from the hello-world directory, execute the following to add the library and restart the development environment:

npm install --save axios
npm install vuetify --save
npm run serve

Update main.js to include the Vuetify framework

import Vuetify from 'vuetify'
Vue.use(Vuetify)
import 'vuetify/dist/vuetify.min.css'

index.html: 

<linkhref='https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons'rel="stylesheet">
 
HelloWorld.vue:
<v-data-table
:items="incidents"
item-key="sys_id"
>
<templatev-slot:items="props">
<tdalign='left'>{{props.item.sys_id}}</td>
<tdalign='left'>{{props.item.short_description}}</td>
</template>
</v-data-table>
 
data () {
return {
incidents: []
}
},
mounted () {
this.loadIncidents()
},
methods: {
loadIncidents () {
axios({ method:'GET',
'url':'https://techalliance.service-now.com/api/now/table/incident',
headers: {
'Accept':'application/json',
'Content-Type':'application/json',
'Authorization':'Basic '+btoa('grant.hulbert'+':'+'ypK3^AOBrfxqVQt')
}
}).then(result => {
result.data.result.forEach(element=> {
this.incidents.push(element)
})
}, error => {
alert(error)
})
}
}
 
Comments
Thom8
Giga Expert

Hi Grant

You can also just add VueJS via CDN. Works as well. 🙂

Regards,
Thom

Paolo6
Kilo Expert

Hi Grant, thank you for your article, very useful for me.

I have a question. I'm not a developer. I mantain a ServiceNow instance and a Vue.js developer (who knows nothing about ServiceNow) gave me his Vue.js application.

I imported the application with success following your tutorial.

But the application use also an external json file present in the "data" folder under "dist".

Is there a way to load this json file to ServiceNow and let the app use it?

Thank you very much.

Paolo

Thom8
Giga Expert

Can you put the data from the json in a data table and use it from there? I used axios to get and modify my data from a table.

Paolo6
Kilo Expert

Hi Thom, yes I think this is an option but change in the source code is needed.

My question is if it's possible without changing the code; my sensation is no, but I'm not a developer so I'm asking 🙂

Thank you.

Paolo

Thom8
Giga Expert

Hi Paolo

Hmm maybe somebody else has a idea, but my feeling is the same; you have to change something at your source code 😞

Regards,
Thom

SaschaWildgrube
ServiceNow Employee
ServiceNow Employee

The Reactor app enables you to build and deliver React applications directly from a ServiceNow instance. No need for an external development environment or pre-compilation. You can ship apps or individual components as part of your scoped apps. It also supports JSX.

You can apply the same mechanisms to deploy Vue.js apps, too.

Check this out:

https://www.wildgrube.com/servicenow-reactor 

Version history
Last update:
‎05-30-2019 04:18 PM
Updated by: