Solved

How to use AssystRest to insert a batch of events?

  • 28 March 2023
  • 2 replies
  • 204 views

Badge

How to use AssystRest to insert a batch of events?

icon

Best answer by Alan Macfarlane 30 March 2023, 09:22

View original

2 replies

Userlevel 2
Badge +5

Hi

There’s no distinct API for a batch of events, each event would need its own request*. An example to do this might look like:

 

Content-Type: application/json

POST https://{server}:{port}/assystRest/v2/events

BODY
{

   “eventType”: 1,

   “affectedUserName”: “Bob User”,

   “affectedUserEmail”: “bob@company.com”

   // etc.

 

There are some examples here.

 

(*there is a feature called “Object Graph” in REST  which allows the submission of multiple objects via one request however there is no support for it with the /events resource yet - if its is of use to you I would suggest registering the request via our Ideas forum)   

Userlevel 4
Badge +12

We have use API to generate tasks in a couple of processes. I have adapted my code to include a foreach element that cycles through events in an object. You might event be able to populate the object array from a .csv or .xlsx file using Get-Content. It might do what you want it to...

Function Set-JsonBody($eventType,$description,$importProfile,$affectedUser,$itemA,$category){
$myJsonConstruct = @"
{
"eventTypeEnum": "$eventType",
"remarks": "$description",
"importProfileId": $importProfile,
"affectedUser": {
"resolvingParameters": [
{
"parameterName": "shortCode",
"parameterValue": "$affectedUser"
},
{
"parameterName": "CSGId",
"parameterValue": "0"
}
]
},
"itemA": {
"resolvingParameters": [
{
"parameterName": "shortCode",
"parameterValue": "$itemA"
}
]
},
"category": {
"resolvingParameters": [
{
"parameterName": "shortCode",
"parameterValue": "$category"
}
]
}
}
"@
return $myJsonConstruct
}

$username = ""
$pass = ""
$secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
$Uri = "https://{server}:{port}/assystRest/v2/events"

$eventArr = @(
@{
eventType = "INCIDENT"
description = "This is the first event"
importProfile = 12
affectedUser = "BOB@COMPANY.COM"
itemA = "EXAMPLE_ITEM"
category = "EXAMPLE_CATEGORY"
}
@{
eventType = "INCIDENT"
description = "This is the NEXT event"
importProfile = 12
affectedUser = "ROB@COMPANY.COM"
itemA = "EXAMPLE_ITEM"
category = "EXAMPLE_CATEGORY"
}
)

foreach($event in $eventArr){
$Body = Set-JsonBody -eventType $event.eventType `
-description $event.description `
-importProfile $event.importProfile `
-affectedUser $event.affectedUser `
-itemA $event.itemA `
-category $event.category

$Params = @{
"URI" = $Uri
"Method" = 'POST'
"Credential" = $Credential
"ContentType" = 'application/json'
"Body" = $myJsonConstruct
}

Invoke-RestMethod @Params -UseBasicParsing ##Required for running from the Action Processor unsupervised. You might not need it the basic parsing parameter.
}

 

Alternatively, you could use the assystETM to import batches of events from a spreadsheet. We did this when migrating tickets from another system.

Reply