3

I want to use the Java SOAP API to execute a query like the following:

connection.query("SELECT COUNT(Id) FROM Contact WHERE . . .");

However I get back a single SObject:

[SObject  fieldsToNull='{[0]}'
 Id='null'
]

Is there a correct way to do this without querying for all the records and using the size of the .getRecords() array?

Luckily in my case there are less than 200 records as a result of this query so it wouldn't be the end of the world...

Edit -

It has been suggested to use aliasing combined with a "GROUP BY" - however this results in multiple null SObjects being returned instead of just an integer count.

9
  • 1
    As per the docs, this should work: String soqlQuery = "SELECT FirstName, LastName FROM Contact"; try { QueryResult qr = connection.query(soqlQuery); From - developer.salesforce.com/docs/atlas.en-us.api.meta/api/… Jun 14, 2016 at 16:53
  • 1
    The syntax is different. You ought to try to pass a variable into connection.query method. For example, try this: String soqlQuery = "SELECT COUNT(Id) FROM Contact WHERE..."); try{ QueryResult qr = connection.query(soqlQuery); Jun 14, 2016 at 16:58
  • 2
    Have you tried aliasing the count field? [Select Count(Id) NrRecs FROM....]? Otherwise the field name will be null. And, by the way, you will only get one record from that query unless you have a "Group By" clause. Jun 14, 2016 at 17:09
  • 1
    What if you remove Id from the parens? That is, COUNT() instead of COUNT(Id)?
    – Adrian Larson
    Jun 14, 2016 at 17:14
  • 1
    @AdrianLarson - this results in a MalformedQuery error.
    – jimpudar
    Jun 14, 2016 at 17:42

2 Answers 2

1

Imagine the following query:

connection.query("SELECT CreatedById, Count(Id) numOfLeads FROM Lead GROUP BY CreatedById");

The QueryResult will contain records of type AggregateResult extends SObject which is basically a wrapper for each row in the response. You must provide the name for each needed field, the count result here is aliased as "numOfLeads":

String userId = (String) myRecord.getField("CreatedById"); int createdLeads = (int) myRecord.getField("numOfLeads");

1
  • You can alias the field to any name you want (e.g. SELECT COUNT(Id) recordCount FROM Lead and then aggregate.get('recordCount')).
    – Adrian Larson
    Apr 11, 2018 at 18:52
0

Yes, you can get the count of SOQL records but you have to apply LIMIT of 50000 as Salesforce has a SOQL query rows limit. This is referenced in this documentation.

enter image description here

You can you also use SOAP API on visualforce page:

enter image description here

Example of AggregateResult: Count() using in SOQL via SOAP API:

<apex:page >
    <head>
        <script type="text/javascript">
            var __sfdcSessionId = '{!GETSESSIONID()}';
        </script>
        <script src="../../soap/ajax/30.0/connection.js" type="text/javascript"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
    </head>
    <script>

        var query = "SELECT Count(id) cnts FROM case WHERE Status='new' ";
        var result = sforce.connection.query(query);
        console.log(result);
        document.write('Getting AggregateResult records:<br/>');
        var records = result.getArray("records");
        if(records.length>0)
        document.write('Aggregate Count: ' + records[0]['cnts']+'<br/>');

        var query1 = "SELECT Count() FROM case WHERE Status='new' ";
        var result1 = sforce.connection.query(query1);
        console.log(result1);
        document.write('======================<br/>');
        document.write('Getting query result<br/>');
        var records1 = result1.getArray("records");// no records are found because query will return count
        var counts= result1.get("size");
        document.write('Count of records are:' + counts); 

    </script>
</apex:page>
4
  • 2
    I think the OP is concerned about how to get values back over the SOAP API and not directly in Apex code.
    – Keith C
    Jun 14, 2016 at 17:29
  • Modified my Answer according SOAP API uses. Jun 14, 2016 at 17:59
  • Please see @SebastianKessel's comment above - this certainly does not work from the SOAP API using Java unless there is a "GROUP BY" statement in the query, and even then multiple records are returned instead of just a single integer value like I want.
    – jimpudar
    Jun 15, 2016 at 11:54
  • Sorry for late reply, Yes you can use Count() in SOQL via SOAP API, the previous example we related to AggregateResult but I update my answer having Both Example of AggregateResult and use of Count() In SOQL via SOAP API, so you can also use both is SOQL in another form of SOAP API like that. Jun 20, 2016 at 15:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .