KelvinTegelaar / AutotaskAPI

Autotask 2020.2 REST API PowerShell wrapper
https://cyberdrain.com
MIT License
67 stars 25 forks source link

New-AutotaskAPIResource - Internal Server Error 500 #26

Closed bgalinsky closed 2 years ago

bgalinsky commented 3 years ago

Hi Kelvin.

I'm trying to create new a company but I keep getting a (500) Internal Server Error.

After building the $Body object with

$Body = New-AutotaskBody -Resource Companies -NoContent

and setting the following values:

$Body.companyType ='1' $Body.companyName = 'ACME Metals' $Body.companyNumber = 'ACME' $Body.address1 = '123 Main St' $Body.city = 'Anytown' $Body.state = 'SC' $Body.postalCode = '29601' $Body.countryID = 237 $Body.phone = '(864) 555-1212'

I issue the command:

New-AutotaskAPIResource -Resource Companies -Body $Body

Consequently, I get this error:

New-AutotaskAPIResource : Connecting to the Autotask API failed. The remote server returned an error: (500) Internal Server Error. At line:13 char:1 + New-AutotaskAPIResource -Resource Companies -Body $Body + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,New-AutotaskAPIResource

I know my connection to the API is working because I can retrieve my company with the command:

Get-AutotaskAPIResource -Resource Companies -ID 0

Any guidance would be greatly appreciated. I've been looking for examples on how to create a contact too.

KelvinTegelaar commented 3 years ago

You most likely are missing some required UDFs, To check, grab a company that you would like to copy and check which fields are filled in and which are not. Sorry for the late reply!

paulobrienlucidity commented 3 years ago

I'm having the same issue with a new company, none of the UDFs for companies resource are required.

I've tried creating a new company in the UI (with the bare basics).. then getting that company, setting exactly the same properties (except changing company name and leaving id blank) and submitting but get the same 500 error.

Could you give us a working example of creating a new company with the bare basics?

Can create a company via PostMan to this URL https://webservices6.autotask.net/atservicesrest/v1.0/Companies

with this basic body { "companyName":"!Postman Create Company n", "companyType":1, "ownerResourceID":29682903, "phone":"5555555555" }

So, I would assume, this is right syntax?

$body = Get-AutotaskAPIResource -Resource Companies -NoContent $body.companyName = "AAA" $body.phone = "5554455" $body.ownerResourceID = 29682903 $body.companyType = 1 New-AutotaskAPIResource -Resource Companies -Body $body

But i get the 500 error

freyman83 commented 3 years ago

@paulobrienlucidity Same goes for creating new tickets and new products.

New-AutotaskAPIResource : Connecting to the Autotask API failed. De externe server heeft een fout geretourneerd: (500) Interne serverfout. At line:1 char:1

Somehow autotask doesn't accept the way the $body is formatted. I haven't figured this out yet. I switched to https://github.com/ecitsolutions/Autotask to get this done. I pretty similar, still gets updated and it just works.

JanDurryCyberdyne commented 2 years ago

Hello, is there any update on this? We are awaiting an Autotask Fix, right? Is it a Powershell-related issue?

I debugged my code and the JSON body seems to be fine. Could it be an encoding issue? We had issues in the past with API-Requests in the past related to German-"Umlauts" even with the ecitsolutions module.

freyman83 commented 2 years ago

@JanDurryCyberdyne
I got it to work with this Autotask API. I just needed to add more values to the existing fields in the body. It would give me error 500 at first, but it just ment i wasn't providing all the needed information to autotask.

Here is an example for the resource Products. We upload Techdata products on a weekly basis to autotask and adjust pricing on existing objects when they occur. This $body succesfully writes changes to autotask with this API. There are fields that are required to provide (E.G. isActive), however even some seemingly non required fields needed to be filled with a value before autotask accepted the body. Same goes for tickets, just check all the fields in the body (New-AutotaskBody -Resource Tickets -NoContent) and try to provide more information. Cross reference existing tickets with what you are trying to upload. Make sure every value is correct and can be accepted by Autotask.

$body = New-AutotaskBody -Resource Products -NoContent
$body.billingType = "1" $body.defaultVendorID = "344"
$body.description = $fetchproduct.description
$body.doesNotRequireProcurement = "False"
$body.externalProductID = $fetchproduct.externalproductID $body.id = "0"
$body.internalProductID = "99999999"
$body.isActive = "True"
$body.isEligibleForRma = "True"
$body.isSerialized = "False"
$body.manufacturerName = $fetchproduct.manufacturerName $body.name = $fetchproduct.name + + $body.externalProductID
$body.periodType = "1"
$body.productBillingCodeID = '29682840'
$body.productCategory = "18"
$body.sku = $fetchproduct.sku
$body.unitCost = $price.unitCost
$body.unitPrice = $price.unitPrice
$body.vendorProductNumber = $fetchproduct.vendorproductNumber

$body | Set-AutotaskAPIResource -Resource Products

JanDurryCyberdyne commented 2 years ago

@KelvinTegelaar thanks for the update. I´ll check which fields are required for Tickets and will give you an update. I am just wondering, why it seems like they have designed their REST-Endpoint differently from the SOAP-Endpoint, as with the ecitsolutions PS-Wrapper I can create Tickets with the same attributes I'm using right now.

Thanks for your response.

Serg86 commented 2 years ago

The problem is with empty values in the body.

The solution is to filter out all the null values using something like this:

$Body = New-AutotaskBody -Resource "Tickets" -NoContent
$Body.title = "Test Ticket"
$Body.companyID = 0
...
$NoNullValues = $Body | ForEach-Object { $Properties = $_.PSObject.Properties.Name | Where-Object { $null -ne $Body.$_} ; $Body | Select-Object $Properties }

companyID = 0 will be preserved since 0 is not $null.

paulobriennz commented 2 years ago

The problem is with empty values in the body.

The solution is to filter out all the null values using something like this:

$Body = New-AutotaskBody -Resource "Tickets" -NoContent
$Body.title = "Test Ticket"
$Body.companyID = 0
...
$NoNullValues = $Body | ForEach-Object { $Properties = $_.PSObject.Properties.Name | Where-Object { $null -ne $Body.$_} ; $Body | Select-Object $Properties }

companyID = 0 will be preserved since 0 is not $null.

You sir are a legend. It was the null values in the body - stripped them out using your line of PS and posted and boom, record created. Thank you thank you thank you.