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
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 :) !
Pankaj_GanwaniPankaj_Ganwani
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');

req.setEndpoint('https://ap2.salesforce.com/services/data/v28.0/tooling/query/?q=select+Id+from+CustomObject+where+DeveloperName=\'Object_1\'');
req.setMethod('GET');

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];
system.debug('====='+mk.get('Id'));

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