- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on ‎11-09-2020 04:08 AM
Before we had flow designer in servicenow, outbound integration with any other 3rd party tool was done by creating an outbound message and then creating a business rule to call that outbound message, which include lot of coding. This article will talk about how can you convert this configuration in a flow designer which needs less coding and hence more readable.
Pre-Requisites
You would need minimum two plugins in order to use REST APIs in flow designer.
1. IntegrationHub - This enables you to use REST step in flow designer Action.
2. ServiceNow IntegrationHub Enterprise Pack Installer - This lets you use dynamic inputs in flow designer.
Refer to this link for more details about integration plugins for flow designer.
Use Case
Existing
There are 2 servicenow instances ( in this case, I'll use 2 PDIs) and when incident is created on one instance ( Let's call it Source Instance A), same incident gets created in other instance , (lets call it Target Instance B). When incident is created in instance B, instance A gets the response and a field (in this case correlation_id , however you can use any other field ) on the incident form ( in instance A) gets populated with the incident number which is created in instance B. So correlation in instance A is the incident number of instance B.
New Requirement
When we update an incident in instance A , the corresponding incident number ( mentioned in correlation id) gets updated in instance B. For this example , we will update priority , state and short description from target instance A to source Instance B
Note: we will send the data via flow designer, however I am assuming that you already have a a scripted REST API /logic created in Target instance B which will accept the inputs we'll send from the flow designer. At the end, I'll give the snapshot of the logic I have defined in scripted rest API
Implementation
There are two parts to it.
1. Flow designer
2. Action
So the approach is that first create the action and then test it. Once it is tested successfully, this action can be called by a flow designer. Follow below steps to create action:
1. Navigate to Flow Designer > Designer in navigation pane.
2. you will get a new window. Click on Action and create a new action by clicking "New" button as shown below
3. Provide the name of the action and leave other details as it is and click submit.
Note: If you are working in any specific scope, make sure that is selected in "Application"
4. You'll get new screen as below where you can see inputs and outputs. In this case, inputs are "priority", "state" and "short description" which we need to update on instance B. Additionally you need to provide a lookup number ( in this case correlation id) which will define which incident to be updated in instance B
Note: My scripted rest API accepts correlation id in string parameter, however other inputs in the body of request. This may vary from case to case base don what configurations are you using
5. Now let's define inputs. So in this case there are 4 inputs ( correlation id, state, priority, short description) which need to be sent to Target instance B.
Click on create input on right hand side and create 4 input variables. No need to worry about the names. These need not be same as of backend names, however it will be good if you use similar names so that it is easy for you to understand later.
6. Save your work you have done so far. Click on the "+" sign you see in green color between Inputs and Outputs in left panel and select "REST" step under integrations. The step would be added and would look like below
7. Perform the configurations as below
Connection : "Define Connection Inline
Connection Alias : Select the + sign against this field , Select Basic auth credentials, and provide the details of the user who has access on the target instance B , select any available credentials alias and submit the user configuration
Base URL : Base url of third party tool. in this case it is servicenow so it will be something like https://devXXXXX.service-now.com
Resource path : Path of the scripted rest api to be called at target instance B . Example : /api/sn_hr_core/updateinchr
now in this case target is accepting "Correlation id" in path parameter so provide it here at the end. Click on the data pill against this field, select inputs > correlation id . It will look something like below
HTTP Method : PATCH ( as we are updating the record)
Headers: As this integration is from servicenow to servicenow, make the accept type as JSON, otherwise provide as per your requirement.
Request type : text
Request Body : Use data pill to pull all other three inputs in Json format as shown below
Note: Please make sure that you use same variable names which are being fetched at target.
8. Save the action and click on test
9. Check the execution logs. If it does not give any errors, it indicates that it ran fine. Also you can check at target instance if the incident got updated or not.
Note: if the required action is not performed on target instance, you might need to chec the logic in scripted rest api or business rule you have used at target instance.
Once the action is working fine, PUBLISH it . it will not work unless it is published
So our action is ready, however need to make the inputs dynamic for that we will create a flow and call this action from that flow . Follow below steps for the same :
1. Flow designer > Designer . On home page create new flow as shown below
2. Provide name and set Run as : "System Administrator" . click Submit
3. Provide trigger conditions based on your requirement. Here I am running it on update when correlation id is not empty . Once trigger conditions are complete. click Done.
4. Now click on next step > Action in the search box look for the action you created and select the action
5. Add the inputs dynamically using data pills as shown below. you need to dot walk to the required fields of incident you mentioned din the from trigger condition
After filling in all the inputs, it will look something like below
6. Click on done.
7. Save the Flow
8. Test the flow
9. Activate the flow.
This completed your integration of once instance with other instance using less coding and without using outbound message or business rule.
**********************************
Below is the snapshot of scripted rest api that I used on my target instance, however this will differ based on your requirements.
- 42,367 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
That Code scripted rest API might not work. Instead use that below code which is easy to use and understand. And pass the value for 'state' and 'priority' without quotes from Action. So, no need to parse it into integer at 'scripted rest api' end.
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
gs.log("body - "+ JSON.stringify(request.body.data, null, 4));
var correlation_id = request.body.data.correlation_id;
var state = request.body.data.state;
var short_desc = request.body.data.short_description;
var grIncident = new GlideRecord('incident');
grIncident.addQuery('correlation_id', correlation_id);
grIncident.query();
if (grIncident.next()) {
grIncident.state = state;
grIncident.impact = 1;
grIncident.urgency = 1;
grIncident.short_description = short_desc;
grIncident.update();
}
})(request, response);
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
So I need to create an outbound REST call for a catalog item. In the old days of workflows, I had a sub flow that simply took the input (which was a list of all the variables and their values) and build the REST on that. No need for the flow to know the key's, just build it and go.
This looks as if I need to know all the keys and not very flexible. Am i wrong with that thought ?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Could you tell more precisely about your requirement. It would be better to understand.
Regards,
Kartik
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I have worked that out now. I created an Action, that ran a script and wrote the script there.
All seems a bit of a pain , when before flow, one simple Script Include, called as required (BR, scheduled job) and all written in about 3/4 hrs.
over a day in,
action 1, get connection alias
action 2. get all variables into a json payload (cannot use object, as I have no idea what variables exist on the sc_req_item or ca_task, so needs to be dynamic)
action 3. create another json payload from alternative sources
action 4 - combine payloads into 1 json object
action 5 - post payload
And a flow ontop to call what is needed and pass whatever parameters.
I do have one outstanding issue which is around the credentials and how a third party source wants the information (embedded in the body) need to try a few things
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I still feel a NOOB here but what I want is to trigger a POST to an endpoint, say 'https://my.change-handler.com/changed' with a body like '{ "id": "{{ id of changed CMDP_CI or extended CMDB_CI_* entry }}" } when a mutation of a cmdb_ci (or extended) entry has taken place.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hello sir,
Can you please provide me a existing use case step by step.
Thank you so much!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thank you for help..
can you tell me how we can make for either of the instances means update can be done by instance A to B and vise versa.
Thanks!