|
| 1 | +import app from "../../zendesk.app.mjs"; |
| 2 | + |
| 3 | +export default { |
| 4 | + key: "zendesk-set-custom-ticket-fields", |
| 5 | + name: "Set Custom Ticket Fields", |
| 6 | + description: "Sets one or more custom field values on a ticket. [See the documentation](https://developer.zendesk.com/api-reference/ticketing/tickets/tickets/#update-ticket).", |
| 7 | + type: "action", |
| 8 | + version: "0.0.1", |
| 9 | + props: { |
| 10 | + app, |
| 11 | + ticketId: { |
| 12 | + propDefinition: [ |
| 13 | + app, |
| 14 | + "ticketId", |
| 15 | + ], |
| 16 | + }, |
| 17 | + customFields: { |
| 18 | + type: "string[]", |
| 19 | + label: "Custom Fields", |
| 20 | + description: "Array of custom field objects. Each item should be formatted as `{\"id\": \"field_id\", \"value\": \"field_value\"}`. Example: `{\"id\": \"23129751115165\", \"value\": \"ABCDE\"}`", |
| 21 | + }, |
| 22 | + customSubdomain: { |
| 23 | + propDefinition: [ |
| 24 | + app, |
| 25 | + "customSubdomain", |
| 26 | + ], |
| 27 | + }, |
| 28 | + }, |
| 29 | + methods: { |
| 30 | + updateTicket({ |
| 31 | + ticketId, ...args |
| 32 | + } = {}) { |
| 33 | + return this.app.update({ |
| 34 | + path: `/tickets/${ticketId}`, |
| 35 | + ...args, |
| 36 | + }); |
| 37 | + }, |
| 38 | + }, |
| 39 | + async run({ $: step }) { |
| 40 | + const { |
| 41 | + ticketId, |
| 42 | + customFields, |
| 43 | + customSubdomain, |
| 44 | + } = this; |
| 45 | + |
| 46 | + // Parse custom fields from string array to objects |
| 47 | + const parsedCustomFields = customFields.map((field) => { |
| 48 | + try { |
| 49 | + return typeof field === "string" |
| 50 | + ? JSON.parse(field) |
| 51 | + : field; |
| 52 | + } catch (error) { |
| 53 | + throw new Error(`Failed to parse custom field: ${field}. Each field must be valid JSON with "id" and "value" properties.`); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + // Validate custom fields structure |
| 58 | + parsedCustomFields.forEach((field, index) => { |
| 59 | + if (!field.id) { |
| 60 | + throw new Error(`Custom field at index ${index} is missing required "id" property`); |
| 61 | + } |
| 62 | + if (field.value === undefined) { |
| 63 | + throw new Error(`Custom field at index ${index} is missing required "value" property`); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + const response = await this.updateTicket({ |
| 68 | + step, |
| 69 | + ticketId, |
| 70 | + customSubdomain, |
| 71 | + data: { |
| 72 | + ticket: { |
| 73 | + custom_fields: parsedCustomFields, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }); |
| 77 | + |
| 78 | + step.export("$summary", `Successfully updated ${parsedCustomFields.length} custom field(s) on ticket ${response.ticket.id}`); |
| 79 | + |
| 80 | + return response; |
| 81 | + }, |
| 82 | +}; |
0 commit comments