0

Create Deal

by
Published Jun 6, 2022

Create a deal from the common fields.

Script hubspot Verified

The script

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

3
/**
4
 * Create Deal
5
 * Create a deal from the common fields. `pipeline` and `dealstage` take internal IDs (see List Pipelines and Stages); `closedate` is an ISO 8601 date. Use Create CRM Object for any other property.
6
 */
7
export async function main(
8
  auth: RT.Hubspot,
9
  amount: string | undefined,
10
  closedate: string | undefined,
11
  dealname: string | undefined,
12
  dealstage: string | undefined,
13
  hubspot_owner_id: string | undefined,
14
  pipeline: string | undefined
15
) {
16
  const properties: { [key: string]: string } = {}
17
  const fields = {
18
    amount,
19
    closedate,
20
    dealname,
21
    dealstage,
22
    hubspot_owner_id,
23
    pipeline,
24
  }
25
  for (const [key, value] of Object.entries(fields)) {
26
    if (value != null && value !== "") properties[key] = value
27
  }
28

29
  const response = await fetch("https://api.hubapi.com/crm/v3/objects/deals", {
30
    method: "POST",
31
    headers: {
32
      Authorization: `Bearer ${auth.token}`,
33
      "Content-Type": "application/json",
34
      Accept: "application/json",
35
    },
36
    body: JSON.stringify({ properties }),
37
  })
38

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

43
  return await response.json()
44
}
45

Other submissions
  • Submitted by adam186 Deno
    Created 539 days ago
    1
    import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
    2
    import { Client } from "npm:@hubspot/api-client@^8.1.0";
    3
    
    
    4
    /**
    5
     * @param closedate Uses the following date-time format:
    6
     * `2019-12-07T16:50:06.678Z` but time value can be omitted.
    7
     */
    8
    type Hubspot = {
    9
      token: string;
    10
    };
    11
    export async function main(
    12
      auth: Hubspot,
    13
      amount?: string,
    14
      closedate?: string,
    15
      dealname?: string,
    16
      dealstage?: string,
    17
      hubspot_owner_id?: string,
    18
      pipeline?: string,
    19
    ) {
    20
      const client = new Client({
    21
        accessToken: auth.token,
    22
      });
    23
      const properties = removeObjectEmptyFields({
    24
        amount,
    25
        closedate,
    26
        dealname,
    27
        dealstage,
    28
        hubspot_owner_id,
    29
        pipeline,
    30
      });
    31
      try {
    32
        return await client.crm.deals.basicApi.create({ properties });
    33
      } catch (e) {
    34
        throw Error(`
    35
          ${e.code} - ${e.body.category}\n
    36
          Message: ${e.body.message}\n
    37
          Correlation ID: ${e.body.correlationId}
    38
        `);
    39
      }
    40
    }
    41
    
    
  • Submitted by hugo989 Bun
    Created 147 days ago
    1
    import { Client } from "@hubspot/api-client@^8.1.0";
    2
    
    
    3
    /**
    4
     * @param closedate Uses the following date-time format:
    5
     * `2019-12-07T16:50:06.678Z` but time value can be omitted.
    6
     */
    7
    type Hubspot = {
    8
      token: string;
    9
    };
    10
    export async function main(
    11
      auth: Hubspot,
    12
      amount?: string,
    13
      closedate?: string,
    14
      dealname?: string,
    15
      dealstage?: string,
    16
      hubspot_owner_id?: string,
    17
      pipeline?: string,
    18
    ) {
    19
      const client = new Client({
    20
        accessToken: auth.token,
    21
      });
    22
      const properties = removeObjectEmptyFields({
    23
        amount,
    24
        closedate,
    25
        dealname,
    26
        dealstage,
    27
        hubspot_owner_id,
    28
        pipeline,
    29
      });
    30
      try {
    31
        return await client.crm.deals.basicApi.create({ properties });
    32
      } catch (e) {
    33
        throw Error(`
    34
          ${e.code} - ${e.body.category}\n
    35
          Message: ${e.body.message}\n
    36
          Correlation ID: ${e.body.correlationId}
    37
        `);
    38
      }
    39
    }
    40
    
    
    41
    function removeObjectEmptyFields(
    42
      object?: Record<string, any>,
    43
      removeEmptyArraysAndObjects = true,
    44
      createNewObject = true,
    45
    ) {
    46
      if (!object || typeof object !== "object") return {}
    47
      const obj = createNewObject ? { ...object } : object
    48
      const emptyValues = [undefined, null, ""]
    49
      for (const key in obj) {
    50
        const value = obj[key]
    51
        if (emptyValues.includes(value)) {
    52
          delete obj[key]
    53
        } else if (typeof value === "object") {
    54
          if (Object.keys(value).length) {
    55
            obj[key] = removeObjectEmptyFields(value, removeEmptyArraysAndObjects, false)
    56
          }
    57
          if (!Object.keys(value).length && removeEmptyArraysAndObjects) {
    58
            delete obj[key]
    59
          }
    60
        }
    61
      }
    62
      return obj
    63
    }
    64