How to use bulkRetrieve?

Here I am going to explain about bulkRetrieve – retrieving components in bulk (retrieving large number of components) for single metadata type. We can use this to retrieve all custom objects, Apex Classes, Apex Pages, Custom Fields, Validations Rules and other metadata types without specifying api names of components.

Here is the syntax to retrieve components in bulk:

<target name="bulkRetrieve">

  <sf:bulkRetrieve  username="${username}" password="${password}" serverurl="${serverurl}" batchSize = "100"   metadataType="<Specify the metadata you want to retrieve>"    retrieveTarget="retrieveUnpackaged"/>

</target>

Below are the list of parameters can be used for <sf:bulkRetrieve> target:

Username: Salesforce user name for to login. Given user name must have “modify all data” permission. This attribute is required if sessionID is not specified.
password: Password to login. Add 25 digit security token at the end of password.
sessionID: This is required if username and password are not specified. This is the ID of active salesforce session.
Serverurl: specify salesforce server URL, specify login.salesforce.com if you are login to production environment. Specify test.salesforce.com if you are login to sandbox environment. Default value is login.salesforce.com, if the value is blank.
retrieveTarget: Specify the root folder structure which the metadata files are retrieved.
metadataType: Specify the name of the metadata type to be retrieved.
batchSize: Number of items to retrieve while doing multi part retrieve. Default size is 10.
apiVersion: This is optional, this specifies Metadata API version.
maxPoll: This also optional, this specifies number of times to poll the server for the results of the retrieve request.
Unzip & containsFolder are two other optional parameters which can be used for <sf:bulkRetrieve> target.

Specify below target on your build.xml file to retrieve all Apex Classes in your org.

<target name="bulkRetrieveApexClass">
<sf:bulkRetrieve username="${username}" password="${password}" serverurl="${serverurl}" batchSize = "1000" metadataType="ApexClass" retrieveTarget="retrieveUnpackaged"/>
</target>

Execute “bulkRetrieveApexClass” command to retrieve all classes. Specify below target on your build.xml file to retrieve all Apex Triggers in your org.

<target name="bulkRetrieveApexClass">
<sf:bulkRetrieve username="${username}" password="${password}" serverurl="${serverurl}" batchSize = "1000" metadataType="ApexTrigger" retrieveTarget="retrieveUnpackaged"/>
</target>

Execute “bulkRetrieveApexTrigger” command to retrieve all triggers. Specify below target on your build.xml file to retrieve all custom objects in your org.

<target name="bulkRetrieveApexClass">
<sf:bulkRetrieve username="${username}" password="${password}" serverurl="${serverurl}" batchSize = "1000" metadataType="CustomObject" retrieveTarget="retrieveUnpackaged"/>
</target>

Execute “bulkRetrieveApexTrigger” command to retrieve all custom objects.