function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
OfraOfra 

How to find an object type's id in Apex or SOQL?

Hi, 
I'm writing some Apex code and I need to query a custom object's ID as in the URL below:
https://telfed--pras.lightning.force.com/lightning/setup/ObjectManager/01I3q0000026FS5/Details/view
What I have is the ID of a record of that type, and I know how to get the type's name, for example by doing: 
recordId.getSobjectType().getDescribe().getName()
However, I can't seem to find a function that will get me the ID of type. 

I want the ID because I want to query the correct Custom Metadata Record that corresponds to this type via an Entity Definition Relationship field. I can see in the Query Editor Results that the value stored in the record is not the type's name, but the type's ID. So I would like to get the ID so that I can construct the correct where clause. 
SELECT FIELDS(ALL) FROM Incoming_Contact_Data__mdt LIMIT 200
User-added imageAny help would be appreciated!
 
Best Answer chosen by Ofra
OfraOfra
I found it :)
I can query by the name by using: 
SELECT ... FROM Incoming_Contact_Data__mdt
WHERE Object_Name__r.QualifiedApiName = <Name>

 

All Answers

CharuDuttCharuDutt
Hii Ofra
Try Below Line
Id myRecordId = 'a0V9000000KWdz3EAD';
String sObjName = myRecordId.getSObjectType().getDescribe().getName();
System.debug('sObjName >>>  '+ sObjName );
Please Mark It As Best Answer If It Helps
Thank You!
OfraOfra
I found it :)
I can query by the name by using: 
SELECT ... FROM Incoming_Contact_Data__mdt
WHERE Object_Name__r.QualifiedApiName = <Name>

 
This was selected as the best answer