Edits history of script submission #22746 for ' Create Prospect (outreach)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Create Prospect
     * Creates a new prospect in Outreach, optionally linked to an account.
     */
    export async function main(
      auth: RT.Outreach,
      first_name: string | undefined,
      last_name: string | undefined,
      emails: string[] | undefined,
      title: string | undefined,
      company: string | undefined,
      account_id: number | undefined,
      additional_attributes: { [key: string]: any } | undefined
    ) {
      const attributes: { [key: string]: any } = { ...additional_attributes }
      if (first_name !== undefined && first_name !== "") {
        attributes.firstName = first_name
      }
      if (last_name !== undefined && last_name !== "") {
        attributes.lastName = last_name
      }
      if (emails !== undefined && emails.length > 0) {
        attributes.emails = emails
      }
      if (title !== undefined && title !== "") {
        attributes.title = title
      }
      if (company !== undefined && company !== "") {
        attributes.company = company
      }
    
      const data: { [key: string]: any } = { type: "prospect", attributes }
      if (account_id !== undefined) {
        data.relationships = {
          account: { data: { type: "account", id: account_id } },
        }
      }
    
      const response = await fetch("https://api.outreach.io/api/v2/prospects", {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/vnd.api+json",
          Accept: "application/vnd.api+json",
        },
        body: JSON.stringify({ data }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 hours ago