Edits history of script submission #22316 for ' Describe Object (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))
    }
    
    /**
     * Describe Object
     * Retrieve metadata for an object — fields, types, picklist values, relationships, and requiredness. Use this to discover an org's picklist values (which are not fixed).
     */
    export async function main(auth: RT.Salesforce, sobject: DynSelect_sobject) {
      const apiVersion = auth.api_version || "v60.0"
      const url = new URL(
        `${auth.instance_url}/services/data/${apiVersion}/sobjects/${sobject}/describe`
      )
    
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 9 days ago