0

Update Multiple Custom Field items on Card

by
Published Oct 30, 2023

Setting, updating, and removing the values for multiple Custom Fields on a card. For more details on updating custom fields check out the Getting Started With Custom Fields

Script trello Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 398 days ago
1
type Trello = {
2
  key: string;
3
  token: string;
4
};
5
/**
6
 * Update Multiple Custom Field items on Card
7
 * Setting, updating, and removing the values for multiple Custom Fields on a card. For more details on updating custom fields check out the Getting Started With Custom Fields
8
 */
9
export async function main(
10
  auth: Trello,
11
  body: {
12
    customFieldItems?: {
13
      idCustomField?: { [k: string]: unknown };
14
      value?: {
15
        text?: string;
16
        checked?: boolean;
17
        date?: string;
18
        number?: number;
19
        [k: string]: unknown;
20
      };
21
      idValue?: { [k: string]: unknown };
22
      [k: string]: unknown;
23
    }[];
24
    [k: string]: unknown;
25
  }
26
) {
27
  const url = new URL(`https://api.trello.com/1/cards/${idCard}/customFields`);
28
  for (const [k, v] of [
29
    ["key", auth.key],
30
    ["token", auth.token],
31
  ]) {
32
    if (v !== undefined && v !== "") {
33
      url.searchParams.append(k, v);
34
    }
35
  }
36
  const response = await fetch(url, {
37
    method: "PUT",
38
    headers: {
39
      "Content-Type": "application/json",
40
      Authorization: undefined,
41
    },
42
    body: JSON.stringify(body),
43
  });
44
  if (!response.ok) {
45
    const text = await response.text();
46
    throw new Error(`${response.status} ${text}`);
47
  }
48
  return await response.text();
49
}
50