Edits history of script submission #22747 for ' Create Task (outreach)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Create Task
     * Creates a manual task, optionally tied to a prospect and assigned to an owner.
     */
    export async function main(
      auth: RT.Outreach,
      action: "action_item" | "call" | "email" | "in_person",
      due_at: string,
      note: string | undefined,
      prospect_id: number | undefined,
      owner_id: number | undefined
    ) {
      const attributes: { [key: string]: any } = { action, dueAt: due_at }
      if (note !== undefined && note !== "") {
        attributes.note = note
      }
    
      const relationships: { [key: string]: any } = {}
      if (prospect_id !== undefined) {
        // the writable association is `subject`; the `prospect` relationship is readonly
        relationships.subject = { data: { type: "prospect", id: prospect_id } }
      }
      if (owner_id !== undefined) {
        relationships.owner = { data: { type: "user", id: owner_id } }
      }
    
      const response = await fetch("https://api.outreach.io/api/v2/tasks", {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/vnd.api+json",
          Accept: "application/vnd.api+json",
        },
        body: JSON.stringify({ data: { type: "task", attributes, relationships } }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 hours ago