0
Update a custom field
One script reply has been approved by the moderators Verified

A specific, existing custom field can be updated by making a PUT request on the URL for that custom field.

Created by hugo697 313 days ago Viewed 8968 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 313 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Update a custom field
6
 * A specific, existing custom field can be updated by making a PUT request on the URL for that custom field.
7
 */
8
export async function main(
9
  auth: Asana,
10
  custom_field_gid: string,
11
  opt_pretty: string | undefined,
12
  opt_fields: string | undefined,
13
  body: {
14
    data?: (({ gid?: string; resource_type?: string; [k: string]: unknown } & {
15
      date_value?: { date?: string; date_time?: string; [k: string]: unknown };
16
      display_value?: string;
17
      enabled?: boolean;
18
      enum_options?: ({
19
        gid?: string;
20
        resource_type?: string;
21
        [k: string]: unknown;
22
      } & {
23
        color?: string;
24
        enabled?: boolean;
25
        name?: string;
26
        [k: string]: unknown;
27
      })[];
28
      enum_value?: ({
29
        gid?: string;
30
        resource_type?: string;
31
        [k: string]: unknown;
32
      } & {
33
        color?: string;
34
        enabled?: boolean;
35
        name?: string;
36
        [k: string]: unknown;
37
      }) & { [k: string]: unknown };
38
      multi_enum_values?: ({
39
        gid?: string;
40
        resource_type?: string;
41
        [k: string]: unknown;
42
      } & {
43
        color?: string;
44
        enabled?: boolean;
45
        name?: string;
46
        [k: string]: unknown;
47
      })[];
48
      name?: string;
49
      number_value?: number;
50
      resource_subtype?:
51
        | "text"
52
        | "enum"
53
        | "multi_enum"
54
        | "number"
55
        | "date"
56
        | "people";
57
      text_value?: string;
58
      type?: "text" | "enum" | "multi_enum" | "number";
59
      [k: string]: unknown;
60
    }) & {
61
      asana_created_field?:
62
        | "a_v_requirements"
63
        | "account_name"
64
        | "actionable"
65
        | "align_shipping_link"
66
        | "align_status"
67
        | "allotted_time"
68
        | "appointment"
69
        | "approval_stage"
70
        | "approved"
71
        | "article_series"
72
        | "board_committee"
73
        | "browser"
74
        | "campaign_audience"
75
        | "campaign_project_status"
76
        | "campaign_regions"
77
        | "channel_primary"
78
        | "client_topic_type"
79
        | "complete_by"
80
        | "contact"
81
        | "contact_email_address"
82
        | "content_channels"
83
        | "content_channels_needed"
84
        | "content_stage"
85
        | "content_type"
86
        | "contract"
87
        | "contract_status"
88
        | "cost"
89
        | "creation_stage"
90
        | "creative_channel"
91
        | "creative_needed"
92
        | "creative_needs"
93
        | "data_sensitivity"
94
        | "deal_size"
95
        | "delivery_appt"
96
        | "delivery_appt_date"
97
        | "department"
98
        | "department_responsible"
99
        | "design_request_needed"
100
        | "design_request_type"
101
        | "discussion_category"
102
        | "do_this_task"
103
        | "editorial_content_status"
104
        | "editorial_content_tag"
105
        | "editorial_content_type"
106
        | "effort"
107
        | "effort_level"
108
        | "est_completion_date"
109
        | "estimated_time"
110
        | "estimated_value"
111
        | "expected_cost"
112
        | "external_steps_needed"
113
        | "favorite_idea"
114
        | "feedback_type"
115
        | "financial"
116
        | "funding_amount"
117
        | "grant_application_process"
118
        | "hiring_candidate_status"
119
        | "idea_status"
120
        | "ids_link"
121
        | "ids_patient_link"
122
        | "implementation_stage"
123
        | "insurance"
124
        | "interview_area"
125
        | "interview_question_score"
126
        | "itero_scan_link"
127
        | "job_s_applied_to"
128
        | "lab"
129
        | "launch_status"
130
        | "lead_status"
131
        | "localization_language"
132
        | "localization_market_team"
133
        | "localization_status"
134
        | "meeting_minutes"
135
        | "meeting_needed"
136
        | "minutes"
137
        | "mrr"
138
        | "must_localize"
139
        | "name_of_foundation"
140
        | "need_to_follow_up"
141
        | "next_appointment"
142
        | "next_steps_sales"
143
        | "num_people"
144
        | "number_of_user_reports"
145
        | "office_location"
146
        | "onboarding_activity"
147
        | "owner"
148
        | "participants_needed"
149
        | "patient_date_of_birth"
150
        | "patient_email"
151
        | "patient_phone"
152
        | "patient_status"
153
        | "phone_number"
154
        | "planning_category"
155
        | "point_of_contact"
156
        | "position"
157
        | "post_format"
158
        | "prescription"
159
        | "priority"
160
        | "priority_level"
161
        | "product"
162
        | "product_stage"
163
        | "progress"
164
        | "project_size"
165
        | "project_status"
166
        | "proposed_budget"
167
        | "publish_status"
168
        | "reason_for_scan"
169
        | "referral"
170
        | "request_type"
171
        | "research_status"
172
        | "responsible_department"
173
        | "responsible_team"
174
        | "risk_assessment_status"
175
        | "room_name"
176
        | "sales_counterpart"
177
        | "sentiment"
178
        | "shipping_link"
179
        | "social_channels"
180
        | "stage"
181
        | "status"
182
        | "status_design"
183
        | "status_of_initiative"
184
        | "system_setup"
185
        | "task_progress"
186
        | "team"
187
        | "team_marketing"
188
        | "team_responsible"
189
        | "time_it_takes_to_complete_tasks"
190
        | "timeframe"
191
        | "treatment_type"
192
        | "type_work_requests_it"
193
        | "use_agency"
194
        | "user_name"
195
        | "vendor_category"
196
        | "vendor_type"
197
        | "word_count";
198
      currency_code?: string;
199
      custom_label?: string;
200
      custom_label_position?: "prefix" | "suffix";
201
      description?: string;
202
      enum_options?: ({
203
        gid?: string;
204
        resource_type?: string;
205
        [k: string]: unknown;
206
      } & {
207
        color?: string;
208
        enabled?: boolean;
209
        name?: string;
210
        [k: string]: unknown;
211
      })[];
212
      format?: "currency" | "identifier" | "percentage" | "custom" | "none";
213
      has_notifications_enabled?: boolean;
214
      is_global_to_workspace?: boolean;
215
      precision?: number;
216
      [k: string]: unknown;
217
    }) & { people_value?: string[]; workspace: string; [k: string]: unknown };
218
    [k: string]: unknown;
219
  }
220
) {
221
  const url = new URL(
222
    `https://app.asana.com/api/1.0/custom_fields/${custom_field_gid}`
223
  );
224
  for (const [k, v] of [
225
    ["opt_pretty", opt_pretty],
226
    ["opt_fields", opt_fields],
227
  ]) {
228
    if (v !== undefined && v !== "") {
229
      url.searchParams.append(k, v);
230
    }
231
  }
232
  const response = await fetch(url, {
233
    method: "PUT",
234
    headers: {
235
      "Content-Type": "application/json",
236
      Authorization: "Bearer " + auth.token,
237
    },
238
    body: JSON.stringify(body),
239
  });
240
  if (!response.ok) {
241
    const text = await response.text();
242
    throw new Error(`${response.status} ${text}`);
243
  }
244
  return await response.json();
245
}
246