How do I create an incident with the REST API? – REST Web API – Builders Network

Sure, I use batch file (shown below) to call a Powershell script – also shown – HTH (sorry if the formatting is not very good – also please note, I am not a programmer – so please be kind!)

CreateIncident.bat

================

@echo OFF

REM echo %1

set myMessage=”DEMO Help me – I am having a REST issue”

REM echo $myMessage

if [%1]==[] goto Continue

set myMessage=%1

:Continue

REM echo %myMessage%

PowerShell.exe -Command “& ‘.CreateIncident.ps1’ ‘%myMessage%'”

CreateIncident.ps1

================

NOTE: all the configuration URL, Uname, password are held in my configKeith.json file

param ([string]$MyIncidentMessage)

Function GetFieldIndexByName

{

param ($fieldsObj, $fieldName)

$array = $fieldsObj.fields;

for ($i=0; $i -lt $array.length; $i++) {

If ($array[$i].name -eq $fieldName )

{

return $i

}

}

return -1

}

Function SetFieldValue

{

param ($fieldsObj, [string] $fieldName, [string] $newValue)

$fieldIndex = GetFieldIndexByName $fieldsObj $fieldName

#Write-Host $fieldName $newValue

if( $fieldIndex -gt -1 )

{

$fieldsObj.fields[$fieldIndex].value = $newValue

$fieldsObj.fields[$fieldIndex].dirty = “true”

}

}

#—————————————————–

# Logon to Service

# Get client ID from the config file

$config = Get-Content -Raw -Path configKeith.json | ConvertFrom-Json

$clientId = $config.clientid

$baseuri = $config.baseUri

$user = $config.user

$password = $config.password

#Login using uname/pass and token ID

$loginUri = $baseuri + ‘token’

$formFields = @

#Getting an Access Token

$tokenResponse = Invoke-WebRequest -Uri $loginUri -Method Post -Body $formFields -ContentType “application/x-www-form-urlencoded”

#Write-Host $request

$tokenResponseObject = $tokenResponse | ConvertFrom-Json

#KS Write-Host $tokenResponseObject.access_token

#—————————————————–

# Request Data

$testUrl = $baseuri + ‘api/V1/GetBusinessObjectTemplate’

# Create request for the business object template POST method

$dataRequest = @”

{

“BusObId”: “6dd53665c0c24cab86870a21cf6434ae”,

“FieldNames”: [“Source”,”CustomerDisplayName”, “Priority”, “Service”, “Category”, “SubCategory”, “ShortDescription”, “Description”]

}

“@

# Get teh BO Template

$results = Invoke-WebRequest -Method Post -Uri $testUrl -Header @{ “Authorization” = “Bearer ” + $tokenResponseObject.access_token} -ContentType “application/json” -Body $dataRequest

# output the template to json

#$results.Content | Out-File botemplate.json

# convert the template from json to a real object

$fieldsObject = $results.Content | ConvertFrom-Json

#KS

#—————————————————–

# Set Values to Create an Email Issue Incident

SetFieldValue $fieldsObject “Source” “Event”

SetFieldValue $fieldsObject “CustomerDisplayName” “John Allard”

#SetFieldValue $fieldsObject “CustomerRecID” “9365dad2de6bb99d1639784ab8a566c5a31f46d243”

SetFieldValue $fieldsObject “Priority” “3”

SetFieldValue $fieldsObject “Service” “E-Mail / Calendaring”

SetFieldValue $fieldsObject “Category” “Desktop Client”

SetFieldValue $fieldsObject “SubCategory” “Submit Incident”

SetFieldValue $fieldsObject “ShortDescription” “Email Issue”

#SetFieldValue $fieldsObject “Description” “Problem with accessing eMail (from REST WS API) – woo hoo part 2.”

SetFieldValue $fieldsObject “Description” $MyIncidentMessage

$myJsonFields = $fieldsObject | ConvertTo-Json

$myJsonFields = ‘{“busObId”: “6dd53665c0c24cab86870a21cf6434ae”,’ + $myJsonFields.Substring(1,($myJsonFields.length-2)) + ‘}’

$dataIncidentRequest = $myJsonFields

$createIncidentUrl = $baseuri + ‘api/V1/SaveBusinessObject’

#Invoke the REST API to create the incident

$incidentResults = Invoke-WebRequest -Method Post -Uri $createIncidentUrl -Header @{ “Authorization” = “Bearer ” + $tokenResponseObject.access_token} -ContentType “application/json” -Body $dataIncidentRequest

# Write the Results (to screen)

Write-Host $incidentResults



Source

Leave a comment

Your email address will not be published. Required fields are marked *