(function process( /RESTAPIRequest/ request, body) { function updateAdditionalInfo(event, fieldPrefix, jsonObject, additionalInfo) { for (var key in jsonObject) { if (!jsonObject.hasOwnProperty(key)) continue; 

       var value = jsonObject[key]; 
        var newKey = fieldPrefix ? fieldPrefix + '_' + key : key; 
 
        // Skip keys that are mapped to top-level fields 
        if (["message_key", "alertSource", "alertNode", "alertLevel", "alertType"].includes(key)) continue; 
 
        if (value !== null && typeof value === 'object' && !Array.isArray(value)) { 
            additionalInfo[newKey] = value; 
        } else { 
            additionalInfo[newKey] = value; 
        } 
    } 
} 
 
function generateRandomString(length) { 
        var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; 
        var result = ''; 
        for (var i = 0; i < length; i++) { 
            result += chars.charAt(Math.floor(Math.random() * chars.length)); 
        } 
        return result; 
    } 
 
function mapSeverity(level) { 
    // Map alert levels to severity codes (customize as needed) 
    var map = { 
        "Critical": "1", // Commvault 
        "Warning": "2", // Commvault 
        "Info": "3", // Commvault 
        "Low": "4", 
        "Auto Pick": "5" 
    }; 
    return map[level] || "5"; // default to lowest severity 
} 
 
try { 
    gs.info("TransformEvents_custom received body: " + body); 
    var jsonObject = JSON.parse(body); 
 
    var event = new GlideRecord('em_event'); 
    event.initialize(); 
 
    // Top-level event field mapping 
    event.message_key = jsonObject.message_key || ""; 
    event.source = jsonObject.alertSource || "GenericJson"; 
    event.node = jsonObject.alertNode || ""; 
    event.severity = mapSeverity(jsonObject.alertLevel); 
    event.event_class = jsonObject.alertType || "GenericJsonClass"; 
    event.alertSource = jsonObject.alertSource; 
    event.alertNode = jsonObject.alertNode; 
    event.time_of_event = jsonObject.alertDateTime; 
    event.description = jsonObject.alertName; 
    event.resource = jsonObject.alertResource; 
    event.alert.description = jsonObject.alertMessage; 
    event.alert.additional_info = jsonObject.alertMessage; 
    event.metric_name = jsonObject.alertMetricName; 
    // Event of type alert is create and the event will also create an alert at the service now end. 
    event.type = jsonObject.alertType; 
     
    event.message_key = generateRandomString(16); 
 
    // Populate additional_info 
    var additionalInfo = {}; 
    updateAdditionalInfo(event, "", jsonObject, additionalInfo); 
 
    // Flatten nested objects if any 
    var notDone = true; 
    while (notDone) { 
        notDone = false; 
        for (var key in additionalInfo) { 
            if (Object.prototype.toString.call(additionalInfo[key]) === '[object Object]') { 
                updateAdditionalInfo(event, key, additionalInfo[key], additionalInfo); 
                additionalInfo[key] = ""; // You could also delete it 
                notDone = true; 
            } 
        } 
    } 
 
    // event.additional_info = JSON.stringify(additionalInfo); 
    event.additional_info = jsonObject.alertMessage; 
    var eventSysId = event.insert(); 
    gs.info("Event inserted with Sys ID: " + eventSysId); 
} catch (er) { 
    gs.error("Error processing event: " + er.message); 
    status = 500; 
    return er.message; 
} 
 
return "success123"; 
  

})(request, body); //  