0

Change sort order of custom view

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
 * Change sort order of custom view
7
 *
8
 */
9
export async function main(
10
  auth: Zoho,
11
  id: string,
12
  _module: string | undefined,
13
  body: {
14
    custom_views: {
15
      display_value: string;
16
      system_name: string;
17
      category: string;
18
      created_time: string;
19
      modified_time: string;
20
      last_accessed_time: string;
21
      name: string;
22
      created_by: { name: string; id: string; email?: string };
23
      modified_by: { name: string; id: string; email?: string };
24
      module: { name: string; id: string; email?: string };
25
      criteria: {
26
        comparator: string;
27
        field: { api_name: string; id: string };
28
        value: {};
29
        group_operator: string;
30
        group: {}[];
31
      };
32
      default: false | true;
33
      system_defined: false | true;
34
      locked: false | true;
35
      favorite: number;
36
      offline: false | true;
37
      access_type: "shared" | "public" | "only_to_me";
38
      shared_to: {
39
        type: "territories" | "roles" | "groups" | "users";
40
        name: string;
41
        id: string;
42
        subordinates: false | true;
43
      }[];
44
      fields: { id: string; api_name: string; _pin: false | true }[];
45
      sort_by: { id: string; api_name: string };
46
      sort_order: "asc" | "desc";
47
      id: string;
48
    }[];
49
  },
50
) {
51
  const url = new URL(
52
    `https://zohoapis.com/crm/v8/settings/custom_views/${id}/actions/change_sort`,
53
  );
54
  for (const [k, v] of [["module", _module]]) {
55
    if (v !== undefined && v !== "" && k !== undefined) {
56
      url.searchParams.append(k, v);
57
    }
58
  }
59
  const response = await fetch(url, {
60
    method: "PUT",
61
    headers: {
62
      "Content-Type": "application/json",
63
      Authorization: "Zoho-oauthtoken " + auth.token,
64
    },
65
    body: JSON.stringify(body),
66
  });
67
  if (!response.ok) {
68
    const text = await response.text();
69
    throw new Error(`${response.status} ${text}`);
70
  }
71
  return await response.json();
72
}
73