0

Get Deal

by
Published Jun 6, 2022

Retrieve a deal by record ID (or by a unique property via `id_property`), with the properties and associated object types you list.

Script hubspot Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 8 days ago
1
//native
2

3
/**
4
 * Get Deal
5
 * Retrieve a deal by record ID (or by a unique property via `id_property`), with the properties and associated object types you list.
6
 */
7
export async function main(
8
  auth: RT.Hubspot,
9
  deal_id: string,
10
  properties: string[] | undefined,
11
  properties_with_history: string[] | undefined,
12
  associations: string[] | undefined,
13
  archived: boolean | undefined,
14
  id_property: string | undefined
15
) {
16
  const url = new URL(`https://api.hubapi.com/crm/v3/objects/deals/${deal_id}`)
17
  if (properties?.length)
18
    url.searchParams.append("properties", properties.join(","))
19
  if (properties_with_history?.length)
20
    url.searchParams.append(
21
      "propertiesWithHistory",
22
      properties_with_history.join(",")
23
    )
24
  if (associations?.length)
25
    url.searchParams.append("associations", associations.join(","))
26
  if (archived) url.searchParams.append("archived", String(archived))
27
  if (id_property != null && id_property !== "")
28
    url.searchParams.append("idProperty", id_property)
29

30
  const response = await fetch(url, {
31
    headers: {
32
      Authorization: `Bearer ${auth.token}`,
33
      Accept: "application/json",
34
    },
35
  })
36

37
  if (!response.ok) {
38
    throw new Error(`${response.status} ${await response.text()}`)
39
  }
40

41
  return await response.json()
42
}
43

Other submissions
  • Submitted by adam186 Deno
    Created 532 days ago
    1
    import { Client } from "npm:@hubspot/api-client@^8.1.0";
    2
    
    
    3
    /**
    4
     * @param deal_id You may find it in the HubSpot App under the name of `Record ID`
    5
     *
    6
     * @param properties List of the properties to be returned in the response.
    7
     *
    8
     * @param properties_with_history List of the properties to be returned
    9
     * along with their history of previous values.
    10
     *
    11
     * @param id_property The name of a property whose values are unique for this object type.
    12
     */
    13
    type Hubspot = {
    14
      token: string;
    15
    };
    16
    export async function main(
    17
      auth: Hubspot,
    18
      deal_id: string,
    19
      properties?: string[],
    20
      properties_with_history?: string[],
    21
      associations?: string[],
    22
      archived?: boolean,
    23
      id_property?: string,
    24
    ) {
    25
      const client = new Client({
    26
        accessToken: auth.token,
    27
      });
    28
    
    
    29
      try {
    30
        return await client.crm.deals.basicApi.getById(
    31
          deal_id,
    32
          getProp(properties),
    33
          getProp(properties_with_history),
    34
          getProp(associations),
    35
          archived || undefined,
    36
          id_property || undefined,
    37
        );
    38
      } catch (e) {
    39
        throw Error(`
    40
          ${e.code}\n
    41
          Message: ${e.body.message || e.body}\n
    42
        `);
    43
      }
    44
    }
    45
    
    
    46
    function getProp(prop?: string[]) {
    47
      return Array.isArray(prop) && prop.length ? prop : undefined;
    48
    }
    49
    
    
  • Submitted by hugo989 Bun
    Created 140 days ago
    1
    import { Client } from "@hubspot/api-client@^8.1.0";
    2
    
    
    3
    /**
    4
     * @param deal_id You may find it in the HubSpot App under the name of `Record ID`
    5
     *
    6
     * @param properties List of the properties to be returned in the response.
    7
     *
    8
     * @param properties_with_history List of the properties to be returned
    9
     * along with their history of previous values.
    10
     *
    11
     * @param id_property The name of a property whose values are unique for this object type.
    12
     */
    13
    type Hubspot = {
    14
      token: string;
    15
    };
    16
    export async function main(
    17
      auth: Hubspot,
    18
      deal_id: string,
    19
      properties?: string[],
    20
      properties_with_history?: string[],
    21
      associations?: string[],
    22
      archived?: boolean,
    23
      id_property?: string,
    24
    ) {
    25
      const client = new Client({
    26
        accessToken: auth.token,
    27
      });
    28
    
    
    29
      try {
    30
        return await client.crm.deals.basicApi.getById(
    31
          deal_id,
    32
          getProp(properties),
    33
          getProp(properties_with_history),
    34
          getProp(associations),
    35
          archived || undefined,
    36
          id_property || undefined,
    37
        );
    38
      } catch (e) {
    39
        throw Error(`
    40
          ${e.code}\n
    41
          Message: ${e.body.message || e.body}\n
    42
        `);
    43
      }
    44
    }
    45
    
    
    46
    function getProp(prop?: string[]) {
    47
      return Array.isArray(prop) && prop.length ? prop : undefined;
    48
    }
    49