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:
pythonHeaders: {'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:
pythonheaders = {
'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:
Match Stage:
json{ "$match": { "request.display_value": "REQ0011387" } }- This matches documents where the
request.display_valueis equal toREQ0011387. This filters the collection to only include the REQ of interest.
- This matches documents where the
Lookup Stage:
json{ "$lookup": { "from": "sc_task", "localField": "_id", "foreignField": "parent.sys_id", "as": "sc_task" } }- $lookup performs a join with the
sc_taskcollection.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 theparent.sys_idin thesc_taskcollection.as: "sc_task": The result of the lookup is stored in a new field calledsc_task.
- $lookup performs a join with the
Unwind Stage:
json{ "$unwind": "$sc_task" }- $unwind deconstructs the
sc_taskarray and outputs one document for each element of the array. If multiple tasks are related to the REQ, each one will be processed individually.
- $unwind deconstructs the
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
projectstage 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" } }- "Task Assignment Group": The field contains a typo. It should be
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_valueis "REQ0011387". - $lookup: Joins the
sc_taskcollection to retrieve tasks where theparent.sys_idmatches the REQ's_id. - $unwind: Breaks apart the
sc_taskarray, 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:
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-authtoken).
Testing via Postman:
- You can use tools like Postman to manually test the query by sending the headers and payload in a
POSTrequest. Paste the aggregation pipeline in the body of the request as raw JSON.
- You can use tools like Postman to manually test the query by sending the headers and payload in a
Using Python to Execute: You can run this query using Python's
requestslibrary to interact with the ServiceNow API. Example:pythonimport 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)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