Wednesday, July 5, 2023

FieldDefinition - How to get list of all fields present in any Object using Tooling API in Salesforce

We can utilize Tooling API to get information of all fields present in a object. You can put different filters like to get all fields or specific fields like auto number fields, ExternalID fields etc.

Below is apex class which contains method to get all fields. Remember that callout uses Named Credential in order to do handshake with SFDC org.

This method contains 3 parameters:

  • selOrgNCName : Name of Named Credential created to connect to SFDC org
  • objAPIName : Specify the object API name in order to get list of all fields
  • namespacePrefix : Specify namespacePrefix if you want to retrieve fields related to any installed packages. If you want to get list of fields created in SFDC org, then specify blank or NULL.
Note:
  • I have added filter not to return auto number fields. If you want to get those fields details then remove it from query string.
  • If you want to query only External Id fields then specify filter as DataType+Like+\'%25(External+ID)%25\'';


Hope this will help!!



Tuesday, July 4, 2023

EntityDefinition - How to get list of all objects present in any SFDC org using tooling API

Salesforce provide Metadata API through which we can metadata information present in any SFDC org. Metadata API is SOAP based which involves consuming WSDL and then calling methods provided in WSDL.

Salesforce has introduced Tooling API which is REST based and can be used to get metadata information from any salesforce org. Tooling API is very easy to implement.

EntityDefinition provides information about standard and custom metadata. Before invoking any Tooling API endpoint, we need to do handshake with SFDC org. You can utilise Oauth 2.0 (username-password flow) or create "Named Credential" for SFDC org from which you want to fetch information.


Below is Apex class which contains method through which we can get all object details:

"findAllObjects" Method contains 3 parameters: 

  • selOrgNCName : Name of Named Credential created to connect to SFDC org
  • namespacePrefix : Specify namespacePrefix if you want to retrieve objects related to any installed packages. If you want to get list of objects created in SFDC org, then specify blank or NULL.
  • objName : specify search string if you want to get list of specific objects that contain that string. Pass blank or NULL if you want to retrieve all objects.


Hope this will help!!