Visual FoxPro 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

Visual FoxPro Example

LOCAL loRest
LOCAL lnSuccess
LOCAL lnBTls
LOCAL lnPort
LOCAL lnBAutoReconnect
LOCAL loJson
LOCAL loSbRequestBody
LOCAL loSbResponseBody
LOCAL lnRespStatusCode
LOCAL loJsonResponse
LOCAL lcTicketUrl
LOCAL lnTicketId
LOCAL lcTicketExternal_id
LOCAL lcTicketViaChannel
LOCAL lcTicketViaSourceRel
LOCAL lcTicketCreated_at
LOCAL lcTicketUpdated_at
LOCAL lcTicketType
LOCAL lcTicketSubject
LOCAL lcTicketRaw_subject
LOCAL lcTicketDescription
LOCAL lcTicketPriority
LOCAL lcTicketStatus
LOCAL lcTicketRecipient
LOCAL lnTicketRequester_id
LOCAL lnTicketSubmitter_id
LOCAL lnTicketAssignee_id
LOCAL lnTicketOrganization_id
LOCAL lnTicketGroup_id
LOCAL lcTicketForum_topic_id
LOCAL lcTicketProblem_id
LOCAL lnTicketHas_incidents
LOCAL lnTicketIs_public
LOCAL lcTicketDue_at
LOCAL lcTicketSatisfaction_rating
LOCAL lnTicketBrand_id
LOCAL lnTicketAllow_channelback
LOCAL lnAuditId
LOCAL lnAuditTicket_id
LOCAL lcAuditCreated_at
LOCAL lnAuditAuthor_id
LOCAL lcAuditMetadataSystemIp_address
LOCAL lcAuditMetadataSystemLocation
LOCAL lnAuditMetadataSystemLatitude
LOCAL lnAuditMetadataSystemLongitude
LOCAL lcAuditViaChannel
LOCAL lcAuditViaSourceRel
LOCAL i
LOCAL lnCount_i
LOCAL lnId
LOCAL lcType
LOCAL lnAuthor_id
LOCAL lcBody
LOCAL lcHtml_body
LOCAL lcPlain_body
LOCAL lnPublic
LOCAL lnAudit_id
LOCAL lcValue
LOCAL lcField_name
LOCAL lcViaChannel
LOCAL lnViaSourceFromDeleted
LOCAL lcViaSourceFromTitle
LOCAL lnViaSourceFromId
LOCAL lcViaSourceRel
LOCAL lcSubject
LOCAL j
LOCAL lnCount_j
LOCAL lcUrl
LOCAL lcFile_name
LOCAL lcContent_url
LOCAL lcMapped_content_url
LOCAL lcContent_type
LOCAL lnSize
LOCAL lcWidth
LOCAL lcHeight
LOCAL lnInline
LOCAL k
LOCAL lnCount_k
LOCAL lnIntVal

loRest = CreateObject('Chilkat_9_5_0.Rest')

*  URL: https://subdomain.zendesk.com/api/v2/tickets.json
lnBTls = 1
lnPort = 443
lnBAutoReconnect = 1
lnSuccess = loRest.Connect("subdomain.zendesk.com",lnPort,lnBTls,lnBAutoReconnect)
IF (lnSuccess <> 1) THEN
    ? "ConnectFailReason: " + STR(loRest.ConnectFailReason)
    ? loRest.LastErrorText
    RELEASE loRest
    CANCEL
ENDIF

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

loJson = CreateObject('Chilkat_9_5_0.JsonObject')
loJson.UpdateString("ticket.subject","My application crashed.  Please help.")
loJson.UpdateString("ticket.comment.body","Here is a screen shot")
loJson.UpdateString("ticket.comment.uploads[0]","W7Y11FCxfQlzOFPDbVDUEE45C")

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

loSbRequestBody = CreateObject('Chilkat_9_5_0.StringBuilder')
loJson.EmitSb(loSbRequestBody)
loSbResponseBody = CreateObject('Chilkat_9_5_0.StringBuilder')
lnSuccess = loRest.FullRequestSb("POST","/api/v2/tickets.json",loSbRequestBody,loSbResponseBody)
IF (lnSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loRest
    RELEASE loJson
    RELEASE loSbRequestBody
    RELEASE loSbResponseBody
    CANCEL
ENDIF

lnRespStatusCode = loRest.ResponseStatusCode
IF (lnRespStatusCode >= 400) THEN
    ? "Response Status Code = " + STR(lnRespStatusCode)
    ? "Response Header:"
    ? loRest.ResponseHeader
    ? "Response Body:"
    ? loSbResponseBody.GetAsString()
    RELEASE loRest
    RELEASE loJson
    RELEASE loSbRequestBody
    RELEASE loSbResponseBody
    CANCEL
ENDIF

loJsonResponse = CreateObject('Chilkat_9_5_0.JsonObject')
loJsonResponse.LoadSb(loSbResponseBody)

lcTicketUrl = loJsonResponse.StringOf("ticket.url")
lnTicketId = loJsonResponse.IntOf("ticket.id")
lcTicketExternal_id = loJsonResponse.StringOf("ticket.external_id")
lcTicketViaChannel = loJsonResponse.StringOf("ticket.via.channel")
lcTicketViaSourceRel = loJsonResponse.StringOf("ticket.via.source.rel")
lcTicketCreated_at = loJsonResponse.StringOf("ticket.created_at")
lcTicketUpdated_at = loJsonResponse.StringOf("ticket.updated_at")
lcTicketType = loJsonResponse.StringOf("ticket.type")
lcTicketSubject = loJsonResponse.StringOf("ticket.subject")
lcTicketRaw_subject = loJsonResponse.StringOf("ticket.raw_subject")
lcTicketDescription = loJsonResponse.StringOf("ticket.description")
lcTicketPriority = loJsonResponse.StringOf("ticket.priority")
lcTicketStatus = loJsonResponse.StringOf("ticket.status")
lcTicketRecipient = loJsonResponse.StringOf("ticket.recipient")
lnTicketRequester_id = loJsonResponse.IntOf("ticket.requester_id")
lnTicketSubmitter_id = loJsonResponse.IntOf("ticket.submitter_id")
lnTicketAssignee_id = loJsonResponse.IntOf("ticket.assignee_id")
lnTicketOrganization_id = loJsonResponse.IntOf("ticket.organization_id")
lnTicketGroup_id = loJsonResponse.IntOf("ticket.group_id")
lcTicketForum_topic_id = loJsonResponse.StringOf("ticket.forum_topic_id")
lcTicketProblem_id = loJsonResponse.StringOf("ticket.problem_id")
lnTicketHas_incidents = loJsonResponse.BoolOf("ticket.has_incidents")
lnTicketIs_public = loJsonResponse.BoolOf("ticket.is_public")
lcTicketDue_at = loJsonResponse.StringOf("ticket.due_at")
lcTicketSatisfaction_rating = loJsonResponse.StringOf("ticket.satisfaction_rating")
lnTicketBrand_id = loJsonResponse.IntOf("ticket.brand_id")
lnTicketAllow_channelback = loJsonResponse.BoolOf("ticket.allow_channelback")
lnAuditId = loJsonResponse.IntOf("audit.id")
lnAuditTicket_id = loJsonResponse.IntOf("audit.ticket_id")
lcAuditCreated_at = loJsonResponse.StringOf("audit.created_at")
lnAuditAuthor_id = loJsonResponse.IntOf("audit.author_id")
lcAuditMetadataSystemIp_address = loJsonResponse.StringOf("audit.metadata.system.ip_address")
lcAuditMetadataSystemLocation = loJsonResponse.StringOf("audit.metadata.system.location")
lnAuditMetadataSystemLatitude = loJsonResponse.IntOf("audit.metadata.system.latitude")
lnAuditMetadataSystemLongitude = loJsonResponse.IntOf("audit.metadata.system.longitude")
lcAuditViaChannel = loJsonResponse.StringOf("audit.via.channel")
lcAuditViaSourceRel = loJsonResponse.StringOf("audit.via.source.rel")
i = 0
lnCount_i = loJsonResponse.SizeOfArray("ticket.collaborator_ids")
DO WHILE i < lnCount_i
    loJsonResponse.I = i
    i = i + 1
ENDDO
i = 0
lnCount_i = loJsonResponse.SizeOfArray("ticket.follower_ids")
DO WHILE i < lnCount_i
    loJsonResponse.I = i
    i = i + 1
ENDDO
i = 0
lnCount_i = loJsonResponse.SizeOfArray("ticket.email_cc_ids")
DO WHILE i < lnCount_i
    loJsonResponse.I = i
    i = i + 1
ENDDO
i = 0
lnCount_i = loJsonResponse.SizeOfArray("ticket.tags")
DO WHILE i < lnCount_i
    loJsonResponse.I = i
    i = i + 1
ENDDO
i = 0
lnCount_i = loJsonResponse.SizeOfArray("ticket.custom_fields")
DO WHILE i < lnCount_i
    loJsonResponse.I = i
    i = i + 1
ENDDO
i = 0
lnCount_i = loJsonResponse.SizeOfArray("ticket.sharing_agreement_ids")
DO WHILE i < lnCount_i
    loJsonResponse.I = i
    i = i + 1
ENDDO
i = 0
lnCount_i = loJsonResponse.SizeOfArray("ticket.fields")
DO WHILE i < lnCount_i
    loJsonResponse.I = i
    i = i + 1
ENDDO
i = 0
lnCount_i = loJsonResponse.SizeOfArray("ticket.followup_ids")
DO WHILE i < lnCount_i
    loJsonResponse.I = i
    i = i + 1
ENDDO
i = 0
lnCount_i = loJsonResponse.SizeOfArray("audit.events")
DO WHILE i < lnCount_i
    loJsonResponse.I = i
    lnId = loJsonResponse.IntOf("audit.events[i].id")
    lcType = loJsonResponse.StringOf("audit.events[i].type")
    lnAuthor_id = loJsonResponse.IntOf("audit.events[i].author_id")
    lcBody = loJsonResponse.StringOf("audit.events[i].body")
    lcHtml_body = loJsonResponse.StringOf("audit.events[i].html_body")
    lcPlain_body = loJsonResponse.StringOf("audit.events[i].plain_body")
    lnPublic = loJsonResponse.BoolOf("audit.events[i].public")
    lnAudit_id = loJsonResponse.IntOf("audit.events[i].audit_id")
    lcValue = loJsonResponse.StringOf("audit.events[i].value")
    lcField_name = loJsonResponse.StringOf("audit.events[i].field_name")
    lcViaChannel = loJsonResponse.StringOf("audit.events[i].via.channel")
    lnViaSourceFromDeleted = loJsonResponse.BoolOf("audit.events[i].via.source.from.deleted")
    lcViaSourceFromTitle = loJsonResponse.StringOf("audit.events[i].via.source.from.title")
    lnViaSourceFromId = loJsonResponse.IntOf("audit.events[i].via.source.from.id")
    lcViaSourceRel = loJsonResponse.StringOf("audit.events[i].via.source.rel")
    lcSubject = loJsonResponse.StringOf("audit.events[i].subject")
    j = 0
    lnCount_j = loJsonResponse.SizeOfArray("audit.events[i].attachments")
    DO WHILE j < lnCount_j
        loJsonResponse.J = j
        lcUrl = loJsonResponse.StringOf("audit.events[i].attachments[j].url")
        lnId = loJsonResponse.IntOf("audit.events[i].attachments[j].id")
        lcFile_name = loJsonResponse.StringOf("audit.events[i].attachments[j].file_name")
        lcContent_url = loJsonResponse.StringOf("audit.events[i].attachments[j].content_url")
        lcMapped_content_url = loJsonResponse.StringOf("audit.events[i].attachments[j].mapped_content_url")
        lcContent_type = loJsonResponse.StringOf("audit.events[i].attachments[j].content_type")
        lnSize = loJsonResponse.IntOf("audit.events[i].attachments[j].size")
        lcWidth = loJsonResponse.StringOf("audit.events[i].attachments[j].width")
        lcHeight = loJsonResponse.StringOf("audit.events[i].attachments[j].height")
        lnInline = loJsonResponse.BoolOf("audit.events[i].attachments[j].inline")
        k = 0
        lnCount_k = loJsonResponse.SizeOfArray("audit.events[i].attachments[j].thumbnails")
        DO WHILE k < lnCount_k
            loJsonResponse.K = k
            k = k + 1
        ENDDO
        j = j + 1
    ENDDO
    j = 0
    lnCount_j = loJsonResponse.SizeOfArray("audit.events[i].recipients")
    DO WHILE j < lnCount_j
        loJsonResponse.J = j
        lnIntVal = loJsonResponse.IntOf("audit.events[i].recipients[j]")
        j = j + 1
    ENDDO
    i = i + 1
ENDDO

RELEASE loRest
RELEASE loJson
RELEASE loSbRequestBody
RELEASE loSbResponseBody
RELEASE loJsonResponse

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
      }
    }
  }
}