Configure listener transform scripts

Configure a push connector to connect to an external event source, using custom script that processes the collected event messages and transforms it to the required event format. Select to send events either through the MID Server or the instance, in each case using the URL of the required format.

Before you begin

Role required: evt_mgmt_admin

About this task

The listener transform script accepts event messages that external event sources generate.

Configure the connector to listen to an external event source. Using custom listener transform script, send the event messages through either the MID Server or the instance.

Procedure

  1. Navigate to Event Management > Event Listener (Push) > Listener Transform Scripts.
  2. Click New or click the listener transform script that you want to modify, for example, AWS or Azure.
  3. Fill in the fields in the form, as needed.
    Field Value
    Name A unique name for this listener for easy identification.
    Description Enter a description for the use of the listener transform script.
    Header name Specify the name of the header that you want the value for, such as Set-Header.
    Header value The value of the header name that you want the value for, such as Set-Header.
    Type Select to send events either Instance or MID. In each case, use the URL of the selected type. See the description of the URL parameter value field.
    Active Select to enable pulling events from this external event source.
    Order Order in which an event rule is evaluated when multiple rules are defined for the same type of event. Event rules are evaluated in ascending order.
    URL parameter value
    • Send events to the instance.

      Use the URL in this format for Azure: https://{Instance-name}.service-now.com/api/global/em/inbound_event_azure Use the URL in this format for all connectors, other than Azure: https://{Instance-name}.service-now.com/api/global/em/inbound_event?source={URL_parameter_value}

    • Send events through the MID Server.

      Use the URL in the format:http://{MID_Server_IP}:{MID_Web_Server_Port}/api/mid/em/inbound_event?Transform={Transform_script}

      Note: Sending events to the MID Server requires the prior configuration of the MID WebService Event Listener extension.
  4. In the Script section:
    • If the value selected for the Type field is MID, the Transform script field appears. In this field, specify or search for the name of the MID script include that accepts event messages that the required external event source generates and that the script parses into the required event format. Use this naming convention for the script: TransformEvents_<your source>
    • If the value selected for the Type field is Instance, the Script editor appears. In the Script editor, enter the customized script that accepts event messages that the required external event source generates and that the script parses into the required event format.
    Example, showing fields that have been transformed, being added to an event form.
    (function process(/*RESTAPIRequest*/ request, body) {
    	/*Function that receives a JSON object, adding all its fields to the Additional information object. The field name is a concatenation of the field key and the parent field key if it exists.*/
    	function updateAdditionalInfo(event, field,jsonObject,additionalInfo) {
            for (var key in jsonObject) {
                var newKey = key;
                if (field != "") {
                    newKey = field + '_' + key;
                }
    			// You can do some transformation here and set fields on the event
    			//if(key == "MySource")
    			//   event.source = jsonObject[key];
                additionalInfo[newKey] = jsonObject[key];
            }
        }
        
        try
    	{		
            gs.info("TransformEvents_generic received body:" + body);
    		var jsonObject = JSON.parse(body);
            var event = new GlideRecord('em_event');
    		event.source = "GenericJson"; //TODO: Need to define
            event.event_class = "GenericJsonClass"; //TODO: Need to define
            event.severity = "5";
    		
            var additionalInfo = {};
            updateAdditionalInfo(event, "",jsonObject,additionalInfo);
    		/*Iterates over Additional information JSON object and adds all nested objects' fields as fields of the Additional information object*/
            var notDone = true;
            while (notDone) {
                notDone = false;
                for (var key in additionalInfo) {
                    if (Object.prototype.toString.call(additionalInfo[key]) == '[object Object]') {
                        notDone = true;
                        updateAdditionalInfo(event, key,additionalInfo[key],additionalInfo);
    					additionalInfo[key] = "";
                    }
                }
            }
    		gs.info("TransformEvents_generic generated additional information:" + JSON.stringify(additionalInfo));
            event.additional_info = JSON.stringify(additionalInfo);
            event.insert();
    	}
    	catch(er){
    		gs.error(er);
    		status=500;
    		return er;
    	}
    	return "success";
    })(request, body);