Edits history of script submission #22319 for ' Get Related Records (salesforce)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_sobject = string
    export type DynSelect_relationship_name = string
    export type DynMultiselect_fields = string[]
    
    // Dropdown of the org's objects (global describe).
    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))
    }
    
    // Dependent dropdown: child relationship names of the chosen object.
    export async function relationship_name(
      auth: RT.Salesforce,
      sobject: DynSelect_sobject
    ) {
      if (!sobject) return []
      const apiVersion = auth.api_version || "v60.0"
      const response = await fetch(
        `${auth.instance_url}/services/data/${apiVersion}/sobjects/${sobject}/describe`,
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { childRelationships } = (await response.json()) as {
        childRelationships: {
          relationshipName: string | null
          childSObject: string
        }[]
      }
      return childRelationships
        .filter((r) => r.relationshipName)
        .map((r) => ({
          value: r.relationshipName as string,
          label: `${r.relationshipName}${r.childSObject}`,
        }))
        .sort((a, b) => a.label.localeCompare(b.label))
    }
    
    // Dependent multi-select: fields of the related child object behind the chosen relationship.
    export async function fields(
      auth: RT.Salesforce,
      sobject: DynSelect_sobject,
      relationship_name: DynSelect_relationship_name
    ) {
      if (!sobject || !relationship_name) return []
      const apiVersion = auth.api_version || "v60.0"
      const headers = {
        Authorization: `Bearer ${auth.token}`,
        Accept: "application/json",
      }
      const parent = await fetch(
        `${auth.instance_url}/services/data/${apiVersion}/sobjects/${sobject}/describe`,
        { headers }
      )
      if (!parent.ok) {
        throw new Error(`${parent.status} ${await parent.text()}`)
      }
      const { childRelationships } = (await parent.json()) as {
        childRelationships: {
          relationshipName: string | null
          childSObject: string
        }[]
      }
      const child = childRelationships.find(
        (r) => r.relationshipName === relationship_name
      )
      if (!child) return []
      const childDescribe = await fetch(
        `${auth.instance_url}/services/data/${apiVersion}/sobjects/${child.childSObject}/describe`,
        { headers }
      )
      if (!childDescribe.ok) {
        throw new Error(`${childDescribe.status} ${await childDescribe.text()}`)
      }
      const { fields: fieldList } = (await childDescribe.json()) as {
        fields: { name: string; label: string }[]
      }
      return fieldList
        .map((f) => ({ value: f.name, label: `${f.label} (${f.name})` }))
        .sort((a, b) => a.label.localeCompare(b.label))
    }
    
    /**
     * Get Related Records
     * List child records related to a parent via a child relationship name (e.g. Account → Contacts), optionally restricted to a chosen set of fields.
     */
    export async function main(
      auth: RT.Salesforce,
      sobject: DynSelect_sobject,
      record_id: string,
      relationship_name: DynSelect_relationship_name,
      fields: DynMultiselect_fields | undefined
    ) {
      const apiVersion = auth.api_version || "v60.0"
      const url = new URL(
        `${auth.instance_url}/services/data/${apiVersion}/sobjects/${sobject}/${record_id}/${relationship_name}`
      )
      if (fields && fields.length > 0) {
        url.searchParams.append("fields", fields.join(","))
      }
    
      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