flycastpartnersinc / FreshservicePS

Powershell Module to interface with Freshworks Freshservice REST API.
MIT License
23 stars 5 forks source link

New-FreshServiceTicket Fails When You Have Both an Attachment and CC_Emails #24

Open DavidHummingbird opened 1 month ago

DavidHummingbird commented 1 month ago

Expected Behavior

 $params = @{
        status      = $fsStatus
        priority    = $fsPriority
        urgency     = $fsUrgency
        impact      = $fsImpact
        source      = $fsSource
        email       = $fsRequester
        subject     = $fsSubject
        type        = "Incident"
        category    = $fsCategory
        cc_emails   = @($cc_email)
        description = $body
        attachments = @($(New-Object System.IO.FileInfo -ArgumentList $file))
    }
  $fsTicket = New-FreshServiceTicket @params
  # $fsTicket should be a valid ticket

Current Behavior

  $params = @{
        status      = $fsStatus
        priority    = $fsPriority
        urgency     = $fsUrgency
        impact      = $fsImpact
        source      = $fsSource
        email       = $fsRequester
        subject     = $fsSubject
        type        = "Incident"
        category    = $fsCategory
        cc_emails   = @($cc_email)
        description = $body
        attachments = @($(New-Object System.IO.FileInfo -ArgumentList $file))
    }
  $fsTicket = New-FreshServiceTicket @params

  {"description": "Validation failed", "errors": [ {"field": "cc_emails", "message": "The value provided is of type String.It should be of type Array", "code": "datatype_mismatch" }] }

Possible Solution

The issue is in Public/New-FreshserviceTicket.ps1 (and may exist in other modules). The problem is using BOTH CC_Emails and Attachments -- when you use Attachments, the $jsonBody is assigned to $params.form but not converted to JSON. The .form method accepts an IDictionary Invoke-WebRequest but the content of arrays (such as CC_Emails) is not expanded - it's represented by its object type descriptor:

Inserting some debug code on line 475 of New-FreshServiceTicket:

                Write-Host $jsonBody # DEBUG
                $result = Invoke-FreshworksRestMethod @params

... produces this result:

[category, IT Operations] 
[priority, 1] 
[urgency, 2] 
[status, 4] 
[impact, 1] 
[type, Incident] 
[email, no-reply@company.com] 
[subject, Microsoft Photo Change Review: David] 
[attachments[], System.IO.FileInfo[]] 
[source, 2] 
[description, <p><b>David</b> changed their photo recently in Workday and we're syncing it to Microsoft.<br/>] 
[cc_emails, System.String[]]

Steps to Reproduce (for bugs)

(see code sample above)

Context

In my use case, I'm pulling a photo out of an HR system and sending it to FreshService to create a ticket for review. My requester is my service account, and I want to CC the employee (owner of the photo) on the original ticket, so they get the description. If I make the employee the requester, then they won't get the description of the ticket, just the notification that a ticket was created on their behalf.

Your Environment

DavidHummingbird commented 1 month ago

My current workaround is to create the ticket with the CC_Email, and then use Set-FreshServiceTicket to add the attachment immediately after.

r-wilkins commented 1 month ago

This has to due with how powershell handles JSON depths. I ran into this earlier when trying to modified a large amount of agents role permissions. THis article should explain a bit about it. But by default it only goes to a depth of 2 and it needs to be longer to turn those arrays into json arrays. https://jeffbrown.tech/going-deep-converting-powershell-objects-to-json/

But try to go into your new-freshserviceticket.ps1 file and replace the lines

$params.Body = $jsonBody | ConvertTo-Json

with

$params.Body = $jsonBody | ConvertTo-Json -depth 5