KelvinTegelaar / AutotaskAPI

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

Error calling New-AutotaskBody after Get-AutotaskAPIResource #49

Open LedgerLand opened 1 year ago

LedgerLand commented 1 year ago

I have run into what I believe is a bug when trying to create a new resource object. In my specific case, these are the commands to reproduce the issue:

Add-AutotaskAPIAuth -ApiIntegrationcode $ATTrackingApi -credentials $ATCredentials
$companyDetail = Get-AutotaskAPIResource -Resource Companies -SimpleSearch "id eq 0"
$newCompany = New-AutotaskBody -Resource Companies -NoContent

These steps produce the following error:

New-AutotaskBody : Getting object failed: The remote server returned an error: (404) Not Found.
At line:1 char:11
+ $ticket = New-AutotaskBody -Resource Companies -NoContent
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,New-AutotaskBody

I am writing using Visual Studio Code on a Windows 11 Pro workstation. Below is the PowerShell version table:

Name                           Value
----                           -----
PSVersion                      5.1.22621.608
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.22621.608
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

If I re-run the command "Add-AutotaskAPIAuth -ApiIntegrationcode $ATTrackingApi -credentials $ATCredentials -VerboseAdd-AutotaskAPIAuth -ApiIntegrationcode $ATTrackingApi -credentials $ATCredentials" after the Get request the issue does not happen.

Equally, if you try and create a different resource to the one queried (ie. query tickets but create a company) then this does not happen.

If you need anymore details just let me know :-)

phill-holbrook commented 1 year ago

I can confirm and reproduce this bug using the Contracts entity:

$Data = Get-AutotaskAPIResource -Resource Contracts -SearchQuery $filter
$Body = New-AutotaskBody -Resource Contracts
New-AutotaskBody: Getting object failed: Response status code does not indicate success: 404 (Not Found).

After re-authenticating using Add-AutotaskAPIAuth:

$Body = New-AutotaskBody -Resource Contracts

There must be something in Get-AutotaskAPIResource that's breaking a script variable or something.