help needed for api call
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2024 02:40 PM
hi
i have a field called u_imapcted_asset but when i put into the Rest API i am not getting any result . not understanding why
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2024 03:56 AM
Share your code where you are trying to pass serial number and tag.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2024 05:47 AM
import requests
from flask import Flask, request, render_template, jsonify
import os
app = Flask(__name__)
# ServiceNow API Configuration
SERVICENOW_URL = "https://your_instance.service-now.com"
SERVICENOW_USERNAME = "your_username"
SERVICENOW_PASSWORD = "your_password"
def fetch_servicenow_incidents(hostname):
"""
Fetch incidents from ServiceNow for the given hostname.
"""
api_url = f"{SERVICENOW_URL}/api/now/table/incident"
query = f"cmdb_ci.name={hostname}" # Adjust the query to match your instance fields
headers = {"Accept": "application/json"}
try:
response = requests.get(
api_url,
auth=(SERVICENOW_USERNAME, SERVICENOW_PASSWORD),
headers=headers,
params={"sysparm_query": query, "sysparm_limit": 50},
)
response.raise_for_status() # Raise an exception for HTTP errors
data = response.json()
if "result" in data:
incidents = data["result"]
return incidents
else:
return []
except requests.exceptions.RequestException as e:
print(f"Error fetching incidents from ServiceNow: {e}")
return []
@app.route('/')
def index():
return render_template('index.html')
@app.route('/ask', methods=['POST'])
def ask():
user_query = request.json.get('query', '').strip()
if not user_query:
return jsonify({"error": "Please enter a valid hostname."})
try:
# Fetch incidents from ServiceNow
incidents = fetch_servicenow_incidents(user_query)
# Prepare incidents for analysis
if incidents:
incident_summary = [
{
"Number": incident.get("number", "N/A"),
"Short Description": incident.get("short_description", "N/A"),
"State": incident.get("state", "N/A"),
"Created": incident.get("sys_created_on", "N/A"),
"Priority": incident.get("priority", "N/A"),
}
for incident in incidents
]
else:
incident_summary = [{"message": "No incidents found for the hostname."}]
# Perform analysis on incidents
analysis = perform_analysis(incident_summary)
return jsonify({
"hostname": user_query,
"incident_summary": incident_summary,
"analysis": analysis,
})
except Exception as e:
return jsonify({"error": f"An unexpected error occurred: {str(e)}"})
def perform_analysis(incidents):
"""
Analyze the incidents to provide insights.
"""
if not incidents or isinstance(incidents[0], dict) and "message" in incidents[0]:
return "No incidents available for analysis."
# Example analysis logic
open_incidents = [inc for inc in incidents if inc["State"] not in ("Resolved", "Closed")]
high_priority_incidents = [inc for inc in incidents if inc["Priority"] in ("1", "2")]
analysis = [
f"Total incidents: {len(incidents)}",
f"Open incidents: {len(open_incidents)}",
f"High-priority incidents: {len(high_priority_incidents)}",
]
return "\n".join(analysis)
if __name__ == '__main__':
app.run(debug=True)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2024 05:55 AM
query = f"cmdb_ci.name={hostname}"
Replace this line with below code.
var hostName = {hostname};
var gr = new GlideRecord('alm_hardware'); // replace with your table name
gr.addQuery('name', hostName );
gr.query();
if(gr.next()) {
query = "u_impacted_assetLIKE"+gr.sys_id;
}
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2024 05:57 AM
@Runjay Patel the rest is good ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2024 06:01 AM
I didnt check that, i just help with you that how you will pass the query with sysid.
You can take help of this blog to make use of script in your scripted REST API.
https://servicenowwithrunjay.com/scripted-rest-api-servicenow/
You can also check this video.