Edits history of script submission #23827 for ' Create Organization (zendesk)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Create Organization
     * Create an organization. Names must be unique in the account. Users whose email domain is in `domain_names` can be mapped to it automatically. Set any other field through `fields`, e.g. `{ "organization_fields": { "tier": "gold" }, "group_id": 123 }`.
     */
    export async function main(
      auth: RT.Zendesk,
      name: string,
      domain_names: string[] | undefined,
      details: string | undefined,
      notes: string | undefined,
      external_id: string | undefined,
      tags: string[] | undefined,
      fields: { [key: string]: any } | undefined
    ) {
      const organization = {
        ...fields,
        name: name || undefined,
        domain_names: domain_names?.length ? domain_names : undefined,
        details: details || undefined,
        notes: notes || undefined,
        external_id: external_id || undefined,
        tags: tags?.length ? tags : undefined,
      }
    
      const response = await fetch(
        `https://${auth.subdomain}.zendesk.com/api/v2/organizations`,
        {
          method: "POST",
          headers: {
            Authorization: auth.token
              ? `Bearer ${auth.token}`
              : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify({ organization }),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 days ago