KEMBAR78
Guide - Automated Servicenow Application | PDF | Annotation | Json
0% found this document useful (0 votes)
25 views55 pages

Guide - Automated Servicenow Application

This guide provides a step-by-step process for mass updating applications and plugins in a ServiceNow instance by creating a custom global application and two custom flow actions. It includes exercises for setting up a service account, creating flow actions to generate JSON and make POST calls, and building a flow and catalog item for automation. The document outlines the necessary prerequisites and detailed instructions for each exercise to facilitate the automation process.

Uploaded by

dssnowref
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views55 pages

Guide - Automated Servicenow Application

This guide provides a step-by-step process for mass updating applications and plugins in a ServiceNow instance by creating a custom global application and two custom flow actions. It includes exercises for setting up a service account, creating flow actions to generate JSON and make POST calls, and building a flow and catalog item for automation. The document outlines the necessary prerequisites and detailed instructions for each exercise to facilitate the automation process.

Uploaded by

dssnowref
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Overview

This hands-on guide is to show you how to Mass Update Applications and Plugins within
your ServiceNow instance. The guide will walk you through creating a custom global
application, building two custom flow actions and putting it all together with a flow and
simple catalog item.

Contents
Overview ...................................................................................................................... 1
Pre-Requisites .............................................................................................................. 2
Exercise 1: Create a Global Scoped Application .............................................................. 2
Exercise 2: Create a Service Account and Credential ....................................................... 4
Create a Service Account ........................................................................................... 4
Create a Connection and Credential ........................................................................... 7
Exercise 3: Create Custom Flow Actions ....................................................................... 10
Create Flow Action called ServiceNow App Updater JSON Generator .......................... 10
Create Flow Action called ServiceNow App Updater POST Batch Install Plan ............... 23
Exercise 4: Create Flow and Catalog Item ..................................................................... 39
Create Flow called ServiceNow Application Updater Item .......................................... 39
Create Catalog Item called ServiceNow Application Updater ...................................... 55
Pre-Requisites
Integration Hub Starter

Exercise 1: Create a Global Scoped Application


The first part of this guide is to create a Global Scoped Application using ServiceNow
Studio. You could also opt to create a Scoped application as well.

1. Navigate to System Application > Studio

2. Click Create Application and click Let’s Get Started

3. Enter the following information


a. Name: ServiceNow Application Updater
b. Description: Mass update applications and plugins in ServiceNow
c. Scope: Global
4. Click Create
5. Click Continue in Studio (Advanced)
Exercise 2: Create a Service Account and Credential
In this exercise you will create the service account and credential record needed for the
automation. This exercise will show steps for basic authentication.

Create a Service Account


1. Navigate to Organization > Users

2. Click New

3. Enter in the following information


a. User ID
b. First name
c. Last name
d. Web service only: true
e. Internal Integration User: true
4. Right-Click the header and click Save

5. Click Set Password


6. In the pop-up, click Generate and copy the password (store this for later) then click
Save Password and Close the Pop-Up

a. If Password needs reset is true, make it false and Save


7. On the related list called Roles, click Edit and add the sn_cicd.sys_ci_automation
role and click Save

a. Best practice would be to create a group and associate the role to the group
and add this user record to the group

Create a Connection and Credential


1. Navigate to Connection & Credential > Connection & Credential Aliases
2. Click New

3. Enter the following values


a. Name: ServiceNow Internal CICD Credential
b. Type: Credential

4. Right-Click the header and Save


5. In the related list for Connections click New
6. Click Basic Auth Credentials
a. **Note: you could setup other credential types as well but for this we will use
Basic**

7. Enter the following values


a. Name: ServiceNow Internal CICD Basic
b. Username: <username from the account created previously>
c. Password: <password from the account created previously>

8. Click Update
Exercise 3: Create Custom Flow Actions
This is the core part of the guide – Building Custom Flow Actions that will be used within a
flow. The first custom flow action is to generate a JSON that is then passed to the second
custom flow. A big shout out to Eric Riemer who developed the Update Apps script which is
used within the first custom flow action. The second custom flow action will take the JSON
and make a POST call to the Batch Install Plan API.

Create Flow Action called ServiceNow App Updater JSON Generator


1. Navigate to Process Automation > Flow Designer

2. Click Create New and choose Action


3. Enter the following values and click Submit
a. Action name: ServiceNow App Updater JSON Generator
b. Description: Generates the JSON needed to Mass Update ServiceNow
Applications
4. Click the + icon to add a new action step
5. Type and select Script

6. Enter the following values


a. Name: Generate JSON
b. Inputs: none
c. Script:
(function execute(inputs, outputs) {
var currentVersion = gs.getProperty('glide.buildtag');
var counter = 0;
var currentDate = new GlideDateTime();
var installPlanName = "Update Apps - " + currentVersion + " " +
currentDate; //This is the display name of your Batch Install Plan
var loadDemoData = false; //Want Demo Data with the app?
var updateCheck = false; //this can take some time to run and adds a
LOT of stuff to the load making the important bit harder to find
if (updateCheck)
new sn_appclient.UpdateChecker().checkAvailableUpdates();
var prevName;
var appsArray = [];
var grSSA = new GlideRecord('sys_store_app');
grSSA.addEncodedQuery('install_dateISNOTEMPTY^hide_on_ui=false^vendor=
ServiceNow^ORvendorISEMPTY');
grSSA.orderBy('name');
grSSA.orderBy('version');
grSSA.query();
while (grSSA.next()) {
var curName = grSSA.getValue('name');
var latestVersion = updateAvailable(grSSA);
if (curName == prevName) {
continue;
}
if (latestVersion) {
prevName = curName;
counter++;
var appObject = {
displayName: curName,
id: grSSA.getUniqueValue(),
load_demo_data: loadDemoData,
type: "application",
requested_version: grSSA.getValue('latest_version')
};
appsArray.push(appObject);
}
}
function updateAvailable(grSSA) {
var installedVersion = grSSA.getValue('version');
var latestVersion = grSSA.getValue('latest_version');
var installedArray = installedVersion.split('.');
var latestArray = latestVersion.split('.');
var len = Math.max(installedArray.length, latestArray.length);
for (var i = 0; i < len; i++) {
var installed = installedArray[i] ?
parseInt(installedArray[i]) : 0;
var latest = latestArray[i] ? parseInt(latestArray[i]) : 0;
if (installed < latest) {
return true;
} else if (installed > latest) {
return false;
}
}
return false;
}
if (appsArray.length > 0) {
var appsPackages = {};
appsPackages.packages = appsArray;
appsPackages.name = installPlanName;
var data = new global.JSON().encode(appsPackages);
}
outputs.appcounter = counter;
outputs.currentversion = currentVersion;
outputs.appsjson = data;
})(inputs, outputs);

d. Outputs:
i. appCounter
1. Type: integer
2. Mandatory: true
ii. currentVersion
1. Type: string
2. Mandatory: true
iii. appsJSON
1. Type: JSON
2. Mandatory: true
7. Click on Outputs
8. Click Create Output

9. Create the following outputs


a. NOTE: to add more outputs click Create Outputs
b. currentVersion
i. Type: string
ii. Mandatory: true
c. appCounter
i. Type: integer
ii. Mandatory: true
d. appsJSON
i. Type: JSON
ii. Mandatory: True

10. Click Exit Edit Mode

11. Map the data pills from the Generate JSON script step to each Action Output
a. You can drag and drop the data pills from the far right onto the Action Output
value
12. Click Save and click Test

13. Check Run test in background and click Run Test

14. Click the Your test has finished running. View the Action execution details.
15. Confirm there are no errors and outputs have data

16. Copy the output from the appsJSON by clicking on the value and then copying from
the pop-up and close the pop-up
a. Store this somewhere as it be used in a later step
b. Sample Payload

{"result":{"links":{"progress":{"id":"c4ef7f4c83af92109ddc53a6feaad386","u
rl":"https://dev188302.service-
now.com/api/sn_cicd/progress/c4ef7f4c83af92109ddc53a6feaad386"},"results":
{"id":"08efb78883af92109ddc53a6feaad32f","url":"https://dev188302.service-
now.com/api/sn_cicd/app/batch/results/08efb78883af92109ddc53a6feaad32f"},"
rollback":{"id":"08efb78883af92109ddc53a6feaad32f","url":"https://dev18830
2.service-
now.com/api/sn_cicd/app/batch/rollback/08efb78883af92109ddc53a6feaad32f"}}
,"status":"0","status_label":"Pending","status_message":"","status_detail"
:"","error":"","percent_complete":0}}
17. Click back on the Action tab and close the Test Action window and Click Publish
18. Click Publish
Create Flow Action called ServiceNow App Updater POST Batch Install
Plan
1. Click the + icon and click Action

2. Enter the following values and click Submit


a. Action name: ServiceNow App Updater POST Batch Install Plan
b. Description: Makes a POST call to the Batch Install Plan
3. Click Create Input

4. Create the following Inputs


a. appsJSON
i. Type: JSON
ii. Mandatory: true
5. Add a Script step with the following values
a. Name: Instance URI
b. Inputs: none
c. Script:

(function execute(inputs, outputs) {

outputs.instanceuri = gs.getProperty('glide.servlet.uri');

})(inputs, outputs);

d. Outputs:
i. instanceURI
1. Type: string
2. Mandatory: true
6. Add REST action step

7. Enter the following values


a. Name: POST Batch Install Plan
b. Connection: Define Connection Inline
c. Credential Alias: ServiceNow_Internal_CICD_Credential
d. Base URL: [script step data pill output
instanceURI]app/sn_cicd/app/batch/install
e. Build Request: Manually
f. HTTP Method: POST
g. Request Type: Text
h. Request Body:
8. Click Test REST Step

9. Enter the following values and click Run Test


a. appsJSON: Copy and Paste the output from Exercise 3 Step 16 into the
payload field
i. NOTE: if you did not save the payload you can retest the previous Flow
Action that was created in Exercise 3 and get the appsJSON payload
b. instanceURI: https://YOURINSTANCE.service-now.com/
i. replace YOURINSTANCE with your instance name
10. Validate you get a 200 status code
19. Copy the output from the Response Body by clicking on the value and then copying
from the pop-up and close the pop-up
a. Store this somewhere as it be used in a later step
11. Close the Test REST Step pop-up
12. Add JSON Parser action step

13. Enter the following values


a. Name: JSON Parser Batch Install Plan
b. Source data: [POST Batch Install Plan.Response Body]
c. Payload: Copy and Paste the output from Exercise 3 Step 16 into the payload
field
i. NOTE: if you did not save the payload you can retest the previous Flow
Action that was created in Exercise 3 and get the appsJSON payload
ii. Sample Result

{"result":{"links":{"progress":{"id":"c4ef7f4c83af92109ddc53a6f
eaad386","url":"https://dev188302.service-
now.com/api/sn_cicd/progress/c4ef7f4c83af92109ddc53a6feaad386"}
,"results":{"id":"08efb78883af92109ddc53a6feaad32f","url":"http
s://dev188302.service-
now.com/api/sn_cicd/app/batch/results/08efb78883af92109ddc53a6f
eaad32f"},"rollback":{"id":"08efb78883af92109ddc53a6feaad32f","
url":"https://dev188302.service-
now.com/api/sn_cicd/app/batch/rollback/08efb78883af92109ddc53a6
feaad32f"}},"status":"0","status_label":"Pending","status_messa
ge":"","status_detail":"","error":"","percent_complete":0}}
d. DEVELOPER TIP: if you want to format your code select all the code by
pressing CTRL+A then pressing ALT+SHIFT+F
14. Click Generate Target

15. Confirm the Target object is created


16. Click Outputs

17. Add a Script Step

18. Enter the following values


a. Name: Results URL
b. Inputs:
i. results_id
1. Mapped with data pill [JSON Parser
Step.root.result.links.results.id]
ii. instance_uri
1. Mapped with data pill [Instance URI.instance_uri]
c. Script

(function execute(inputs, outputs) {

var table = 'sys_batch_install_plan';

var targetURL = inputs.instance_uri +


'now/nav/ui/classic/params/target/' + table + '.do?sys_id=' +
inputs.results_id;

outputs.results_url = targetURL;

})(inputs, outputs);

d. Outputs:
i. results_uri
1. Type: string
2. Mandatory: true
19. Create Ouputs
a. Results sysID
i. Type: string
ii. Mandatory: true
b. Results URL
i. Type: string
ii. Mandatory: true
20. Map Data Pills to the Ouputs
a. Results sysID
i. Mapped with data pill [JSON Parser Step.root.result.links.results.id]
b. Results URL
i. Mapped with data pill [Results URL.results_url]

21. Click Save and Publish


Exercise 4: Create Flow and Catalog Item
Now the guide will help you build out a flow that will utilize the two custom flow actions
created previously. The flow will then be used by a new catalog item to fulfill the automated
mass update of applications within your instance.

Create Flow called ServiceNow Application Updater Item


1. Click the + icon and click Flow

2. Enter the following values


a. Flow name: ServiceNow Application Updater Item
b. Description: Catalog Item Fulfillment
c. Run as: System User
3. Click Add a trigger

4. Choose Application > Service Catalog


5. Click Done

6. Add Action Create Catalog Task


7. Enter the following values
a. Requested item:
i. [Trigger.Requested Item]
b. Short Description:
i. Automation: [Trigger.Requested Item.Item.Name]
c. Add field values
i. Assignment group: Service Desk
1. Recommendation: create a new group for automations so that
you can assign automation records to it without triggering
notifications to actual fulfillers
d. Wait: False

8. Add Annotation
a. Automation: ServiceDesk
9. Add Action ServiceNow App Updater JSON Generator and click Done

10. Add Flow Logic If


11. Enter the following values and click Done
a. Condition label: appCounter is 0
i. DEVELOPER TIP: adding condition labels makes the flow more
human readable
b. Condition 1:
i. [2 – ServiceNow App Updater JSON Generator.appCounter] is 0
12. Add Action Update Record

13. Enter the following values and click Done


a. Record: [1 – Create Catalog Task.Catalog Task]
b. Table: (auto-fills)
c. Fields:
i. State: Closed Skipped
ii. Work notes:
1. Auto-closing: No applications to update
d. Annotation: State, Work notes
i. DEVELOPER TIP: when updating records recommendation is to add
what is being changed as this makes it much easier to identify where
and what information is being updated within the flow

14. Add Flow Logic End Flow


15. Add Action Update Record

16. Enter the following values and click Done


a. Record: [1 – Create Catalog Task.Catalog Task]
b. Table: (auto-fills)
c. Fields:
i. Description:
1. Current version: [2 – ServiceNow App Updater JSON
Generator.currentVersion]
ii. Work notes:
1. Calling Batch Install Plan Action
Apps to update: [2 – ServiceNow App Updater JSON
Generator.appCounter]

appsJSON:
[2 – ServiceNow App Updater JSON Generator.appsJSON]

17. Add Action ServiceNow App Updater POST Batch Install Plan

18. Enter the following values and click Done


a. appsJSON: [2 – ServiceNow App Updater JSON Generator.appsJSON]

19. Add Action Update Record


20. Enter the following values and click Done
a. Record: [1 – Create Catalog Task.Catalog Task]
b. Table: (auto-fills)
c. Fields:
i. Work notes:

ResultsURL:

[code]<a href="[7 – ServiceNow App Updater POST Batch Install


Plan.Results URL]">[7 – ServiceNow App Updater POST Batch
Install Plan.Results sysID]</a>[/code]

a. Note: you can copy and paste the following code into
the work notes to get started, the italicized brackets
(from above) are meant to show data pills. The [code]
and [/code] are actual code that needs to be within the
Work notes. Then just add the data pills so it matches
the above.

ResultsURL:
[code]<a href=""></a>[/code]
2. NOTE: If HTML tags are not allowed in your instance you can
setup this step to just paste the sysID or URL or both
b. Annotation: Work notes
21. Add Action Look Up Record

22. Enter the following values and click Done


a. Table: sys_batch_install_plan
i. Note: You may need to update the following property to include the
table from above – Property sn_flow_designer.allowed_system_tables.
b. Condition:
i. Sys ID is [7 – ServiceNow App Updater POST Batch Install Plan.Results
sysID]
c. Annotation: Batch Install Plan by sysID
23. Add Action Wait for Condition

24. Enter the following values and click Done


a. Record: [9 – Look Up Record.Batch Install Plan Record]
b. Table: (auto-fills)
c. Conditions
i. State is not Ready
d. Annotation: State is not one of Ready, In progress
25. Add Flow Logic If

26. Enter the following values and click Done


a. Condition label: State is not Installed
b. Condition 1:
i. [9 – Look Up Record.Batch Install Plan Record.State] is not Installed

27. Add Action Update Record


28. Enter the following values and click Done
a. Record: [1 – Create Catalog Task.Catalog Task]
b. Table: (auto-fills)
c. Fields:
i. Work notes:

There was an issue with the Batch install

State: [9 – Look Up Record.Batch Install Plan Record.State]

ii. Annotation: Work notes

29. Add Flow Logic Else


30. Add Action Update Record

31. Enter the following values and click Done


a. Record: [1 – Create Catalog Task.Catalog Task]
b. Table: (auto-fills)
c. Fields:
i. Work notes:

There was an issue with the Batch install

State: [9 – Look Up Record.Batch Install Plan Record.State]

ii. Annotation: State is Closed Complete, Work notes


32. Your flow should match
33. Click Save and Activate

Create Catalog Item called ServiceNow Application Updater


1. Create a new catalog and map the flow created from above to the catalog item
2. Order the catalog item and validate the flow runs fully

CONGRATULATIONS! YOU HAVE BUILT A CUSTOM APPLICATION THAT USES ONE


CATALOG ITEM, ONE FLOW, TWO CUSTOM FLOW ACTIONS TO MASS UPDATE YOUR
SERVICENOW INSTANCE’S APPLICATIONS AND PLUGINS!

You might also like