0

Get emails related records

by
Published Oct 17, 2025
Script zoho Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Zoho = {
3
  token: string;
4
};
5
/**
6
 * Get emails related records
7
 *
8
 */
9
export async function main(
10
  auth: Zoho,
11
  module_name: string,
12
  record_id: string,
13
  _type:
14
    | "all_contacts_draft_crm_emails"
15
    | "all_contacts_scheduled_crm_emails"
16
    | "all_contacts_sent_crm_emails"
17
    | "sent_from_crm"
18
    | "scheduled_in_crm"
19
    | "drafts"
20
    | "user_emails"
21
    | undefined,
22
  index: string | undefined,
23
  owner_id: string | undefined,
24
) {
25
  const url = new URL(
26
    `https://zohoapis.com/crm/v8/${module_name}/${record_id}/Emails`,
27
  );
28
  for (const [k, v] of [
29
    ["type", _type],
30
    ["index", index],
31
    ["owner_id", owner_id],
32
  ]) {
33
    if (v !== undefined && v !== "" && k !== undefined) {
34
      url.searchParams.append(k, v);
35
    }
36
  }
37
  const response = await fetch(url, {
38
    method: "GET",
39
    headers: {
40
      Authorization: "Zoho-oauthtoken " + auth.token,
41
    },
42
    body: undefined,
43
  });
44
  if (!response.ok) {
45
    const text = await response.text();
46
    throw new Error(`${response.status} ${text}`);
47
  }
48
  return await response.json();
49
}
50