Event Management - Event Filed Mapping

PankajK75582456
Tera Contributor

Hi,

 

I have created 1 custom fields on both event and alert table where using event rules to create an alert and event mapping field to handle complex mapping for fields.

 

The event rule is running fine and able to update the custom created field from event data (payload in addtional information), but event mapping field is unable to update any custom created record.

I tried updating the OOB field with the same parse value of given script and it is working but the moment I change it to custom field, It stops working ( still able to see the logs without any errors). PFB the event field mapping script and help me to fix it-

 

custom field name: u_quality_scores

 

script: 

(function eventFieldMappingScript(eventGr, origEventSysId, fieldMappingRuleName) {
    try {
        var addInfoStr = eventGr.getValue('additional_info') || '{}';
        var info = JSON.parse(addInfoStr);
        
        gs.info('[' + fieldMappingRuleName + '] quality_score raw value: ' + info.quality_score);
        
        if (info.quality_score != null) {
            var score = parseInt(info.quality_score, 10);
            if (!isNaN(score)) {
                eventGr.setValue('u_quality_scores', parseInt(score + 1));
				return true;
                // gs.info('[' + fieldMappingRuleName + '] Set u_quality_scores to ' + (score + 1));
            }
        }
        
        return true;
    } catch (e) {
        gs.error("The script type mapping rule '" + fieldMappingRuleName + "' ran with the error: \n" + e);
        return false;
    }
})(eventGr, origEventSysId, fieldMappingRuleName);

 

1 REPLY 1

Venky Kshatriy2
Tera Contributor

Hi @PankajK75582456 
 this behavior actually matches how Event Field Mapping (Advanced mapping using script) works in Event Management.

In an Event Field Mapping advanced script, eventGr is a temporary in‑memory event object. You can call eventGr.setValue(...), but the Event record in the database is immutable and will not be updated. The changes are only meant to influence how the alert will be generated.

Because of that, setting a custom column on em_event often won’t carry forward to the alert, while setting certain OOB fields (severity, node, resource, etc.) works because those are part of the standard event→alert pipeline.

Also, ServiceNow’s guidance/community best practice is: don’t extend em_event for enrichment—use additional_info and/or custom fields on em_alert.

 

Why your script logs fine but custom field stays empty

Your script parses additional_info correctly and logs the value — so script execution is fine.
The issue is where you’re writing the result:

 

JavaScript

 

eventGr.setValue('u_quality_scores', ...)
 

Show more lines

 

This sets a value on the temporary event object, but unless that value is:

  • part of the standard event fields used during alert generation, or
  • present in Additional Information with a matching name,

…it will not land in the alert record.

 

ServiceNow explicitly supports auto-populating alert fields when the event additional_info contains a key with the same name as the alert field.

So instead of setting eventGr.u_quality_scores, add/update:

  • info.u_quality_scores = score + 1;
  • then write back additional_info
    (function eventFieldMappingScript(eventGr, origEventSysId, fieldMappingRuleName) {

    try {

        var addInfoStr = eventGr.getValue('additional_info') || '{}';

        var info;

 

        // Be defensive in case additional_info is not valid JSON

        try {

            info = JSON.parse(addInfoStr);

        } catch (jsonErr) {

            info = {};

        }

 

        gs.info('[' + fieldMappingRuleName + '] quality_score raw value: ' + info.quality_score);

 

        if (info.quality_score != null) {

            var score = parseInt(info.quality_score, 10);

 

            if (!isNaN(score)) {

                // Put it into additional_info with the SAME NAME as the alert custom field

                info.u_quality_scores = score + 1;

 

                // Save back to the temporary event object (used for alert generation)

                eventGr.setValue('additional_info', JSON.stringify(info));

 

                gs.info('[' + fieldMappingRuleName + '] Set additional_info.u_quality_scores to ' + (score + 1));

            }

        }

 

        return true;

    } catch (e) {

        gs.error("The script type mapping rule '" + fieldMappingRuleName + "' ran with the error: \n" + e);

        return false;

    }

})(eventGr, origEventSysId, fieldMappingRuleName);

If my answer is correct, please mark it as solved so it can assist others as well.