Create a data stream action to get user activity
- UpdatedAug 3, 2023
- 7 minutes to read
- Vancouver
- IT Asset Management
Create a data stream action to get user activity from a SaaS application.
Before you begin
If you're using an existing ServiceNow® Integration Hub spoke, find out if it has a data stream action to get user activity that you can use instead of creating one.
For more information about data stream actions, see Data Stream actions.
Role required: flow_designer or admin
About this task
Before creating the data stream action, decide how you want to define meaningful user activity. Meaningful activity can be a combination of user actions. Create a separate data stream action for each user activity metric. For example, the base system Webex Meetings integration defines meaningful activity as hosting a meeting. It uses one data stream action to get the dates of the most recently hosted meetings for all users. If you also wanted the Webex Meetings integration to include logging in as meaningful activity, you would create a second data stream action to get the most recent login times for all users.
Your data stream action to get users may return a user activity metric such as last login time. In this case, you don't need to create a data stream action to get user activity or a subflow to get user activity unless you want to define additional user activity metrics. Make sure that your subflow to get users sets this user activity metric as the Last activity input in the Upsert User Subscription action.
Procedure
- Navigate to All > Flow Designer > Designer.
- Click New and then select Data Stream.
-
On the form, fill in the fields.
Table 1. Action Properties form Field Value Name Name of your choice. For example, Get User Activity. Accessible From All application scopes. Category Leave this field empty. Protection None. Application Spoke app to integrate with the SaaS application. This spoke app can be an existing Integration Hub spoke or a new spoke that you created. In-Flow Annotation Leave this field empty. Description Description of your choice. - Click Submit.
- In the Inputs section of the Action Outline, click Create Input.
-
Add a look back time input.
Table 2. Inputs Label Name Type Mandatory Look back time look_back_time Date/Time Yes -
If the API that you're working with requires user authentication for requests,
add inputs for authentication.
Examples of common user authentication inputs are admin user id and site name. See the documentation for your chosen API to learn about the requirements for user authentication in your specific case. If the API requires an access token, a Credential Value variable is automatically created later. You don't need to add an access token as an input.
When you use your completed data stream action in a subflow, you define what values to pass as these inputs.
- Click Request in the Action Outline.
-
On the form, fill in the fields.
Table 3. Request form Field Value How will you get data Choose either REST Step or SOAP step. Your choice depends on the API for the SaaS application that you're integrating with. Enable pagination Selected. Run a script before each request Not selected. - Click Pagination Setup step in the Action Outline.
-
Define pagination variables based on the query parameters used by the SaaS API.
If you're using offset-based pagination, use the Limit / Offset pagination template to preload the pagination configuration.
Note: The value of the reservedgetNextPage
variable determines whether to request another page of results. As long as thegetNextPage
variable is true, the action continues to send requests for the next page. -
Write a Pagination Variables Script to update the pagination variables.
The script runs on each request. If you're using a pagination template, adjust the preloaded script as needed.
The following image shows a completed example of the pagination setup step. This example is from the Get Users data stream action used in the Webex Download Subscriptions subflow.
Note: Pagination variables only support the string data type. To perform math operations, convert the value to an integer, perform any required operations, then convert it back to a string. - Click SOAP step or REST step in the Action Outline depending on the option that you selected for how you will get data.
-
If you selected SOAP, fill in the form as shown.
Table 4. SOAP step form Field Value Connection Details Connection Use Connection Alias. Connection Alias Connection alias that you created when you created the integration profile. If you have not yet created an integration profile, follow the steps to create a custom integration profile with a connection alias. Endpoint This value is automatically populated when you select the connection alias. It's set to the Connection URL from the HTTP(s) Connection record linked to the alias. Request Details Build Envelope Manually. SOAP Action API request to get a list of meaningful user activity. For example, the base system Webex Meetings integration defines meaningful user activity as hosting a meeting so that it uses the LstsummaryMeeting
request to get a list of all meetings. See the documentation for your chosen API to select the appropriate request.SOAP Envelope XML request message to get a list of all users. See the documentation for your chosen API to learn how to write an XML request message. In general, the header should have your input variables for user authentication as well as the Credential Value variable as the access token. The body should include the request to get a list of meaningful user activities, a start date set as the Look back time input, and your variables from the pagination setup step. Note: For an example of a SOAP envelope, see the Get User Activity data stream action used in the Webex Update User Activity subflow. -
If you selected REST, fill in the form as shown.
Table 5. REST step form Field Value Connection Details Connection Use Connection Alias. Connection Alias Connection alias that you created when you created the integration profile. If you have not yet created an integration profile, follow the steps to create a custom integration profile with a connection alias. Base URL This value is automatically populated when you select the connection alias. It's set to the Connection URL from the HTTP(s) Connection record linked to the alias. Request Details Build Request Manually. Resource Path Path to the resource. This value gets appended to the Base URL. See the documentation for the API that you're working with to learn how to construct the resource path. HTTP Method GET. Query Parameters Add parameters for pagination. Set the values as the variables that you created in the Pagination Setup step. Add another parameter for the start date so that the request returns results from the start date to the current date. Set the value as the Look back time input.Note: Make sure the Look back time input date/time variable is correctly formatted for the API that you're working with. If you need to reformat or convert to another data type such as a string, you can do this in the action preprocessing script step.The following image shows a completed example of the REST step. This example is from the Get Audit Log data stream action used in the Jira Update User Activity subflow.
- Click Parsing in the Action Outline.
-
On the form, fill in the fields.
Table 6. Parsing form Field Value How will you identify each record JSON/XML Splitter How will you parse each item into an object Script Parser - Click Splitter step in the Action Outline.
-
On the form, fill in the fields.
Table 7. Splitter step form Field Value Source Format Select XML or JSON depending on the format returned by the API response. Item Path Absolute path to a meaningful activity element in the response message. See the documentation for the API that you're working with for information about the format of the response message. - Example XML item path:
/message/body/meeting
- Example JSON item path:
$.data.meeting
- Example XML item path:
- Click Outputs in the Action Outline.
-
Click Create Output and edit the variable as
shown.
Table 8. Action Output Label Name Type Mandatory targetObject targetObject Object No -
Add child items for targetObject to store the user email and
the date of meaningful activity.
For example, an XML response might look like this.For this response, add the child items as shown.
<message> <body> <meeting> <meetingID>12345</meetingID> <startDate>08/13/2019 20:08:16</startDate> <hostEmail>email@email.com</hostEmail> </meeting> <meeting> ... </meeting> </body> </message>
Table 9. Child items for targetObject Label Name Type Mandatory email email String No last_activity last_activity String No - In the Action Outline, click Script Parser step.
-
Create a targetObject output object for each meaningful activity element in the response and then map each the activity date and user email to the targetObject child items.
The parser script is executed for each user element.
Note: These examples show the types of elements that could be contained in a response. Don't directly copy these scripts. Use element names from the documentation for the API that you're working with.Example script that parses an XML response.(function parse(inputs, outputs) { var xmlDoc = new XMLDocument(inputs.sourceItem, false); outputs.targetObject.email = xmlDoc.getNodeText('/meeting/hostEmail'); outputs.targetObject.last_activity = xmlDoc.getNodeText('/meeting/startDate'); })(inputs, outputs)
Example script that parses a JSON response.(function parse(inputs, outputs) { var record = JSON.parse(inputs.sourceItem); outputs.targetObject.email = record.hostEmail; outputs.targetObject.last_activity = record.startDate; })(inputs, outputs)
-
To test your data stream action, click Test.
-
View the test results and system logs for details about any
errors.
To view system logs, navigate to System Logs > System Log > All.
- If your data stream action has errors, make sure that you're using the correct endpoints and that the API requests and responses are structured as expected.
-
View the test results and system logs for details about any
errors.
- After verifying that the data stream action is working as expected, click Publish.