+ Start a Discussion

Apex code



I am New To  apex coding.

Can any body told me how to get the record count of  an object (accounts)  using Apex coding .

Its bit urgent.



Please Please suggest any answer


Simply use.... select count(ID) from .....


Hi ,


Thanks can u explan little.


use aggregateResult.


aggregateResult ar = [select COUNT(name) num  from Account ];


integer count = (Integer) ar['num'];

Imran MohammedImran Mohammed

Integer countVal = [select count() from ObjectName];

ex: [select count() from Account];

The count function will fetch the number of records in that object.




so this select will return only the integer and not the object itself as SOQL use to do???

Imran MohammedImran Mohammed

Yes, it will return just an integer rather than the object list.


But ,If U want object list then how to do it.


use aggregateResult if you want the sum ,count,etc...and the other fields too....


But aggregate result will not return the object type...you will have to cast each field result to their respective type. Correct me if i'm wrong


global class SampleTest{ private void totalListySample() { try { AggregateResult ar = null; String groupByQuery = [select Name FROM Mileage__c GROUP BY Name]; QueryResult qr = binding.query(groupByQuery); if (qr.getSize() > 0){ System.debug('Query returned " + qr.getRecords().length + results.'); for (Mileage__c s:qr.getRecords()) { ar = (AggregateResult)s; // retrieve each item in the select list // using get_any() on the AggregateResult object // for an Apache Axis client with the enterprise WSDL. // If this was the partner WSDL, you would use getField() // on SObject instead. System.debug('Name: + ar.get_any()[0].getValue()'); //System.debug('Max Amount: + ar.get_any()[1].getValue()'); // System.debug('Min Amount: + ar.get_any()[2].getValue() '); } } else { System.debug('No results found.'); } System.debug('Query successfully executed.'); } catch (Exception ex) { System.debug('Failed to execute query successfully' + 'Error message', ex.getMessage()); } } }





ITRY TO DO LIKE THIS but its show error on line 8 colmn6

plz help me on this

Imran MohammedImran Mohammed

Yes, type cast should be done.

To get object list, do a SOQL  query with the fields you need and then invoke the size method on the List returned.

In this way you will come to know the Record count as well as the Object list.

But this will return upto 1000 records.

If you use SOQL for loop, you can fetch upto 10000 records.


If the object has more than 10000 records, i will suggest you to have 2 queries

1. To count number of records which i posted earlier

2. To get object list using SOQL for loop which will fetch upto 10000 records.




your code is group in a mess..i can't find line 8..can you be more specific also about the error you obtained   .... :-)


QueryResult qr = binding.query(groupByQuery);




It show foolowing error...


ErrorError: Compile Error: Invalid type: QueryResult at line 8 column 6




why are you using a queryResul???? what do you exactly want to achieve ?? :-)