+ Start a Discussion
srikanth nsrikanth n 

How to get a count records from object?

How to get a count  records from object? ex:- if i  have 1 lakh records in object 

Hey Srikanth,

1) Open the Developer Console, In Query Editor & execute simple query as "SELECT id FROM Acccount", you will get the number of record in Query Results.

2) If you want to use this count in Code, you can write query as 
Integer count = SELECT count() FROM ObjectName;
Check count you will get number of records from object,
Happy coding!!

srikanth nsrikanth n
Hi amit, If use that query with more than 50k records then governor limits encounter. then how avoid that.
If this is for a VF page or in a webservice, you can use the @ReadOnly (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_ReadOnly.htm) annotation.  This allows you to query up to 1 million rows, but as the name implies, no DML can be performed on those objects.

If its not those use cases, then I have seen people use custom objects or custom settings to hold the counts of certain objects and then run Batch (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm) jobs every hour, day, week, whatever to update those counts.
Srujana D 7Srujana D 7
Hi Srikanth,
You can use Batch Apex class to get the all the records. you have to set batch size.
Harness the power of visual flows post winter 18. Record counts can now be obtained on the fly:

1. Perform a fast lookup with your required filters

2. Assign the record count to a sObject collections variable. (in screenshot: nofCases)

3. Use a Assignment step, to assign the variable to a number variable. (Point to note: the assignment variable should be of type number only to make use of equals count operator. In the screenshot, this is called testCount)

4. Voila! the testCount variable is a count of the records you are searching for. Play with it.

If this helped anyone here, dont forget to hit the thumbs up.


Rahul Tiwari 85Rahul Tiwari 85
For counting records go to developer conole and in query editor write select count() from object
it will give you the total number of records of object
hope this helps you
Jaya Kumar 17Jaya Kumar 17
@rahul tiwari 85 Have you understood the question in first place?! Can you query more than 100000 records in soql? try and respond!