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
MunmunMunmun 

Apex code

Hi,

 

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

FromCRMWonderlandFromCRMWonderland

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

MunmunMunmun

Hi ,

 

Thanks can u explan little.

vanessenvanessen

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.

 

 

vanessenvanessen

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.

MunmunMunmun

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

vanessenvanessen

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

vanessenvanessen

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

MunmunMunmun

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.

 

 

vanessenvanessen

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

MunmunMunmun

QueryResult qr = binding.query(groupByQuery);

 

 

 

It show foolowing error...

 

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

 

 

vanessenvanessen

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