print items second script
To print the RITM Number, REQ Number, Catalog Item, Task Number, Task Description, Task Assignment Group, and Task State on separate lines in your second script, you can iterate through the result and use print() statements for each field individually.
Here’s how you can modify the second script to achieve that:
Modified extract_req.py Script:
pythonimport requests
import json
import os
# Replace with actual ServiceNow auth token
x_snow_auth = os.environ.get('X_SNOW_AUTH', 'your_auth_token_here')
def extract_req_details():
# Read the request_number from the file
with open("request_number.txt", "r") as f:
request_number = f.read().strip()
headers = {
'x-snow-auth': x_snow_auth,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
payload = [
{ "$match": { "request.display_value": request_number } },
{ "$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"
}}
]
response = requests.post(
'https://atlas-api.ubsdev.net/api/now/table/<table_name>', # Replace with correct API endpoint
headers=headers,
data=json.dumps(payload)
)
if response.status_code == 200:
req_details = response.json()
# Extract required fields
for item in req_details:
ritm_number = item.get("RITM Number", "N/A")
req_number = item.get("REQ Number", "N/A")
catalog_item = item.get("Catalog Item", "N/A")
task_number = item.get("Task Number", "N/A")
task_description = item.get("Task Description", "N/A")
task_assignment_group = item.get("Task Assignment Group", "N/A")
task_state = item.get("Task State", "N/A")
# Print each field in a separate line
print(f"RITM Number: {ritm_number}")
print(f"REQ Number: {req_number}")
print(f"Catalog Item: {catalog_item}")
print(f"Task Number: {task_number}")
print(f"Task Description: {task_description}")
print(f"Task Assignment Group: {task_assignment_group}")
print(f"Task State: {task_state}")
print("-" * 40) # Separator between tasks
else:
print(f"Failed to retrieve REQ details. Status Code: {response.status_code}")
print(response.text)
if __name__ == "__main__":
extract_req_details()
Explanation of Changes:
Iterating through the response:
- The script iterates over the response to extract values for each field (
RITM Number,REQ Number, etc.).
- The script iterates over the response to extract values for each field (
Extracting fields:
- The
.get()method is used to safely extract values from the dictionary. If a key doesn’t exist, it returns"N/A".
- The
Printing each field:
- Each field is printed on a separate line using
print(f"Field: {value}").
- Each field is printed on a separate line using
Separator:
- A line (
"-" * 40) is printed between each set of task details for better readability.
- A line (
Output Example:
yamlRITM Number: RITM123456
REQ Number: REQ0011458
Catalog Item: Laptop
Task Number: TASK1234
Task Description: Install Software
Task Assignment Group: IT Support
Task State: Open
----------------------------------------
Running the Script:
To test this:
Ensure you have already generated the
request_numberfrom the first script.Run this second script as follows:
bashpython extract_req.py
This will print the details line-by-line as specified.
No comments