Scripted REST API Azure Webhook not working
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2025 04:47 AM
Hi!
I am trying to create a script to create incidents in service now based on Azure alerts. This is the script in that I am using in Scripted REST API:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
try {
// Validate token
var apiKey = request.queryParams['apiKey'];
var secret = '*******' ; // token
if (apiKey == secret) {
response.setStatus(401);
response.setHeader("Content-Type", "application/json");
response.setBody(JSON.stringify({ status: "error", message: "Unauthorized" }));
return;
}
// Parse request body
var rawBody = request.body.data;
var payload = (typeof rawBody === "string") ? JSON.parse(rawBody) : rawBody;
if (!payload || !payload.data || !payload.data.essentials) {
response.setStatus(400);
response.setHeader("Content-Type", "application/json");
response.setBody(JSON.stringify({ status: "error", message: "Missing essentials in payload" }));
return;
}
var e = payload.data.essentials;
// Create incident
var gr = new GlideRecord("incident");
gr.initialize();
gr.short_description = "Azure Alert: " + e.alertRule;
gr.description =
"Severity: " + e.severity + "\n" +
"Signal Type: " + e.signalType + "\n" +
"Condition: " + e.monitorCondition + "\n" +
"Fired At: " + e.firedDateTime + "\n\n" +
"Description:\n" + e.description;
var sysId = gr.insert();
// Return success to Azure
response.setStatus(200);
response.setHeader("Content-Type", "application/json");
response.setBody(JSON.stringify({
status: "success",
message: "Incident created",
sys_id: sysId
}));
} catch (err) {
gs.error("Azure Webhook Script Error: " + err.message);
response.setStatus(500);
response.setHeader("Content-Type", "application/json");
response.setBody(JSON.stringify({
status: "error",
message: "Internal Server Error:" + err.message
}));
}
})(request, response);
When I am trying to test it, I've got this error in Azure: Failed
I've been trying to test it with Postman and I've got this:
{
"error": {
"message": "Script Evaluation Exception",
"detail": "Cannot convert {\"status\":\"error\",\"message\":\"Internal Server Error:Cannot convert {\\\"status\\\":\\\"error\\\",\\\"message\\\":\\\"Unauthorized\\\"} to org.mozilla.javascript.ScriptableObject (sys_ws_operation.e1b2d62583e1621010b97629feaad38c.operation_script; line 9)\"} to org.mozilla.javascript.ScriptableObject (sys_ws_operation.e1b2d62583e1621010b97629feaad38c.operation_script; line 53)"
},
"status": "failure"
}
"error": {
"message": "Script Evaluation Exception",
"detail": "Cannot convert {\"status\":\"error\",\"message\":\"Internal Server Error:Cannot convert {\\\"status\\\":\\\"error\\\",\\\"message\\\":\\\"Unauthorized\\\"} to org.mozilla.javascript.ScriptableObject (sys_ws_operation.e1b2d62583e1621010b97629feaad38c.operation_script; line 9)\"} to org.mozilla.javascript.ScriptableObject (sys_ws_operation.e1b2d62583e1621010b97629feaad38c.operation_script; line 53)"
},
"status": "failure"
}
Can you help me on this topic?
2 REPLIES 2
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2025 06:31 AM
Hi @AlexanderGreat ,
can you try this script:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
try {
// Validate token
var apiKey = request.queryParams['apiKey'];
var secret = '*******'; // token
if (apiKey != secret) {
response.setStatus(401);
response.setHeader("Content-Type", "application/json");
response.setBody(JSON.stringify({ status: "error", message: "Unauthorized" }));
return;
}
// Parse request body
var rawBody = request.body.data;
var payload = (typeof rawBody === "string") ? JSON.parse(rawBody) : rawBody;
if (!payload || !payload.data || !payload.data.essentials) {
response.setStatus(400);
response.setHeader("Content-Type", "application/json");
response.setBody(JSON.stringify({ status: "error", message: "Missing essentials in payload" }));
return;
}
var e = payload.data.essentials;
// Create incident
var gr = new GlideRecord("incident");
gr.initialize();
gr.short_description = "Azure Alert: " + e.alertRule;
gr.description =
"Severity: " + e.severity + "\n" +
"Signal Type: " + e.signalType + "\n" +
"Condition: " + e.monitorCondition + "\n" +
"Fired At: " + e.firedDateTime + "\n\n" +
"Description:\n" + e.description;
var sysId = gr.insert();
// Return success to Azure
response.setStatus(200);
response.setHeader("Content-Type", "application/json");
response.setBody(JSON.stringify({
status: "success",
message: "Incident created",
sys_id: sysId
}));
} catch (err) {
gs.error("Azure Webhook Script Error: " + err.message);
response.setStatus(500);
response.setHeader("Content-Type", "application/json");
response.setBody(JSON.stringify({
status: "error",
message: "Internal Server Error: " + err.message
}));
}
})(request, response);
Thanks and Regards,
Chiranjeevi R
Please mark as Correct Answer/Helpful, if applicable.
Thanks & Regards,
Chiranjeevi
ServiceNow Developer | | ITSM | | ServiceNow Discovery | | Event Management | | Service Mapping | | CMDB
Please mark as Correct Answer/Helpful, if applicable.
Chiranjeevi
ServiceNow Developer | | ITSM | | ServiceNow Discovery | | Event Management | | Service Mapping | | CMDB
Please mark as Correct Answer/Helpful, if applicable.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2025 06:44 AM
Same issue 😞
{
"error": {
"message": "Script Evaluation Exception",
"detail": "Cannot convert {\"status\":\"error\",\"message\":\"Internal Server Error: Cannot convert {\\\"status\\\":\\\"error\\\",\\\"message\\\":\\\"Missing essentials in payload\\\"} to org.mozilla.javascript.ScriptableObject (sys_ws_operation.e1b2d62583e1621010b97629feaad38c.operation_script; line 20)\"} to org.mozilla.javascript.ScriptableObject (sys_ws_operation.e1b2d62583e1621010b97629feaad38c.operation_script; line 53)"
},
"status": "failure"
}