Get Schema of all sObjects in one Api Call, like Schema Builder

I am building a web tool to get the schema of a particular sObject, or an entire org, and save it as a Json file using the SalesForce Rest API.

To do this, I am first getting a list of all the sObjects from /services/data/v47.0/sobjects/, then calling /services/data/v47.0/sobjects/{SOBJECT_NAME}/describe on each one of them to get the schema.

This approach works, but needs one api call for each object.
Using the Batch Api can reduce the api calls by a factor of 25, but the data still needs a lot of resource-intensive post processing, which might be an issue on lower-powered devices.

I was poking around Schema Builder, and found that it loads a complete Schema of the org as a Json Object from the endpoint salesforce.com/_ui/platform/schema/ui/schemabuilder/SchemaBuilderServlet.

The loaded Json has all the objects of the org along with the fields and children, and needs no further processing.
Eg:

"objs":{
    "ConsumptionSchedule":{
        "afl":true,
        "h":false,
        "l":"Consumption Schedule",
        "n":"ConsumptionSchedule",
        "rflds":{
        "BillingTermUnit":{
            "dt":"e",
            "eid":"ConsumptionSchedule",
            "r":true,
            "t":"Picklist() ",
            "d":[

            ],
            "u":"/_ui/common/config/field/StandardFieldAttributes/d?id=BillingTermUnit&type=ConsumptionSchedule&retURL=%2F_ui%2Fplatform%2Fschema%2Fui%2Fschemabuilder%2FSchemaBuilderUi&setupid=ConsumptionScheduleFields",
            "rel":"lu",
            "id":"BillingTermUnit",
            "l":"Billing Term Unit",
            "cust":false,
            "n":"BillingTermUnit"
        },
        "CreatedBy":{
            "dt":"Y",
            "eid":"ConsumptionSchedule",
            "r":true,
            "t":"Lookup(User) ",
            "d":[
            "User"
            ],
            "u":"/_ui/common/config/field/StandardFieldAttributes/d?id=CreatedBy&type=ConsumptionSchedule&retURL=%2F_ui%2Fplatform%2Fschema%2Fui%2Fschemabuilder%2FSchemaBuilderUi&setupid=ConsumptionScheduleFields",
            "rel":"lu",
            "id":"CreatedBy",
            "l":"Created By",
            "cust":false,
            "n":"CreatedBy"
        },
        "LastModifiedBy":{
            "dt":"Y",
            "eid":"ConsumptionSchedule",
            "r":true,
            "t":"Lookup(User) ",
            "d":[
            "User"
            ],
            "u":"/_ui/common/config/field/StandardFieldAttributes/d?id=LastModifiedBy&type=ConsumptionSchedule&retURL=%2F_ui%2Fplatform%2Fschema%2Fui%2Fschemabuilder%2FSchemaBuilderUi&setupid=ConsumptionScheduleFields",
            "rel":"lu",
            "id":"LastModifiedBy",
            "l":"Last Modified By",
            "cust":false,
            "n":"LastModifiedBy"
        },
        "RatingMethod":{
            "dt":"e",
            "eid":"ConsumptionSchedule",
            "r":true,
            "t":"Picklist() ",
            "d":[

            ],
            "u":"/_ui/common/config/field/StandardFieldAttributes/d?id=RatingMethod&type=ConsumptionSchedule&retURL=%2F_ui%2Fplatform%2Fschema%2Fui%2Fschemabuilder%2FSchemaBuilderUi&setupid=ConsumptionScheduleFields",
            "rel":"lu",
            "id":"RatingMethod",
            "l":"Rating Method",
            "cust":false,
            "n":"RatingMethod"
        },
        "Type":{
            "dt":"e",
            "eid":"ConsumptionSchedule",
            "r":true,
            "t":"Picklist() ",
            "d":[

            ],
            "u":"/_ui/common/config/field/StandardFieldAttributes/d?id=Type&type=ConsumptionSchedule&retURL=%2F_ui%2Fplatform%2Fschema%2Fui%2Fschemabuilder%2FSchemaBuilderUi&setupid=ConsumptionScheduleFields",
            "rel":"lu",
            "id":"Type",
            "l":"Type",
            "cust":false,
            "n":"Type"
        }
        },
        "u":"/p/setup/layout/LayoutFieldList?type=ConsumptionSchedule&setupid=ConsumptionScheduleFields",
        "filter_opts":[
        "sel",
        "std"
        ],
        "plu":"/ui/setup/layout/PageLayouts?type=ConsumptionSchedule&setupid=ConsumptionScheduleLayouts",
        "le":11460.0,
        "flds":{
        "IsActive":{
            "dt":"B",
            "eid":"ConsumptionSchedule",
            "r":false,
            "t":"Checkbox ",
            "d":[

            ],
            "u":"/_ui/common/config/field/StandardFieldAttributes/d?id=IsActive&type=ConsumptionSchedule&retURL=%2F_ui%2Fplatform%2Fschema%2Fui%2Fschemabuilder%2FSchemaBuilderUi&setupid=ConsumptionScheduleFields",
            "rel":"",
            "id":"IsActive",
            "l":"Active",
            "cust":false,
            "n":"IsActive"
        },
        "BillingTerm":{
            "dt":"N",
            "eid":"ConsumptionSchedule",
            "r":true,
            "t":"Number(9, 0) ",
            "d":[

            ],
            "u":"/_ui/common/config/field/StandardFieldAttributes/d?id=BillingTerm&type=ConsumptionSchedule&retURL=%2F_ui%2Fplatform%2Fschema%2Fui%2Fschemabuilder%2FSchemaBuilderUi&setupid=ConsumptionScheduleFields",
            "rel":"",
            "id":"BillingTerm",
            "l":"Billing Term",
            "cust":false,
            "n":"BillingTerm"
        },
        "BillingTermUnit":{
            "dt":"e",
            "eid":"ConsumptionSchedule",
            "r":true,
            "t":"Picklist() ",
            "d":[

            ],
            "u":"/_ui/common/config/field/StandardFieldAttributes/d?id=BillingTermUnit&type=ConsumptionSchedule&retURL=%2F_ui%2Fplatform%2Fschema%2Fui%2Fschemabuilder%2FSchemaBuilderUi&setupid=ConsumptionScheduleFields",
            "rel":"lu",
            "id":"BillingTermUnit",
            "l":"Billing Term Unit",
            "cust":false,
            "n":"BillingTermUnit"
        },
        "Name":{
            "dt":"S",
            "eid":"ConsumptionSchedule",
            "r":true,
            "t":"Text(255) ",
            "d":[

            ],
            "u":"/_ui/common/config/field/StandardFieldAttributes/d?id=Name&type=ConsumptionSchedule&retURL=%2F_ui%2Fplatform%2Fschema%2Fui%2Fschemabuilder%2FSchemaBuilderUi&setupid=ConsumptionScheduleFields",
            "rel":"",
            "id":"Name",
            "l":"Consumption Schedule Name",
            "cust":false,
            "n":"Name"
        },
        "CreatedBy":{
            "dt":"Y",
            "eid":"ConsumptionSchedule",
            "r":true,
            "t":"Lookup(User) ",
            "d":[
            "User"
            ],
            "u":"/_ui/common/config/field/StandardFieldAttributes/d?id=CreatedBy&type=ConsumptionSchedule&retURL=%2F_ui%2Fplatform%2Fschema%2Fui%2Fschemabuilder%2FSchemaBuilderUi&setupid=ConsumptionScheduleFields",
            "rel":"lu",
            "id":"CreatedBy",
            "l":"Created By",
            "cust":false,
            "n":"CreatedBy"
        },
        "CreatedDate":{
            "dt":"F",
            "eid":"ConsumptionSchedule",
            "r":true,
            "t":"Date/Time ",
            "d":[

            ],
            "u":"/_ui/common/config/field/StandardFieldAttributes/d?id=CreatedDate&type=ConsumptionSchedule&retURL=%2F_ui%2Fplatform%2Fschema%2Fui%2Fschemabuilder%2FSchemaBuilderUi&setupid=ConsumptionScheduleFields",
            "rel":"",
            "id":"CreatedDate",
            "l":"Created Date",
            "cust":false,
            "n":"CreatedDate"
        },
        "Description":{
            "dt":"J",
            "eid":"ConsumptionSchedule",
            "r":false,
            "t":"Long Text Area(32000) ",
            "d":[

            ],
            "u":"/_ui/common/config/field/StandardFieldAttributes/d?id=Description&type=ConsumptionSchedule&retURL=%2F_ui%2Fplatform%2Fschema%2Fui%2Fschemabuilder%2FSchemaBuilderUi&setupid=ConsumptionScheduleFields",
            "rel":"",
            "id":"Description",
            "l":"Description",
            "cust":false,
            "n":"Description"
        },
        "LastModifiedBy":{
            "dt":"Y",
            "eid":"ConsumptionSchedule",
            "r":true,
            "t":"Lookup(User) ",
            "d":[
            "User"
            ],
            "u":"/_ui/common/config/field/StandardFieldAttributes/d?id=LastModifiedBy&type=ConsumptionSchedule&retURL=%2F_ui%2Fplatform%2Fschema%2Fui%2Fschemabuilder%2FSchemaBuilderUi&setupid=ConsumptionScheduleFields",
            "rel":"lu",
            "id":"LastModifiedBy",
            "l":"Last Modified By",
            "cust":false,
            "n":"LastModifiedBy"
        },
        "LastModifiedDate":{
            "dt":"F",
            "eid":"ConsumptionSchedule",
            "r":true,
            "t":"Date/Time ",
            "d":[

            ],
            "u":"/_ui/common/config/field/StandardFieldAttributes/d?id=LastModifiedDate&type=ConsumptionSchedule&retURL=%2F_ui%2Fplatform%2Fschema%2Fui%2Fschemabuilder%2FSchemaBuilderUi&setupid=ConsumptionScheduleFields",
            "rel":"",
            "id":"LastModifiedDate",
            "l":"Last Modified Date",
            "cust":false,
            "n":"LastModifiedDate"
        },
        "MatchingAttribute":{
            "dt":"S",
            "eid":"ConsumptionSchedule",
            "r":false,
            "t":"Text(255) ",
            "d":[

            ],
            "u":"/_ui/common/config/field/StandardFieldAttributes/d?id=MatchingAttribute&type=ConsumptionSchedule&retURL=%2F_ui%2Fplatform%2Fschema%2Fui%2Fschemabuilder%2FSchemaBuilderUi&setupid=ConsumptionScheduleFields",
            "rel":"",
            "id":"MatchingAttribute",
            "l":"Matching Attribute",
            "cust":false,
            "n":"MatchingAttribute"
        },
        "NumberOfRates":{
            "dt":"1",
            "eid":"ConsumptionSchedule",
            "r":false,
            "t":"Roll-Up Summary (COUNT Consumption Rate) ",
            "d":[

            ],
            "u":"/_ui/common/config/field/StandardFieldAttributes/d?id=NumberOfRates&type=ConsumptionSchedule&retURL=%2F_ui%2Fplatform%2Fschema%2Fui%2Fschemabuilder%2FSchemaBuilderUi&setupid=ConsumptionScheduleFields",
            "rel":"",
            "id":"NumberOfRates",
            "l":"Number of Consumption Rates",
            "cust":false,
            "n":"NumberOfRates"
        },
        "RatingMethod":{
            "dt":"e",
            "eid":"ConsumptionSchedule",
            "r":true,
            "t":"Picklist() ",
            "d":[

            ],
            "u":"/_ui/common/config/field/StandardFieldAttributes/d?id=RatingMethod&type=ConsumptionSchedule&retURL=%2F_ui%2Fplatform%2Fschema%2Fui%2Fschemabuilder%2FSchemaBuilderUi&setupid=ConsumptionScheduleFields",
            "rel":"lu",
            "id":"RatingMethod",
            "l":"Rating Method",
            "cust":false,
            "n":"RatingMethod"
        },
        "Type":{
            "dt":"e",
            "eid":"ConsumptionSchedule",
            "r":true,
            "t":"Picklist() ",
            "d":[

            ],
            "u":"/_ui/common/config/field/StandardFieldAttributes/d?id=Type&type=ConsumptionSchedule&retURL=%2F_ui%2Fplatform%2Fschema%2Fui%2Fschemabuilder%2FSchemaBuilderUi&setupid=ConsumptionScheduleFields",
            "rel":"lu",
            "id":"Type",
            "l":"Type",
            "cust":false,
            "n":"Type"
        },
        "UnitOfMeasure":{
            "dt":"L",
            "eid":"ConsumptionSchedule",
            "r":false,
            "t":"Picklist ",
            "d":[

            ],
            "u":"/_ui/common/config/field/StandardFieldAttributes/d?id=UnitOfMeasure&type=ConsumptionSchedule&retURL=%2F_ui%2Fplatform%2Fschema%2Fui%2Fschemabuilder%2FSchemaBuilderUi&setupid=ConsumptionScheduleFields",
            "rel":"",
            "id":"UnitOfMeasure",
            "l":"Unit of Measure",
            "cust":false,
            "n":"UnitOfMeasure"
        }
        },
        "sblitemid":"0GI6g000001tfp0",
        "id":"ConsumptionSchedule",
        "to":10488.0,
        "cust":false
    }

}

My question is: Can this object be retrieved from the Rest/Soap Api directly? If not, what is the best way to generate the Schema with minimum Api calls and no modifications to the org (no custom Apex code, extension etc.).

Answer

Composite REST resource would have been our suggestion but it has a limit of 25 sub-requests, same as Batch. A better out of the box solution is describeSObjects() in SOAP API:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:partner.soap.sforce.com">
   <soapenv:Header>
    ...
   </soapenv:Header>
   <soapenv:Body>
      <urn:describeSObjects>
         <urn:sObjectType>Contact</urn:sObjectType>
         <urn:sObjectType>Account</urn:sObjectType>
      </urn:describeSObjects>
   </soapenv:Body>
</soapenv:Envelope>

Response:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Header>
      ...
   </soapenv:Header>
   <soapenv:Body>
      <describeSObjectsResponse>
         <result>
            ...
            <label>Contact</label>
            <labelPlural>Contacts</labelPlural>
         </result>
         <result>
            ...
            <label>Account</label>
            <labelPlural>Accounts</labelPlural>
         </result>

      </describeSObjectsResponse>
   </soapenv:Body>
</soapenv:Envelope>

The describeSObjects() call is limited to a maximum of 100 objects returned, a 4x improvement over the Batch option. To get the list of objects, call describeGlobal(). You will want to use the Partner WSDL as your SOAP interface.

Attribution
Source : Link , Question Author : Kartik Soneji , Answer Author : identigral

Leave a Comment