Create Bulk Read job (Bulk Export)
Purpose
To create a bulk read job to export records.
Endpoints
Request Details
Request URL
{api-domain}/crm/bulk/{version}/read
Header
Authorization: Zoho-oauthtoken d92d4xxxxxxxxxxxxx15f52
Scope
scope=ZohoCRM.bulk.read
(and)
scope=ZohoCRM.modules.{module_name}.{operation_type}
Possible module names
leads, accounts, contacts, deals, campaigns, tasks, cases, events, calls, solutions, products, vendors, price books, quotes, sales orders, purchase orders, invoices, and custom
Possible operation types
ALL - Full access to the record
READ - Get bulk read job
Sample Request
Copiedcurl "https://www.zohoapis.com/crm/bulk/v2.1/read"
-X POST
-H "Authorization: Zoho-oauthtoken 1000.8cb99dxxxxxxxxxxxxx9be93.9b8xxxxxxxxxxxxxxxf"
-H "Content-Type: application/json"
-d "@inputData.json"
Copied//Get instance of BulkReadOperations Class
$bulkReadOperations = new BulkReadOperations();
//Get instance of RequestWrapper Class that will contain the request body
$requestWrapper = new RequestWrapper();
//Get instance of CallBack Class
$callback = new CallBack();
// To set valid callback URL.
$callback->setUrl("https://www.example.com/callback");
//To set the HTTP method of the callback URL. The allowed value is post.
$callback->setMethod(new Choice("post"));
//The Bulkread Job's details is posted to this URL on successful completion / failure of job.
$requestWrapper->setCallback($callback);
//Get instance of Query Class
$query = new Query();
$module = new Module();
$module->setAPIName($moduleAPIName);
//Specifies the API Name of the module to be read.
$query->setModule($module);
//Specifies the unique ID of the custom view whose records you want to export.
$query->setCvid("34770610087501");
// List of Field API Names
$fieldAPINames = array();
array_push($fieldAPINames, "Last_Name");
//Specifies the API Name of the fields to be fetched.
$query->setFields($fieldAPINames);
//To set page value, By default value is 1.
$query->setPage(1);
//Get instance of Criteria Class
$criteria = new Criteria();
$criteria->setGroupOperator(new Choice("or"));
$criteriaList = array();
$group11 = new Criteria();
$group11->setGroupOperator(new Choice("and"));
$groupList11 = array();
$group111 = new Criteria();
$field = new Field();
$field->setAPIName("Last_Name");
$group111->setField($field);
$group111->setComparator(new Choice("equal"));
$group111->setValue("TestPHPSDK");
array_push($groupList11, $group111);
$group112 = new Criteria();
$field = new Field();
$field->setAPIName("Owner");
$group112->setField($field);
$group112->setComparator(new Choice("in"));
$owner = array("34770610173021");
$group112->setValue($owner);
array_push($groupList11, $group112);
$group11->setGroup($groupList11);
array_push($criteriaList, $group11);
$group12 = new Criteria();
$group12->setGroupOperator(new Choice("or"));
$groupList12 = array();
$group121 = new Criteria();
$field = new Field();
$field->setAPIName("Company");
$group121->setField($field);
$group121->setComparator(new Choice("equal"));
$group121->setValue("KK");
array_push($groupList12, $group121);
$group122 = new Criteria();
$field = new Field();
$field->setAPIName("Created_Time");
// To set API name of a field.
$group122->setField($field);
// To set comparator(eg: equal, greater_than.).
$group122->setComparator(new Choice("between"));
$createdTime = array(date_create("2020-07-15T17:58:47+05:30")->setTimezone(new \DateTimeZone(date_default_timezone_get())), date_create("2020-10-15T17:58:47+05:30")->setTimezone(new \DateTimeZone(date_default_timezone_get())));
// To set the value to be compare.
$group122->setValue($createdTime);
array_push($groupList12, $group122);
$group12->setGroup($groupList12);
array_push($criteriaList, $group12);
$criteria->setGroup($criteriaList);
//To filter the records to be exported.
$query->setCriteria($criteria);
//To set query JSON object.
$requestWrapper->setQuery($query);
//Specify the value for this key as "ics" to export all records in the Events module as an ICS file.
// $requestWrapper->setFileType(new Choice("ics"));
//Call createBulkReadJob method that takes RequestWrapper instance as parameter
$response = $bulkReadOperations->createBulkReadJob($requestWrapper);
Copied<?php
class CreateBulkReadjob
{
public function execute(){
$curl_pointer = curl_init();
$curl_options = array();
$curl_options[CURLOPT_URL] = "https://www.zohoapis.com/crm/bulk/v2.1/read";
$curl_options[CURLOPT_RETURNTRANSFER] = true;
$curl_options[CURLOPT_HEADER] = 1;
$curl_options[CURLOPT_CUSTOMREQUEST] = "POST";
$requestBody=array();
$query=array();
$query["cvid"]="4150868000000087501";
$criteria=array();
$criteria["group_operator"]="and";
$group=array();
$groupObject1=array();
$groupObject1["comparator"]="equal";
$field=array();
$field["api_name"]="Last_Name";
$groupObject1["field"]=$field;
$groupObject1["value"]="Patricia";
array_push($group,$groupObject1);
$groupObject2=array();
$groupObject2["comparator"]="equal";
$field=array();
$field["api_name"]="Email";
$groupObject2["field"]=$field;
$groupObject2["value"]="patricia.b@zylker.com";
array_push($group,$groupObject2);
$criteria["group"]=$group;
$query["criteria"]=$criteria;
$module=array();
$module["api_name"]="Leads";
$query["module"]=$module;
$fields=array();
array_push($fields,"Last_Name");
array_push($fields,"Owner");
array_push($fields,"Owner.last_name");
array_push($fields,"$converted");
array_push($fields,"Lead_Source");
array_push($fields,"Lead_Status");
array_push($fields,"Company");
array_push($fields,"Email");
array_push($fields,"Mobile");
array_push($fields,"Created_Time");
$query["fields"]=$fields;
$requestBody["query"]=$query;
$callback=array();
$callback["method"]="post";
$callback["url"]="https://www.example.com/callback";
$requestBody["callback"]=$callback;
$headersArray = array();
$headersArray[] = "Authorization". ":" . "Zoho-oauthtoken " . "1000.30f3a589XXXXXXXXXXXXXXXXXXX4077.dc5XXXXXXXXXXXXXXXXXXXee9e7c171c";
$headersArray[] = "Content-Type".":"."application/json";
$curl_options[CURLOPT_HTTPHEADER]=$headersArray;
curl_setopt_array($curl_pointer, $curl_options);
$result = curl_exec($curl_pointer);
$responseInfo = curl_getinfo($curl_pointer);
curl_close($curl_pointer);
list ($headers, $content) = explode("\r\n\r\n", $result, 2);
if(strpos($headers," 100 Continue")!==false){
list( $headers, $content) = explode( "\r\n\r\n", $content , 2);
}
$headerArray = (explode("\r\n", $headers, 50));
$headerMap = array();
foreach ($headerArray as $key) {
if (strpos($key, ":") != false) {
$firstHalf = substr($key, 0, strpos($key, ":"));
$secondHalf = substr($key, strpos($key, ":") + 1);
$headerMap[$firstHalf] = trim($secondHalf);
}
}
$jsonResponse = json_decode($content, true);
if ($jsonResponse == null && $responseInfo['http_code'] != 204) {
list ($headers, $content) = explode("\r\n\r\n", $content, 2);
$jsonResponse = json_decode($content, true);
}
var_dump($headerMap);
var_dump($jsonResponse);
var_dump($responseInfo['http_code']);
}
}
(new CreateBulkReadjob())->execute();
Copiedcvid = '3524033000000087501'
# Get instance of BulkReadOperations Class
bro = BulkRead::BulkReadOperations.new
# Get instance of RequestWrapper Class that will contain the request body
request_wrapper = BulkRead::RequestWrapper.new
# Get instance of CallBack Class
call_back = BulkRead::CallBack.new
# Set valid callback URL
call_back.url = 'https://www.example.com/callback'
# Set the HTTP method of the callback URL. The allowed value is post.
call_back.method = Util::Choice.new('post')
# The Bulk Read Job's details is posted to this URL on successful completion / failure of the job.
request_wrapper.callback = call_back
# Get instance of Query Class
query = BulkRead::Query.new
# Specifies the API Name of the module to be read
query.module = module_api_name
# Specifies the unique ID of the custom view, whose records you want to export.
query.cvid = cvid
# List of field names
field_api_names = ['Last_Name']
# Specifies the API Name of the fields to be fetched
query.fields = field_api_names
# To set page value, By default value is 1.
query.page = 1
# Get instance of Criteria Class
criteria = BulkRead::Criteria.new
# To set API name of a field
criteria.api_name = 'Created_Time'
# To set comparator(eg: equal, greater_than)
criteria.comparator = Util::Choice.new('between')
created_time = ['2020-06-03T17:31:48+05:30', '2020-06-03T17:31:48+05:30']
# To set the value to be compared
criteria.value = created_time
# To filter the records to be exported
query.criteria = criteria
# Set the query object
request_wrapper.query = query
# Specify the value for this key as "ics" to export all records in the Events module as an ICS file.
# request.set_file_type(Util::Choice.new('ics'))
# Call create_bulk_read_job method that takes RequestWrapper instance as parameter
response = bro.create_bulk_read_job(request_wrapper)
Copiedrequire 'net/http'
require 'json'
class CreateBulkReadjob
def execute
url ="https://www.zohoapis.com/crm/bulk/v2.1/read";
url = URI(url)
req = Net::HTTP::Post.new(url.request_uri)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
headers={}
headers["Authorization"]="Zoho-oauthtoken 1000.dfa7XXXXXXXXXXXXXXXXXX84f9665840.c176aeXXXXXXXXXXXX13f3d37a84d"
headers["Content-Type"]="application/json";
headers&.each { |key, value| req.add_field(key, value) }
request_body = {};
callback = {}
callback["url"]="https://www.example.com/callback";
callback["method"]="post";
query = {}
query["module"]="Contacts";
fields = ["Last_Name","Owner","Owner.last_name","Account_Name.Account_Name"]
request_body["callback"] =callback;
query["fields"]=fields;
criteria = {}
criteria["group_operator"]="or";
group = {}
criteria1 = {}
criteria1["api_name"]="Lead_Source";
criteria1["comparator"]="equal";
criteria1["value"]="Advertisement";
criteria2 = {}
criteria2["api_name"]="Owner.last_name";
criteria2["comparator"]="equal";
criteria2["value"]="Boyle";
criteria3 = {}
criteria3["api_name"]="Account_Name.Phone";
criteria3["comparator"]="contains";
criteria3["value"]="5";
group = [criteria1,criteria2,criteria3];
criteria["group"] = group;
query["criteria"] =criteria;
query["page"] =1;
request_body["query"]=query;
request_json = request_body.to_json
req.body = request_json.to_s
response=http.request(req)
status_code = response.code.to_i
headers = response.each_header.to_h
print status_code
print headers
unless response.body.nil?
print response.body
end
end
end
CreateBulkReadjob.new.execute
Request JSON
- callbackJSON Object, optional
The JSON object represents the following details of a bulk read job.
- url(string)-
A valid URL, that should allow HTTP Post method. The Bulk Read Job's details is posted to this URL on successful completion of job or on failure of job. - method(string)-
The HTTP method of the callback url. The supported value is post.
- url(string)-
- queryJSON Object, optional
Represents the JSON object that holds the keys and their value that form the criteria for bulk export. The following are the keys in the JSON object:
- module(JSON object, mandatory)-
Represents the API Name of the module you want to export the records from. For instance, Leads. Specify the module name as "Events" if you want to export the records in the Events module as an ICS file.
- module(JSON object, mandatory)-
- file_type(string, mandatory when you want to export the events as an ICS file)
Specify the value for this key as "ics" to export all records in the Events module as an ICS file.
Sample Input to fetch all the records in a module
Copied{
"callback": {
"url": "https://www.example.com/callback",
"method": "post"
},
"query": {
"module": {
"api_name": "Events"
}
},
"file_type": "ics"
}
This query fetches all the records in the Events module in the ICS format.
To fetch records with criteria
- cvid(string, mandatory) -
Represents the unique ID of the custom view when you want to export records in a custom view. You can obtain the cvid from the Custom View Metadata API. - fields(JSON array, optional) -
Represents the API name of the fields that you want to export. For instance, First_Name, Last_Name, Email, Owner.last_name, and so on. Do not input this key when you want to export the records in the Events module as an ICS file. - page(integer, optional) -
The default value is 1 and means that the first 200,000 records matching your query will get exported. If you want to fetch the records from the range 200,001 to 400,000, then mention the value as '2'. - criteria(JSON object, optional) -
Represents the details that the system uses to filter records.Ex: The API Name of a module or field, comparators used to add two or more criteria, a group which the record/module/field belongs to etc. the following are the keys of this JSON object:- group_operator(string, mandatory if api_name, comparator and value are not specified) -
Represents the Logical operators. Supported values - and, or. - group(JSON array, mandatory if api_name, comparator and value are not specified) -
Represents the array of criteria objects. Each object in the criteria has the following keys:- field(JSON object, mandatory if group and group_operator are not specified)-
The keys in this JSON object represents the API name of a field to be compared. For instance: First_Name, Last_Name, Owner.last_name etc. - value(string/JSON array, mandatory if group and group_operator are not specified)-
Here in this example the key is given as a part of "group" JSON array. You can also specify it directly inside the criteria JSON object. The key represents the value with which the records must be filtered. - comparator(string, mandatory if group and group_operator are not specified.)-
Here in this example the key is given as a part of "group" JSON array. You can also specify it directly inside the criteria JSON object. The key represents the comparator. Example: equal, greater_than. The following table shows the supported comparators.
- field(JSON object, mandatory if group and group_operator are not specified)-
- group_operator(string, mandatory if api_name, comparator and value are not specified) -
Sample Input to fetch records with a single criterion
Copied{
"query": {
"module": {
"api_name": "Contacts"
},
"fields": [
"Last_Name",
"Owner",
"Owner.last_name",
"Account_Name.Account_Name",
"Account_Name.Phone",
"Lead_Source",
"Created_Time"
],
"criteria": {
"field": {
"api_name": "Last_Name"
},
"comparator": "equal",
"value": "Boyle"
}
}
}
This query fetches records based on the specified criteria and the "."(dot) operator is used to fetch data from the parent modules. Account_Name is the default lookup field in the Contacts module. Here, Owner.last_name returns the last name of the owner of the contact, Account_Name returns the ID and Account_Name.Account_Name returns the name of the account associated with the contact, and Account_Name.Phone returns the phone number of the account associated with the contact.
Allowed Comparators
Data type | Comparator | Value and limits |
---|---|---|
Number(Integer)/Decimal/BigInteger/ Currency/Percent | equal, not_equal, in, not_in, less_than, less_equal, greater_than, greater_equal | Any number values or ${EMPTY} for an empty value. Not more than 19 digits for big integer, decimal values for decimal and currency fields. In multi-currency enabled accounts, only the home currency value is supported. |
Text (Email, Phone, URL, Picklist, Multi-select, etc) | equal, not_equal, in, not_in, contains, not_contains, starts_with, ends_with | Any text values or ${EMPTY} for empty value. Not more than 255 characters. |
Date and DateTime | equal, not_equal, in, not_in, between, not_between, greater_than, greater_equal, less_than, less_equal | Any date values in the ISO 8601 format or ${EMPTY} for an empty value. For DateTime fields, milliseconds is not supported. |
Boolean | equal | True or false. |
Lookup | equal, not_equal, in, not_in | Big integer value of the lookup, ${EMPTY} for empty value, or use the .(dot) operator to establish a relation between two modules. Example: "Owner" fetches the ID of the Owner, whereas "Owner.last_name" fetches the last name of the owner. "Account_Name" fetches the ID of the Account associated with the base module, whereas "Account_Name.Phone" fetches the phone number of the account associated with the base module. |
Sample Input to fetch records with multiple criteria and CVID
Copied{
"callback": {
"url": "https://www.example.com/callback",
"method": "post"
},
"query": {
"module": {
"api_name": "Leads"
},
"cvid": "5725767000000087501",
"fields": [
"Last_Name",
"Owner",
"Owner.last_name",
"$converted",
"Lead_Source",
"Lead_Status",
"Company",
"Email",
"Mobile",
"Created_Time"
],
"criteria": {
"group": [
{
"field": {
"api_name": "Last_Name"
},
"comparator": "equal",
"value": "Patricia"
},
{
"field": {
"api_name": "Email"
},
"comparator": "equal",
"value": "patricia.b@zylker.com"
},
{
"comparator": "between",
"field": {
"api_name": "Modified_Time"
},
"value": [
"2019-02-22T15:39:26+05:30",
"2019-07-20T09:00:00+05:30"
]
}
],
"group_operator": "and"
}
}
}
Response Structure
- statusstring
Specifies the status of the API call. Sample - "status": "success".
- messagestring
Specifies the pre-defined comments for the job. Useful in case any errors occur. Sample - "message": "Added successfully."
- detailsJSON object
Contains the following details of the bulk read job.
- operation(string)-
Specifies the type of action the API completed. Sample - "operation" : "read”. - created_by(JSON object)-
Specifies the ID and Name of the user who initiated the bulk read job. Sample - "created_by": { "id": "1000000031045", "name": "Patricia Boyle" }. - created_time (JSON object)-
Specifies the created time of the bulk read job. - state(string)-
Specifies the current status of the bulk read job. Example: "state": "ADDED" or "IN PROGRESS" or "COMPLETED". - id(string)-
Specifies the unique identifier of the bulk read job. Use this ID to check the job status and download the result. Sample - "id": "1000010760002".
- operation(string)-
Sample response
Copied{
"data": [
{
"status": "success",
"code": "ADDED_SUCCESSFULLY",
"message": "Added successfully.",
"details": {
"id": "5725767000000859156",
"operation": "read",
"state": "ADDED",
"created_by": {
"id": "5725767000000411001",
"name": "Patricia Boyle"
},
"created_time": "2019-07-25T05:37:51-07:00"
}
}
],
"info": {}
}
Note
- A maximum of two hundred thousand records can be exported in a single export job. i.e, "page" would be "1" and the records in the page would be "200,000". To know more about the Bulk API limits, go here.
- The first 200,000 records matching the criteria are taken for export if the value of the "page" is "1".
- To fetch data from parent modules, use the "."(dot) operator. For example, Contacts module has the default Account_Name lookup field. To fetch the name of the account that the contact is associated with, use Contacts.Account_Name.Account_Name.
- Use only API names of fields and modules in the input.
- If the "fields" attribute in the query JSON is left empty, all the fields available for the corresponding base module are listed in the CSV file. In case you need only specific fields, specify the field API names for export.
- It is mandatory to specify the cvid if you want to export records under a custom view.
- Along with cvid, you can also specify additional criteria. These criteria will be appended with the existing criteria for the custom/standard view.
For ICS file type
- Exporting records in ICS format is supported only for the Meetings module.
- You can export a maximum of 20,000 records from the Meetings module per batch.
- The "fields" attribute is not supported when you want to export the meetings as an ICS file.
- If you do not specify "file_type" as "ics", the records will be exported in the CSV format, by default.
- If the value of more_records is "true" in the response of the Get Job Details API call, there are more records to be fetched.
Sample callback for job completed
Copied{
"data": [
{
"id": "4150868000004716016",
"operation": "read",
"state": "COMPLETED",
"result": {
"page": 1,
"per_page": 200000,
"count": 127,
"download_url": "/crm/bulk/v2.1/read/4150868000004716016/result",
"more_records": false
},
"query": {
"module": {
"id": "4150868000000002175",
"api_name": "Leads"
},
"page": 1
},
"created_by": {
"id": "4150868000000225013",
"name": "Patricia Boyle"
},
"created_time": "2019-02-22T15:39:26+05:30",
"file_type": "csv"
}
]
}
Possible Errors
- MEDIA_TYPE_NOT_SUPPORTEDHTTP 415
Media type is not supported.
Resolution: You have not passed the 'Content-Type' header along with the request. - INVALID_URL_PATTERNHTTP 404
Please check if the URL trying to access is a correct one
Resolution: The request URL specified is incorrect. Specify a valid request URL. Refer to request URL section above. - OAUTH_SCOPE_MISMATCHHTTP 401
Unauthorized
Resolution: Client does not have ZohoCRM.bulk.read or ZohoCRM.modules.{module_name}.READ. Create a new client with valid scope. Refer to scope section above. - NO_PERMISSIONHTTP 403
Permission denied to read
Resolution: The user does not have permission to read records. Contact your system administrator. - INTERNAL_ERRORHTTP 500
Internal Server Error
Resolution: Unexpected and unhandled exception in Server. Contact support team. - INVALID_REQUEST_METHODHTTP 400
The http request method type is not a valid one
Resolution: You have specified an invalid HTTP method to access the API URL. Specify a valid request method. Refer to endpoints section above. - AUTHORIZATION_FAILEDHTTP 400
User does not have sufficient privilege to read.
Resolution: The user does not have the permission to read records. Contact your system administrator.
Sample callback for job failed
Copied{
"data": [
{
"id": "111111000000052009",
"operation": "read",
"state": "FAILURE",
"result": {
"error_message": {
"status": "error",
"code": "INTERNAL_SERVER_ERROR",
"message": "Internal server error occurred.",
"details": {}
}
},
"query": {
"fields": [
"Last_Name",
"Owner",
"Owner.last_name",
"$converted",
"Lead_Source",
"Lead_Status",
"Company",
"Email",
"Mobile",
"Created_Time"
],
"module": {
"id": "111111000000000042",
"api_name": "Leads"
},
"criteria": {
"field": {
"id": "111111000000000952",
"api_name": "Owner.last_name"
},
"comparator": "equal",
"value": "Patricia Boyle"
},
"page": 1
},
"created_by": {
"id": "111111000000046207",
"name": "Patricia Boyle"
},
"created_time": "2021-02-23T13:36:53+05:30",
"file_type": "csv"
}
]
}