Creating multiple events in ITOM with a single API POST

WillHerr
Mega Contributor
So I have a Powershell function that creates events in ITOM based on a JSON of one or more events.  It works, it's straight forward, it loops a foreach for each entry in the JSON to call https://$myenv.service-now.com/api/now/table/em_event
and creates each item in turn.
What I am hoping to find though is an alternative where I can make one call to the API rather than many.  Batch/bulk the events rather than the brute force 1 event = 1 api call method I have today, that I don't think will scale well if we find ourselves in a high traffic scenario.
 
function Create-ServiceNowEvents {
        param (
            [string]$myenv,
            [array]$events,
            [string]$token
        )
   
        # Define the API endpoint for creating events
        $apiUrl = "https://$myenv.service-now.com/api/now/table/em_event"
   
        # Create the headers for the API request
        $headers = @{
            Authorization = "Bearer $token"
            "Content-Type" = "application/json"
        }
   
        # Loop through each event and send them individually
        foreach ($event in $events) {
            # Convert event data to JSON
            $jsonData = $event | ConvertTo-Json
   
            # Make the API request to create the event
            $response = Invoke-RestMethod -Uri $apiUrl -Method Post -Body $jsonData -Headers $headers
   
            # Output the response
            Write-Output $response
        }
    }
2 REPLIES 2

marcguegueniat
Kilo Sage

Hello,

This is absolutely possible: you will have to create your own custom push connector.

Basically a connector will parse a payload and create 1 or many records in em_event based on content parsed.

I suggest you have a look at existing connectors: check how they work, you can probably reuse some code.

You can script any behavior to handle any payload you want.

Once your push connector is defined, you will need to create one instance for it, just as you do for OOB connectors.

Regards,

MortenPettersen
Tera Contributor

Hi WillHerr

 

ServiceNow gives an example on how this can be achieved on this document page:
Pushing events to the instance using web service API

Ignore the first mentions of "insert multiple" and look for this text:
To create multiple records with a single call, trigger the event web service using the following URL,