Visual FoxPro Zendesk: Create a Ticket

Back to Index

Creates a new Zendesk ticket.

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 printer is on fire!",
    "comment": {
      "body": "The smoke is very colorful."
    }
  }
}' \
  -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 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 printer is on fire!")
loJson.UpdateString("ticket.comment.body","The smoke is very colorful.")

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
        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/7.json",
    "id": 7,
    "external_id": null,
    "via": {
      "channel": "api",
      "source": {
        "from": {},
        "to": {},
        "rel": null
      }
    },
    "created_at": "2018-04-15T18:13:15Z",
    "updated_at": "2018-04-15T18:13:15Z",
    "type": null,
    "subject": "My printer is on fire!",
    "raw_subject": "My printer is on fire!",
    "description": "The smoke is very colorful.",
    "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": 391248331553,
    "ticket_id": 7,
    "created_at": "2018-04-15T18:13:15Z",
    "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": 391248331653,
        "type": "Comment",
        "author_id": 361801727573,
        "body": "The smoke is very colorful.",
        "html_body": "<div class=\"zd-comment\"><p dir=\"auto\">The smoke is very colorful.</p></div>",
        "plain_body": "The smoke is very colorful.",
        "public": true,
        "attachments": [
        ],
        "audit_id": 391248331553
      },
      {
        "id": 391248331833,
        "type": "Create",
        "value": "My printer is on fire!",
        "field_name": "subject"
      },
      {
        "id": 391248331973,
        "type": "Create",
        "value": "361801727573",
        "field_name": "requester_id"
      },
      {
        "id": 391248332053,
        "type": "Create",
        "value": "open",
        "field_name": "status"
      },
      {
        "id": 391248332193,
        "type": "Create",
        "value": null,
        "field_name": "priority"
      },
      {
        "id": 391248332313,
        "type": "Create",
        "value": null,
        "field_name": "type"
      },
      {
        "id": 391248332353,
        "type": "Create",
        "value": "361801727573",
        "field_name": "assignee_id"
      },
      {
        "id": 391248332373,
        "type": "Create",
        "value": "360000681553",
        "field_name": "group_id"
      },
      {
        "id": 391248332393,
        "type": "Create",
        "value": "360015071793",
        "field_name": "organization_id"
      },
      {
        "id": 391248332413,
        "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": 391248332433,
        "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
      }
    }
  }
}