Edits history of script submission #22315 for ' Delete Record (salesforce)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_sobject = string
    
    // Dropdown of the org's objects (global describe), so users pick instead of typing an API name.
    export async function sobject(auth: RT.Salesforce) {
      const apiVersion = auth.api_version || "v60.0"
      const response = await fetch(
        `${auth.instance_url}/services/data/${apiVersion}/sobjects/`,
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { sobjects } = (await response.json()) as {
        sobjects: { name: string; label: string }[]
      }
      return sobjects
        .map((o) => ({ value: o.name, label: `${o.label} (${o.name})` }))
        .sort((a, b) => a.label.localeCompare(b.label))
    }
    
    /**
     * Delete Record
     * Delete a record by ID. Salesforce returns 204 No Content on success, so this returns {success, id}.
     */
    export async function main(
      auth: RT.Salesforce,
      sobject: DynSelect_sobject,
      record_id: string
    ) {
      const apiVersion = auth.api_version || "v60.0"
      const url = new URL(
        `${auth.instance_url}/services/data/${apiVersion}/sobjects/${sobject}/${record_id}`
      )
    
      const response = await fetch(url, {
        method: "DELETE",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return { success: true, id: record_id }
    }
    

    Submitted by hugo989 9 days ago