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.
- 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\'';