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
MilanMilan 

Understanding the implementation of Batch size in Query

Hello,
 
I am firing a Query to retrieve some information. Its a simple test program to query contacts within Salesforce.
 
I am setting the Batch Size to 3 (just to understand implementation of batch size in Salesforce and to test that its inline with my understanding)
 
Code:
QueryResult qr = null;
QueryOptions qo = new QueryOptions();
qo.setBatchSize(new Integer(3));
stub.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(), 
       "QueryOptions", qo);

 
The request generated is as follows, which is inline with my expectations :
Code:
POST /services/Soap/u/9.0 HTTP/1.0
Content-Type: text/xml; charset=utf-8
Accept: application/soap+xml, application/dime, multipart/related, text/*
User-Agent: Axis/1.3
Host: na4-api.salesforce.com:7002
Cache-Control: no-cache
Pragma: no-cache
SOAPAction: ""
Content-Length: 929

<—xml version="1.0" encoding="UTF-8"–>
   <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <soapenv:Header>
         <ns1:SessionHeader soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns:ns1="urn:partner.soap.sforce.com">
            <ns1:sessionId>X.MdpH69QhJuMTs_qEfCdx6CSsJX4ZRKqCwcDq.pR5m2hGyVaHTcsZgDQeW2z0Ihc8MDAV3oO2w_73lxBWwnG.PffGIt.5WPRX.tUakNGDXyQV6RAJI=</ns1:sessionId>
         </ns1:SessionHeader>
         <ns2:QueryOptions soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns:ns2="urn:partner.soap.sforce.com">
            <ns2:batchSize>3</ns2:batchSize>
         </ns2:QueryOptions>
      </soapenv:Header>
      <soapenv:Body>
         <query xmlns="urn:partner.soap.sforce.com">
            <queryString>select email, FirstName, LastName, Id from Contact</queryString>
         </query>
      </soapenv:Body>
   </soapenv:Envelope>

 
The response recieved is as follows:
Code:
Server: 
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Content-Length: 4225
Date: Thu, 03 May 2007 17:53:44 GMT

<—xml version="1.0" encoding="UTF-8"–>
   <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sf="urn:sobject.partner.soap.sforce.com">
      <soapenv:Body>
         <queryResponse>
            <result xsi:type="QueryResult">
               <done>true</done>
               <queryLocator xsi:nil="true"/>
               <records xsi:type="sf:sObject">
                  <sf:type>Contact</sf:type>
                  <sf:Id>0036000000OsYskAAF</sf:Id>
                  <sf:Email>j.davis@expressl&amp;t.net</sf:Email>
                  <sf:FirstName>Josh</sf:FirstName>
                  <sf:LastName>Davis</sf:LastName>
                  <sf:Id>0036000000OsYskAAF</sf:Id>
               </records>
               <records xsi:type="sf:sObject">
                  <sf:type>Contact</sf:type>
                  <sf:Id>0036000000OsYslAAF</sf:Id>
                  <sf:Email>jane_gray@uoa.edu</sf:Email>
                  <sf:FirstName>Jane</sf:FirstName>
                  <sf:LastName>Grey</sf:LastName>
                  <sf:Id>0036000000OsYslAAF</sf:Id>
               </records>
               <records xsi:type="sf:sObject">
                  <sf:type>Contact</sf:type>
                  <sf:Id>0036000000OsYsnAAF</sf:Id>
                  <sf:Email>agreen@uog.com</sf:Email>
                  <sf:FirstName>Avi</sf:FirstName>
                  <sf:LastName>Green</sf:LastName>
                  <sf:Id>0036000000OsYsnAAF</sf:Id>
               </records>
               <records xsi:type="sf:sObject">
                  <sf:type>Contact</sf:type>
                  <sf:Id>0036000000OsYsoAAF</sf:Id>
                  <sf:Email>spavlova@uog.com</sf:Email>
                  <sf:FirstName>Stella</sf:FirstName>
                  <sf:LastName>Pavlova</sf:LastName>
                  <sf:Id>0036000000OsYsoAAF</sf:Id>
               </records>
               <records xsi:type="sf:sObject">
                  <sf:type>Contact</sf:type>
                  <sf:Id>0036000000OsYspAAF</sf:Id>
                  <sf:Email>lboyle@uog.com</sf:Email>
                  <sf:FirstName>Lauren</sf:FirstName>
                  <sf:LastName>Boyle</sf:LastName>
                  <sf:Id>0036000000OsYspAAF</sf:Id>
               </records>
               <records xsi:type="sf:sObject">
                  <sf:type>Contact</sf:type>
                  <sf:Id>0036000000OsYsqAAF</sf:Id>
                  <sf:Email>ajames@uog.com</sf:Email>
                  <sf:FirstName>Ashley</sf:FirstName>
                  <sf:LastName>James</sf:LastName>
                  <sf:Id>0036000000OsYsqAAF</sf:Id>
               </records>
               <records xsi:type="sf:sObject">
                  <sf:type>Contact</sf:type>
                  <sf:Id>0036000000OsYsrAAF</sf:Id>
                  <sf:Email>tripley@uog.com</sf:Email>
                  <sf:FirstName>Tom</sf:FirstName>
                  <sf:LastName>Ripley</sf:LastName>
                  <sf:Id>0036000000OsYsrAAF</sf:Id>
               </records>
               <records xsi:type="sf:sObject">
                  <sf:type>Contact</sf:type>
                  <sf:Id>0036000000OsYsYAAV</sf:Id>
                  <sf:Email>ldcruz@uog.com</sf:Email>
                  <sf:FirstName>Liz</sf:FirstName>
                  <sf:LastName>D&apos;Cruz</sf:LastName>
                  <sf:Id>0036000000OsYsYAAV</sf:Id>
               </records>
               <records xsi:type="sf:sObject">
                  <sf:type>Contact</sf:type>
                  <sf:Id>0036000000OsYsZAAV</sf:Id>
                  <sf:Email>efrank@genepoint.com</sf:Email>
                  <sf:FirstName>Edna</sf:FirstName>
                  <sf:LastName>Frank</sf:LastName>
                  <sf:Id>0036000000OsYsZAAV</sf:Id>
               </records>
               <records xsi:type="sf:sObject">
                  <sf:type>Contact</sf:type>
                  <sf:Id>0036000000OsYscAAF</sf:Id>
                  <sf:Email>rose@edge.com</sf:Email>
                  <sf:FirstName>Rose</sf:FirstName>
                  <sf:LastName>Gonzalez</sf:LastName>
                  <sf:Id>0036000000OsYscAAF</sf:Id>
               </records>
               <records xsi:type="sf:sObject">
                  <sf:type>Contact</sf:type>
                  <sf:Id>0036000000OsYsdAAF</sf:Id>
                  <sf:Email>sean@edge.com</sf:Email>
                  <sf:FirstName>Sean</sf:FirstName>
                  <sf:LastName>Forbes</sf:LastName>
                  <sf:Id>0036000000OsYsdAAF</sf:Id>
               </records>
               <records xsi:type="sf:sObject">
                  <sf:type>Contact</sf:type>
                  <sf:Id>0036000000OsYseAAF</sf:Id>
                  <sf:Email>jrogers@burlington.com</sf:Email>
                  <sf:FirstName>Jack</sf:FirstName>
                  <sf:LastName>Rogers</sf:LastName>
                  <sf:Id>0036000000OsYseAAF</sf:Id>
               </records>
               <records xsi:type="sf:sObject">
                  <sf:type>Contact</sf:type>
                  <sf:Id>0036000000OsYshAAF</sf:Id>
                  <sf:Email>barr_tim@grandhotels.com</sf:Email>
                  <sf:FirstName>Tim</sf:FirstName>
                  <sf:LastName>Barr</sf:LastName>
                  <sf:Id>0036000000OsYshAAF</sf:Id>
               </records>
               <records xsi:type="sf:sObject">
                  <sf:type>Contact</sf:type>
                  <sf:Id>0036000000OsYsiAAF</sf:Id>
                  <sf:Email>bond_john@grandhotels.com</sf:Email>
                  <sf:FirstName>John</sf:FirstName>
                  <sf:LastName>Bond</sf:LastName>
                  <sf:Id>0036000000OsYsiAAF</sf:Id>
               </records>
               <records xsi:type="sf:sObject">
                  <sf:type>Contact</sf:type>
                  <sf:Id>0036000000OsYsjAAF</sf:Id>
                  <sf:Email>b.levy@expressl&amp;t.net</sf:Email>
                  <sf:FirstName>Babara</sf:FirstName>
                  <sf:LastName>Levy</sf:LastName>
                  <sf:Id>0036000000OsYsjAAF</sf:Id>
               </records>
               <records xsi:type="sf:sObject">
                  <sf:type>Contact</sf:type>
                  <sf:Id>0036000000OtG86AAF</sf:Id>
                  <sf:Email xsi:nil="true"/>
                  <sf:FirstName>Joe</sf:FirstName>
                  <sf:LastName>Blow</sf:LastName>
                  <sf:Id>0036000000OtG86AAF</sf:Id>
               </records>
               <size>16</size>
            </result>
         </queryResponse>
      </soapenv:Body>
   </soapenv:Envelope>

 
I am not sure why did i recieve ALL 16 contatcs at one go... I was expecting ONLY three contacts to be returned as my batch size is 3. Should it not have been so ? I read somewhere about default to 250 or 500 or so but if I am explicitely setting it to some value (3 in this case), why does it return me everything ?
 
My guess is that something is wrong in my interpretation of batchSize and any insight will be appreciated.
 
Thanks and regards,
Milan
SuperfellSuperfell
The docs are always a good place to start. QueryOptions header which says "Batches that are larger or smaller than the specified size may be used, in order to maximize performance." and "the minimum is 200"
MilanMilan

I figured that from reading some other posts on this topic but I really could not believe it :smileywink:  .. But thanks for confirming it!

Cheers,

Milan