0

Create Task

by
Published today

Create a task, optionally attached to a contact, company, deal, ticket or other record.

Script hubspot Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 hours ago
1
//native
2

3
export type DynSelect_object_type = string
4

5
// Dropdown of the standard CRM object types plus the account's custom objects.
6
export async function object_type(auth: RT.Hubspot) {
7
  const standard = [
8
    "contacts",
9
    "companies",
10
    "deals",
11
    "tickets",
12
    "line_items",
13
    "products",
14
    "quotes",
15
    "leads",
16
    "notes",
17
    "tasks",
18
    "meetings",
19
    "calls",
20
    "emails",
21
    "communications",
22
  ].map((o) => ({ value: o, label: o }))
23
  const response = await fetch("https://api.hubapi.com/crm/v3/schemas", {
24
    headers: {
25
      Authorization: `Bearer ${auth.token}`,
26
      Accept: "application/json",
27
    },
28
  })
29
  // A token without crm.schemas.custom.read still gets the standard objects.
30
  if (!response.ok) return standard
31
  const { results } = (await response.json()) as {
32
    results: { fullyQualifiedName: string; labels: { plural: string } }[]
33
  }
34
  return [
35
    ...standard,
36
    ...results.map((s) => ({
37
      value: s.fullyQualifiedName,
38
      label: `${s.labels.plural} (custom)`,
39
    })),
40
  ]
41
}
42

43
/**
44
 * Create Task
45
 * Create a task, optionally attached to a contact, company, deal, ticket or other record. `due_date` is ISO 8601 and defaults to now.
46
 */
47
export async function main(
48
  auth: RT.Hubspot,
49
  subject: string,
50
  to_object_type: DynSelect_object_type | undefined,
51
  to_object_id: string | undefined,
52
  task_body: string | undefined,
53
  due_date: string | undefined,
54
  status:
55
    | "NOT_STARTED"
56
    | "IN_PROGRESS"
57
    | "WAITING"
58
    | "COMPLETED"
59
    | "DEFERRED"
60
    | undefined,
61
  priority: "LOW" | "MEDIUM" | "HIGH" | undefined,
62
  task_type: "TODO" | "CALL" | "EMAIL" | undefined,
63
  owner_id: string | undefined
64
) {
65
  const properties: { [key: string]: any } = {
66
    hs_task_subject: subject,
67
    hs_task_body: task_body,
68
    hs_timestamp: due_date || new Date().toISOString(),
69
    hs_task_status: status,
70
    hs_task_priority: priority,
71
    hs_task_type: task_type,
72
    hubspot_owner_id: owner_id,
73
  }
74
  for (const key of Object.keys(properties)) {
75
    if (properties[key] == null || properties[key] === "")
76
      delete properties[key]
77
  }
78

79
  const response = await fetch("https://api.hubapi.com/crm/v3/objects/tasks", {
80
    method: "POST",
81
    headers: {
82
      Authorization: `Bearer ${auth.token}`,
83
      "Content-Type": "application/json",
84
      Accept: "application/json",
85
    },
86
    body: JSON.stringify({ properties }),
87
  })
88

89
  if (!response.ok) {
90
    throw new Error(`${response.status} ${await response.text()}`)
91
  }
92

93
  const created = await response.json()
94
  if (!to_object_type || !to_object_id) return created
95

96
  const association = await fetch(
97
    `https://api.hubapi.com/crm/v4/objects/tasks/${created.id}/associations/default/${to_object_type}/${to_object_id}`,
98
    {
99
      method: "PUT",
100
      headers: {
101
        Authorization: `Bearer ${auth.token}`,
102
        Accept: "application/json",
103
      },
104
    }
105
  )
106
  if (!association.ok) {
107
    throw new Error(`${association.status} ${await association.text()}`)
108
  }
109

110
  return { ...created, association: await association.json() }
111
}
112