0

Search CRM Objects

by
Published today

Search records of a CRM object type by free text (`query`) and/or property filters.

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
export type DynMultiselect_properties = string[]
44

45
// Dependent checklist: the properties of the chosen object type.
46
export async function properties(
47
  auth: RT.Hubspot,
48
  object_type: DynSelect_object_type
49
) {
50
  if (!object_type) return []
51
  const response = await fetch(
52
    `https://api.hubapi.com/crm/v3/properties/${object_type}`,
53
    {
54
      headers: {
55
        Authorization: `Bearer ${auth.token}`,
56
        Accept: "application/json",
57
      },
58
    }
59
  )
60
  if (!response.ok) {
61
    throw new Error(`${response.status} ${await response.text()}`)
62
  }
63
  const { results } = (await response.json()) as {
64
    results: { name: string; label: string }[]
65
  }
66
  return results.map((p) => ({
67
    value: p.name,
68
    label: `${p.label} (${p.name})`,
69
  }))
70
}
71

72
/**
73
 * Search CRM Objects
74
 * Search records of a CRM object type by free text (`query`) and/or property filters. Filters in one group are ANDed, groups are ORed, e.g. [{"filters": [{"propertyName": "email", "operator": "EQ", "value": "a@b.com"}]}]. Max 200 per page and 10,000 results per search. Object types other than contacts, companies and deals need their own scope on the connection (e.g. tickets, crm.objects.line_items.read, crm.objects.custom.read).
75
 */
76
export async function main(
77
  auth: RT.Hubspot,
78
  object_type: DynSelect_object_type,
79
  query: string | undefined,
80
  filter_groups:
81
    | {
82
        filters: {
83
          propertyName: string
84
          operator: string
85
          value?: any
86
          values?: any[]
87
          highValue?: any
88
        }[]
89
      }[]
90
    | undefined,
91
  properties: DynMultiselect_properties | undefined,
92
  sorts:
93
    | { propertyName: string; direction: "ASCENDING" | "DESCENDING" }[]
94
    | undefined,
95
  limit: number | undefined,
96
  after: string | undefined
97
) {
98
  const url = new URL(
99
    `https://api.hubapi.com/crm/v3/objects/${object_type}/search`
100
  )
101

102
  const body: { [key: string]: any } = {}
103
  if (query != null && query !== "") body.query = query
104
  if (filter_groups?.length) body.filterGroups = filter_groups
105
  if (properties?.length) body.properties = properties
106
  if (sorts?.length) body.sorts = sorts
107
  if (limit != null) body.limit = limit
108
  if (after != null && after !== "") body.after = after
109

110
  const response = await fetch(url, {
111
    method: "POST",
112
    headers: {
113
      Authorization: `Bearer ${auth.token}`,
114
      "Content-Type": "application/json",
115
      Accept: "application/json",
116
    },
117
    body: JSON.stringify(body),
118
  })
119

120
  if (!response.ok) {
121
    throw new Error(`${response.status} ${await response.text()}`)
122
  }
123

124
  return await response.json()
125
}
126