Create Deal

Script hubspot Verified

by armancekpboi ยท 6/6/2022

The script

Submitted by hugo989 Bun
Verified 4 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

Other submissions
  • Submitted by adam186 Deno
    Created 396 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