Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DavidHummingbird opened this issue Sep 17, 2024 · 2 comments

Comments

@DavidHummingbird
Copy link

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

  • Module version used: 0.1.6
  • Operating System and PowerShell version: Azure Automation, Powershell 7.2
@DavidHummingbird
Copy link
Author

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants