Struggling to create a servicerequest with customfields using powershell

Hi,

I’m currently in the process of writing a powershell script that will create a servicerequest, but I am struggling with how to correctly format the JSON in regards to customfields. Creating a ticket/servicerequest without customfields is pretty straightforward. Below a anonymized and condensed code snippet of ticket creation, which works:

#new ticket
$EncodedCredentials = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $APIKey, $null)))
$HTTPHeaders = @{}
$HTTPHeaders.Add('Authorization', ("Basic {0}" -f $EncodedCredentials))
$HTTPHeaders.Add('Content-Type', 'application/json')
$TicketsURL = "https://MyCompany.freshservice.com/api/v2/tickets"

$Attributes = @{}
$Attributes.Add('email', 'email@mycompany.com')
$Attributes.Add('subject', 'new incident')
$Attributes.Add('description', 'Description of the incident')
$Attributes.Add('status', 2)
$Attributes.Add('priority', 2)
$Attributes.Add('category', "MyCategory")
$Attributes.Add('sub_category', "MySubCategory")
$Attributes.Add('source', 2)
$JSON = $Attributes | ConvertTo-Json

$Method = "POST"
$URL = $TicketsURL

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $URL -Headers $HTTPHeaders -Body $JSON -Method $Method 
#end new ticket

Now my challenge is, how do I do something similar for creating the right JSON for a servicerequest with custom fields? This is what I currently have:

#new service request
$Attributes = @{}
$Attributes.add('email', 'email@mycompany.com')
$Attributes.add('department', 'IT')
$Attributes.add('job_title', 'Consultant')
$Attributes.add('end_date', '31-12-2022')
$Attributes.add('mailbox_forwarding_needed', "No")
$JSON = $Attributes | ConvertTo-Json

$Method = "POST"
$URL = $ServiceRequestURL

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $URL -Headers $HTTPHeaders -Body $JSON -Method $Method 
#end new service request

But it returns the following error:

field                     message                             code
-----                     -------                             ----
job_title                 Unexpected/invalid field in request invalid_field
department                Unexpected/invalid field in request invalid_field
mailbox_forwarding_needed Unexpected/invalid field in request invalid_field
end_date                  Unexpected/invalid field in request invalid_field

This is what a servicerequest JSON looks like, when a SR has been created manually:

{ 
    "requested_items":[ { 
        "custom_fields": { 
            "email":110000295640, 
            "department":"IT", 
            "job_title":"Consultant", 
            "end_date":"2022-12-31", 
            "contract_end_date_all_belongings_should_be_returned_per_this_date":"2022-12-31", 
            "mailbox_forwarding_needed":"No: The mailbox will be permanently deleted", 
            "person_to_forward_the_mailbox":null, "out_of_office_setup":"No", 
            "out_of_office_messsage":null, 
            "additional_information":"THIS IS A TEST, PLEASE IGNORE" 
        }, 
        "id":110000021155, 
        "created_at":"2022-09-30T13:48:47Z", 
        "updated_at":"2022-09-30T13:48:47Z", 
        "quantity":1, 
        "stage":1, 
        "loaned":false, 
        "cost_per_request":0.0, 
        "remarks":null, 
        "delivery_time":null, 
        "is_parent":true, 
        "service_item_id":13 
    }] 
}

What do I need to modify to make my SR creation successful?

And then I solved it… I had to nest the arrays, so it looks like this:
$Attributes.add(‘email’, $tRequester)

#new service request with array nesting
$Attributes = @{}
$CustomAttributes = @{}
$CustomAttributes.add('email', 9999) # needs to be an integer in current config
$CustomAttributes.add('department', 'IT')
$CustomAttributes.add('job_title', 'Consultant')
$CustomAttributes.add('end_date', '2022-12-31') # ISO8601 date notation required
$CustomAttributes.add('mailbox_forwarding_needed', "Yes")
$CustomAttributes.add('person_to_forward_the_mailbox', 9999) # needs to be an integer in current config
$Attributes.add('custom_fields', $CustomAttributes)
$JSON = $Attributes | ConvertTo-Json

$Method = "POST"
$URL =  $ServiceRequestDir

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $URL -Headers $HTTPHeaders -Body $JSON -Method $Method 

Topic can be closed.

4 Likes

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.