+ Start a Discussion
fadwa mangougfadwa mangoug 

how to get the id of an object using its name ? ( object referenced by a lookup)

Hey there !
I'm having trouble with a method and can use some help plz !
So here's what I'm trying to do :
My method should give me the ID of an object when i use its name, for now i have this but it's not working, I have an error : " Method does not exist or incorrect signature: [Schema.DescribeSObjectResult].get()"
public integer TargetID() {
        Schema.SObjectType sobjectType = Name.getSObjectType();
        Integer sobjectId = sobjectType.getDescribe().get();
  SObject record = Database.query('Select Id From ' + sobjectId + ' Where Name = :Name');

The problem is with the get(), I know it works with names but not with ID's.

Thanks in advance for the help :) !
Hi Fadwa,

You cannot get the Id of the CustomObject using normal SOQL. You will have to use Tooling api to achieve this in salesforce. You can use below mentioned code:
HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');


Http h = new Http();
HttpResponse res = h.send(req);
Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
List<Object> lstObj = (List<Object>)m.get('records');
Map<String,Object> mk = (Map<String,Object>)lstObj[0];

Make sure your salesforce instance must be whitelisted in remote site settings in your org.