+ Start a Discussion

How to find no of records in a object using lookup relations?

How to find no of records in  a object using lookup relations?

Yoganand GadekarYoganand Gadekar



You can store the id of the look up in a ID varaibale and then get the type of the object

, Once you have type of the object you can get the count


ID objectId = your LOOK up id;

string ObjectString=string.valueof(objectId .getSObjectType());

String QueryString = 'select count() from' + ObjectString;

Integer Recordcount = Database.query(QueryString);






just open Developer Console and rund this query


Integer iLookupCount = [select count() from <childObject> where <lookup_Field> != null];

system.debug(' # of lookup relation records are :  '+iLookupCount);


Hello Pawan,


If an object is having lookup relationship then each record will be having the lookup field. Now if the lookup field is blank for any record then you can query over the object with the condition where test_lookup_field not equals to blank.