Second script

 The second script seems to be a MongoDB aggregation pipeline designed to extract details related to a specific REQ (Request) in a ServiceNow environment. It's querying a MongoDB-like database (or a ServiceNow instance using a similar query structure) to match a specific REQ number, then looking up tasks associated with it, and finally projecting certain fields. Let's go through the script step by step and correct any issues.

1. Headers:

python
Headers: {'x-snow-auth': 'xxxxx'}
  • x-snow-auth: This header likely contains an authentication token for ServiceNow's API. Make sure you store and manage this token securely, potentially using Azure Key Vault or environment variables, instead of hardcoding it into the script.

Corrected header example:

python
headers = { 'x-snow-auth': 'xxxxx', # Replace with a securely stored token 'Content-Type': 'application/json', 'Accept': 'application/json' }

2. Payload (MongoDB Aggregation Pipeline):

The payload is a query structure used in MongoDB or a similar API for aggregating data. Each stage of the aggregation pipeline processes documents as they flow through. Here's an explanation and the possible corrections:

  1. Match Stage:

    json
    { "$match": { "request.display_value": "REQ0011387" } }
    • This matches documents where the request.display_value is equal to REQ0011387. This filters the collection to only include the REQ of interest.
  2. Lookup Stage:

    json
    { "$lookup": { "from": "sc_task", "localField": "_id", "foreignField": "parent.sys_id", "as": "sc_task" } }
    • $lookup performs a join with the sc_task collection.
      • from: "sc_task": Specifies the collection from which to fetch related documents.
      • localField: "_id": Refers to the field in the current document (_id) that corresponds to the parent.sys_id in the sc_task collection.
      • as: "sc_task": The result of the lookup is stored in a new field called sc_task.
  3. Unwind Stage:

    json
    { "$unwind": "$sc_task" }
    • $unwind deconstructs the sc_task array and outputs one document for each element of the array. If multiple tasks are related to the REQ, each one will be processed individually.
  4. Project Stage (Issues Detected):

    json
    { "$project": { "RITM Number": "$number", "REQ Number": "$request.display_value", "Catalog Item": "$cat_item.display_value", "Task Number": "$sc_task.number", "Task Description": "$sc_task.short_description", "Task Assignment Group": "$sc_task.assignment task.assignment_group.display_value", " Task State": "$sc_task.state.display_value", "Task Assigned To": "$sc_task.assigned_to.display_value" } }

    This stage selects and renames fields to output only the necessary information. However, there are a couple of issues:

    • "Task Assignment Group": The field contains a typo. It should be "$sc_task.assignment_group.display_value" instead of "$sc_task.assignment task.assignment_group.display_value".
    • " Task State": There is an extra space in the field name. While it's valid in MongoDB, it's not a good practice to have extra spaces in field names.

    The corrected project stage would look like this:

    json
    { "$project": { "RITM Number": "$number", "REQ Number": "$request.display_value", "Catalog Item": "$cat_item.display_value", "Task Number": "$sc_task.number", "Task Description": "$sc_task.short_description", "Task Assignment Group": "$sc_task.assignment_group.display_value", "Task State": "$sc_task.state.display_value", "Task Assigned To": "$sc_task.assigned_to.display_value" } }

Full Corrected Payload:

json
[ { "$match": { "request.display_value": "REQ0011387" } }, { "$lookup": { "from": "sc_task", "localField": "_id", "foreignField": "parent.sys_id", "as": "sc_task" } }, { "$unwind": "$sc_task" }, { "$project": { "RITM Number": "$number", "REQ Number": "$request.display_value", "Catalog Item": "$cat_item.display_value", "Task Number": "$sc_task.number", "Task Description": "$sc_task.short_description", "Task Assignment Group": "$sc_task.assignment_group.display_value", "Task State": "$sc_task.state.display_value", "Task Assigned To": "$sc_task.assigned_to.display_value" } } ]

3. Explanation of the Aggregation Pipeline:

  • $match: Filters the documents to only include those where request.display_value is "REQ0011387".
  • $lookup: Joins the sc_task collection to retrieve tasks where the parent.sys_id matches the REQ's _id.
  • $unwind: Breaks apart the sc_task array, so that each task is processed as a separate document.
  • $project: Selects and renames the fields to only output the required details.

4. How to Test:

To test the script:

  1. ServiceNow API Endpoint:

    • Make sure you have access to the ServiceNow instance with the appropriate permissions to read from the catalog and task collections.
    • The endpoint for running this aggregation might be a ServiceNow-specific API, for example:
      perl
      POST https://<instance>/api/now/table/<table_name>
    • Use the correct authentication method (as suggested, you're using the x-snow-auth token).
  2. Testing via Postman:

    • You can use tools like Postman to manually test the query by sending the headers and payload in a POST request. Paste the aggregation pipeline in the body of the request as raw JSON.
  3. Using Python to Execute: You can run this query using Python's requests library to interact with the ServiceNow API. Example:

    python
    import requests import json headers = { 'x-snow-auth': 'xxxxx', # Replace with your auth token 'Content-Type': 'application/json', 'Accept': 'application/json' } payload = [ { "$match": { "request.display_value": "REQ0011387" } }, { "$lookup": { "from": "sc_task", "localField": "_id", "foreignField": "parent.sys_id", "as": "sc_task" } }, { "$unwind": "$sc_task" }, { "$project": { "RITM Number": "$number", "REQ Number": "$request.display_value", "Catalog Item": "$cat_item.display_value", "Task Number": "$sc_task.number", "Task Description": "$sc_task.short_description", "Task Assignment Group": "$sc_task.assignment_group.display_value", "Task State": "$sc_task.state.display_value", "Task Assigned To": "$sc_task.assigned_to.display_value" } } ] url = "https://<instance>/api/now/table/<table_name>" # Replace with actual endpoint response = requests.post(url, headers=headers, data=json.dumps(payload)) print(response.status_code) print(response.text)
  4. Logging:

    • Add logging to capture any errors or exceptions in case the query fails or returns an unexpected result.

By following these steps, you can extract the REQ details and test the script effectively. Let me know if you need further details or adjustments!

No comments

Theme images by tjasam. Powered by Blogger.