Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Scripted REST API Azure Webhook for alerts error 500

AlexanderGreat
Tera Contributor

Hi!

 

I want to run a script to bring all Azure Alerts as incidents on ServiceNow. I've work on a script but I've got this error on Postman:

 

{
"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"
}

 

This is the 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);
 
 
Can someone help me on this?
0 REPLIES 0