VB.NET UWP/WinRT Zendesk: Create a Ticket with an Attachment (a previously uploaded file)

Back to Index

Creates a new Zendesk ticket with an attachment. The file attachment is indicated by an array of attachment tokens contained within the comment. (The attachment should be uploaded beforehand, and the token can be obtained from the JSON response of the attachment file upload.)

Documentation: https://developer.zendesk.com/rest_api/docs/core/tickets#create-ticket

CURL Command

curl -X POST https://subdomain.zendesk.com/api/v2/tickets.json \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d '{
  "ticket": {
    "subject": "My application crashed.  Please help.",
   "comment":  { "body": "Here is a screen shot", "uploads":  ["W7Y11FCxfQlzOFPDbVDUEE45C"] }
  }
}' \
  -u zendesk_email:zendesk_password

VB.NET UWP/WinRT Example

Dim rest As New Chilkat.Rest
Dim success As Boolean

'  URL: https://subdomain.zendesk.com/api/v2/tickets.json
Dim bTls As Boolean = True
Dim port As Integer = 443
Dim bAutoReconnect As Boolean = True
success = Await rest.ConnectAsync("subdomain.zendesk.com",port,bTls,bAutoReconnect)
If (success <> True) Then
    Debug.WriteLine("ConnectFailReason: " & rest.ConnectFailReason)
    Debug.WriteLine(rest.LastErrorText)
    Exit Sub
End If


rest.SetAuthBasic("zendesk_email","zendesk_password")

Dim json As New Chilkat.JsonObject
json.UpdateString("ticket.subject","My application crashed.  Please help.")
json.UpdateString("ticket.comment.body","Here is a screen shot")
json.UpdateString("ticket.comment.uploads[0]","W7Y11FCxfQlzOFPDbVDUEE45C")

rest.AddHeader("Content-Type","application/json")
rest.AddHeader("Accept","application/json")

Dim sbRequestBody As New Chilkat.StringBuilder
json.EmitSb(sbRequestBody)
Dim sbResponseBody As New Chilkat.StringBuilder
success = Await rest.FullRequestSbAsync("POST","/api/v2/tickets.json",sbRequestBody,sbResponseBody)
If (success <> True) Then
    Debug.WriteLine(rest.LastErrorText)
    Exit Sub
End If

Dim respStatusCode As Integer = rest.ResponseStatusCode
If (respStatusCode >= 400) Then
    Debug.WriteLine("Response Status Code = " & respStatusCode)
    Debug.WriteLine("Response Header:")
    Debug.WriteLine(rest.ResponseHeader)
    Debug.WriteLine("Response Body:")
    Debug.WriteLine(sbResponseBody.GetAsString())
    Exit Sub
End If


Dim jsonResponse As New Chilkat.JsonObject
jsonResponse.LoadSb(sbResponseBody)

Dim ticketUrl As String
Dim ticketId As Integer
Dim ticketExternal_id As String
Dim ticketViaChannel As String
Dim ticketViaSourceRel As String
Dim ticketCreated_at As String
Dim ticketUpdated_at As String
Dim ticketType As String
Dim ticketSubject As String
Dim ticketRaw_subject As String
Dim ticketDescription As String
Dim ticketPriority As String
Dim ticketStatus As String
Dim ticketRecipient As String
Dim ticketRequester_id As Integer
Dim ticketSubmitter_id As Integer
Dim ticketAssignee_id As Integer
Dim ticketOrganization_id As Integer
Dim ticketGroup_id As Integer
Dim ticketForum_topic_id As String
Dim ticketProblem_id As String
Dim ticketHas_incidents As Boolean
Dim ticketIs_public As Boolean
Dim ticketDue_at As String
Dim ticketSatisfaction_rating As String
Dim ticketBrand_id As Integer
Dim ticketAllow_channelback As Boolean
Dim auditId As Integer
Dim auditTicket_id As Integer
Dim auditCreated_at As String
Dim auditAuthor_id As Integer
Dim auditMetadataSystemIp_address As String
Dim auditMetadataSystemLocation As String
Dim auditMetadataSystemLatitude As Integer
Dim auditMetadataSystemLongitude As Integer
Dim auditViaChannel As String
Dim auditViaSourceRel As String
Dim i As Integer
Dim count_i As Integer
Dim id As Integer
Dim type As String
Dim author_id As Integer
Dim body As String
Dim html_body As String
Dim plain_body As String
Dim public As Boolean
Dim audit_id As Integer
Dim value As String
Dim field_name As String
Dim viaChannel As String
Dim viaSourceFromDeleted As Boolean
Dim viaSourceFromTitle As String
Dim viaSourceFromId As Integer
Dim viaSourceRel As String
Dim subject As String
Dim j As Integer
Dim count_j As Integer
Dim url As String
Dim file_name As String
Dim content_url As String
Dim mapped_content_url As String
Dim content_type As String
Dim size As Integer
Dim width As String
Dim height As String
Dim inline As Boolean
Dim k As Integer
Dim count_k As Integer
Dim intVal As Integer

ticketUrl = jsonResponse.StringOf("ticket.url")
ticketId = jsonResponse.IntOf("ticket.id")
ticketExternal_id = jsonResponse.StringOf("ticket.external_id")
ticketViaChannel = jsonResponse.StringOf("ticket.via.channel")
ticketViaSourceRel = jsonResponse.StringOf("ticket.via.source.rel")
ticketCreated_at = jsonResponse.StringOf("ticket.created_at")
ticketUpdated_at = jsonResponse.StringOf("ticket.updated_at")
ticketType = jsonResponse.StringOf("ticket.type")
ticketSubject = jsonResponse.StringOf("ticket.subject")
ticketRaw_subject = jsonResponse.StringOf("ticket.raw_subject")
ticketDescription = jsonResponse.StringOf("ticket.description")
ticketPriority = jsonResponse.StringOf("ticket.priority")
ticketStatus = jsonResponse.StringOf("ticket.status")
ticketRecipient = jsonResponse.StringOf("ticket.recipient")
ticketRequester_id = jsonResponse.IntOf("ticket.requester_id")
ticketSubmitter_id = jsonResponse.IntOf("ticket.submitter_id")
ticketAssignee_id = jsonResponse.IntOf("ticket.assignee_id")
ticketOrganization_id = jsonResponse.IntOf("ticket.organization_id")
ticketGroup_id = jsonResponse.IntOf("ticket.group_id")
ticketForum_topic_id = jsonResponse.StringOf("ticket.forum_topic_id")
ticketProblem_id = jsonResponse.StringOf("ticket.problem_id")
ticketHas_incidents = jsonResponse.BoolOf("ticket.has_incidents")
ticketIs_public = jsonResponse.BoolOf("ticket.is_public")
ticketDue_at = jsonResponse.StringOf("ticket.due_at")
ticketSatisfaction_rating = jsonResponse.StringOf("ticket.satisfaction_rating")
ticketBrand_id = jsonResponse.IntOf("ticket.brand_id")
ticketAllow_channelback = jsonResponse.BoolOf("ticket.allow_channelback")
auditId = jsonResponse.IntOf("audit.id")
auditTicket_id = jsonResponse.IntOf("audit.ticket_id")
auditCreated_at = jsonResponse.StringOf("audit.created_at")
auditAuthor_id = jsonResponse.IntOf("audit.author_id")
auditMetadataSystemIp_address = jsonResponse.StringOf("audit.metadata.system.ip_address")
auditMetadataSystemLocation = jsonResponse.StringOf("audit.metadata.system.location")
auditMetadataSystemLatitude = jsonResponse.IntOf("audit.metadata.system.latitude")
auditMetadataSystemLongitude = jsonResponse.IntOf("audit.metadata.system.longitude")
auditViaChannel = jsonResponse.StringOf("audit.via.channel")
auditViaSourceRel = jsonResponse.StringOf("audit.via.source.rel")
i = 0
count_i = jsonResponse.SizeOfArray("ticket.collaborator_ids")
While i < count_i
    jsonResponse.I = i
    i = i + 1
End While
i = 0
count_i = jsonResponse.SizeOfArray("ticket.follower_ids")
While i < count_i
    jsonResponse.I = i
    i = i + 1
End While
i = 0
count_i = jsonResponse.SizeOfArray("ticket.email_cc_ids")
While i < count_i
    jsonResponse.I = i
    i = i + 1
End While
i = 0
count_i = jsonResponse.SizeOfArray("ticket.tags")
While i < count_i
    jsonResponse.I = i
    i = i + 1
End While
i = 0
count_i = jsonResponse.SizeOfArray("ticket.custom_fields")
While i < count_i
    jsonResponse.I = i
    i = i + 1
End While
i = 0
count_i = jsonResponse.SizeOfArray("ticket.sharing_agreement_ids")
While i < count_i
    jsonResponse.I = i
    i = i + 1
End While
i = 0
count_i = jsonResponse.SizeOfArray("ticket.fields")
While i < count_i
    jsonResponse.I = i
    i = i + 1
End While
i = 0
count_i = jsonResponse.SizeOfArray("ticket.followup_ids")
While i < count_i
    jsonResponse.I = i
    i = i + 1
End While
i = 0
count_i = jsonResponse.SizeOfArray("audit.events")
While i < count_i
    jsonResponse.I = i
    id = jsonResponse.IntOf("audit.events[i].id")
    type = jsonResponse.StringOf("audit.events[i].type")
    author_id = jsonResponse.IntOf("audit.events[i].author_id")
    body = jsonResponse.StringOf("audit.events[i].body")
    html_body = jsonResponse.StringOf("audit.events[i].html_body")
    plain_body = jsonResponse.StringOf("audit.events[i].plain_body")
    public = jsonResponse.BoolOf("audit.events[i].public")
    audit_id = jsonResponse.IntOf("audit.events[i].audit_id")
    value = jsonResponse.StringOf("audit.events[i].value")
    field_name = jsonResponse.StringOf("audit.events[i].field_name")
    viaChannel = jsonResponse.StringOf("audit.events[i].via.channel")
    viaSourceFromDeleted = jsonResponse.BoolOf("audit.events[i].via.source.from.deleted")
    viaSourceFromTitle = jsonResponse.StringOf("audit.events[i].via.source.from.title")
    viaSourceFromId = jsonResponse.IntOf("audit.events[i].via.source.from.id")
    viaSourceRel = jsonResponse.StringOf("audit.events[i].via.source.rel")
    subject = jsonResponse.StringOf("audit.events[i].subject")
    j = 0
    count_j = jsonResponse.SizeOfArray("audit.events[i].attachments")
    While j < count_j
        jsonResponse.J = j
        url = jsonResponse.StringOf("audit.events[i].attachments[j].url")
        id = jsonResponse.IntOf("audit.events[i].attachments[j].id")
        file_name = jsonResponse.StringOf("audit.events[i].attachments[j].file_name")
        content_url = jsonResponse.StringOf("audit.events[i].attachments[j].content_url")
        mapped_content_url = jsonResponse.StringOf("audit.events[i].attachments[j].mapped_content_url")
        content_type = jsonResponse.StringOf("audit.events[i].attachments[j].content_type")
        size = jsonResponse.IntOf("audit.events[i].attachments[j].size")
        width = jsonResponse.StringOf("audit.events[i].attachments[j].width")
        height = jsonResponse.StringOf("audit.events[i].attachments[j].height")
        inline = jsonResponse.BoolOf("audit.events[i].attachments[j].inline")
        k = 0
        count_k = jsonResponse.SizeOfArray("audit.events[i].attachments[j].thumbnails")
        While k < count_k
            jsonResponse.K = k
            k = k + 1
        End While
        j = j + 1
    End While
    j = 0
    count_j = jsonResponse.SizeOfArray("audit.events[i].recipients")
    While j < count_j
        jsonResponse.J = j
        intVal = jsonResponse.IntOf("audit.events[i].recipients[j]")
        j = j + 1
    End While
    i = i + 1
End While

Sample JSON Response Body

{
  "ticket": {
    "url": "https://chilkat.zendesk.com/api/v2/tickets/11.json",
    "id": 11,
    "external_id": null,
    "via": {
      "channel": "api",
      "source": {
        "from": {},
        "to": {},
        "rel": null
      }
    },
    "created_at": "2018-04-15T20:29:49Z",
    "updated_at": "2018-04-15T20:29:49Z",
    "type": null,
    "subject": "My application crashed.  Please help.",
    "raw_subject": "My application crashed.  Please help.",
    "description": "Here is a screen shot",
    "priority": null,
    "status": "open",
    "recipient": null,
    "requester_id": 361801727573,
    "submitter_id": 361801727573,
    "assignee_id": 361801727573,
    "organization_id": 360015071793,
    "group_id": 360000681553,
    "collaborator_ids": [
    ],
    "follower_ids": [
    ],
    "email_cc_ids": [
    ],
    "forum_topic_id": null,
    "problem_id": null,
    "has_incidents": false,
    "is_public": true,
    "due_at": null,
    "tags": [
    ],
    "custom_fields": [
    ],
    "satisfaction_rating": null,
    "sharing_agreement_ids": [
    ],
    "fields": [
    ],
    "followup_ids": [
    ],
    "brand_id": 360000471533,
    "allow_channelback": false
  },
  "audit": {
    "id": 391509606014,
    "ticket_id": 11,
    "created_at": "2018-04-15T20:29:49Z",
    "author_id": 361801727573,
    "metadata": {
      "system": {
        "ip_address": "52.25.83.238",
        "location": "Boardman, OR, United States",
        "latitude": 45.86959999999999,
        "longitude": -119.688
      },
      "custom": {}
    },
    "events": [
      {
        "id": 391509606034,
        "type": "Comment",
        "author_id": 361801727573,
        "body": "Here is a screen shot",
        "html_body": "<div class=\"zd-comment\"><p dir=\"auto\">Here is a screen shot</p></div>",
        "plain_body": "Here is a screen shot",
        "public": true,
        "attachments": [
          {
            "url": "https://chilkat.zendesk.com/api/v2/attachments/360887735934.json",
            "id": 360887735934,
            "file_name": "sample.pdf",
            "content_url": "https://chilkat.zendesk.com/attachments/token/Q7wAubpKB2KERnNFj89JmdvS1/?name=sample.pdf",
            "mapped_content_url": "https://chilkat.zendesk.com/attachments/token/Q7wAubpKB2KERnNFj89JmdvS1/?name=sample.pdf",
            "content_type": "application/pdf",
            "size": 178399,
            "width": null,
            "height": null,
            "inline": false,
            "thumbnails": [
            ]
          }
        ],
        "audit_id": 391509606014
      },
      {
        "id": 391509606394,
        "type": "Create",
        "value": "My application crashed.  Please help.",
        "field_name": "subject"
      },
      {
        "id": 391509606414,
        "type": "Create",
        "value": "361801727573",
        "field_name": "requester_id"
      },
      {
        "id": 391509606434,
        "type": "Create",
        "value": "open",
        "field_name": "status"
      },
      {
        "id": 391509606454,
        "type": "Create",
        "value": null,
        "field_name": "priority"
      },
      {
        "id": 391509606474,
        "type": "Create",
        "value": null,
        "field_name": "type"
      },
      {
        "id": 391509606494,
        "type": "Create",
        "value": "361801727573",
        "field_name": "assignee_id"
      },
      {
        "id": 391509606514,
        "type": "Create",
        "value": "360000681553",
        "field_name": "group_id"
      },
      {
        "id": 391509606534,
        "type": "Create",
        "value": "360015071793",
        "field_name": "organization_id"
      },
      {
        "id": 391509606554,
        "type": "Notification",
        "via": {
          "channel": "rule",
          "source": {
            "from": {
              "deleted": false,
              "title": "Notify requester of received request",
              "id": 360018516913
            },
            "rel": "trigger"
          }
        },
        "subject": "[Request received] {{ticket.title}}",
        "body": "Your request ({{ticket.id}}) has been received and is being reviewed by our support staff.\n\nTo add additional comments, reply to this email.\n\n{{ticket.comments_formatted}}",
        "recipients": [
          361801727573
        ]
      },
      {
        "id": 391509606574,
        "type": "Notification",
        "via": {
          "channel": "rule",
          "source": {
            "from": {
              "deleted": false,
              "title": "Notify all agents of received request",
              "id": 360018517033
            },
            "rel": "trigger"
          }
        },
        "subject": "[{{ticket.account}}] {{ticket.title}}",
        "body": "A ticket (#{{ticket.id}}) by {{ticket.requester.name}} has been received. It is unassigned.\n\n{{ticket.comments_formatted}}",
        "recipients": [
          361801727573
        ]
      }
    ],
    "via": {
      "channel": "api",
      "source": {
        "from": {},
        "to": {},
        "rel": null
      }
    }
  }
}