0

Get emails related record

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