Java SDK Samples - Bulk Read Operations
package samples.src.com.zoho.crm.api.bulkread;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.zoho.crm.api.bulkread.APIException;
import com.zoho.crm.api.bulkread.ActionHandler;
import com.zoho.crm.api.bulkread.ActionResponse;
import com.zoho.crm.api.bulkread.ActionWrapper;
import com.zoho.crm.api.bulkread.BulkReadOperations;
import com.zoho.crm.api.bulkread.CallBack;
import com.zoho.crm.api.bulkread.Criteria;
import com.zoho.crm.api.bulkread.FileBodyWrapper;
import com.zoho.crm.api.bulkread.JobDetail;
import com.zoho.crm.api.bulkread.Query;
import com.zoho.crm.api.bulkread.RequestWrapper;
import com.zoho.crm.api.bulkread.ResponseHandler;
import com.zoho.crm.api.bulkread.ResponseWrapper;
import com.zoho.crm.api.bulkread.Result;
import com.zoho.crm.api.bulkread.SuccessResponse;
import com.zoho.crm.api.util.Choice;
import com.zoho.crm.api.util.APIResponse;
import com.zoho.crm.api.util.Model;
import com.zoho.crm.api.util.StreamWrapper;
public class BulkRead
{
/**
* Create BulkRead Job
* This method is used to create a bulk read job to export records.
* @param moduleAPIName The API Name of the record's module
* @throws Exception
*/
public static void createBulkReadJob(String moduleAPIName) throws Exception
{
//example
//String moduleAPIName = "Leads";
//Get instance of BulkReadOperations Class
BulkReadOperations bulkReadOperations = new BulkReadOperations();
//Get instance of RequestWrapper Class that will contain the request body
RequestWrapper requestWrapper = new RequestWrapper();
//Get instance of CallBack Class
CallBack 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 Bulk Read Job's details is posted to this URL on successful completion / failure of job.
requestWrapper.setCallback(callback);
//Get instance of Query Class
Query query = new Query();
//Specifies the API Name of the module to be read.
query.setModule(moduleAPIName);
//Specifies the unique ID of the custom view whose records you want to export.
// query.setCvid("34770610087501");
// List of Field API Names
List fieldAPINames = new ArrayList();
fieldAPINames.add("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 criteria = new Criteria();
criteria.setGroupOperator(new Choice("or"));
List criteriaList = new ArrayList();
Criteria group11 = new Criteria();
group11.setGroupOperator(new Choice("and"));
List groupList11 = new ArrayList();
Criteria group111 = new Criteria();
group111.setAPIName("Company");
group111.setComparator(new Choice("equal"));
group111.setValue("Zoho");
groupList11.add(group111);
Criteria group112 = new Criteria();
group112.setAPIName("Owner");
group112.setComparator(new Choice("in"));
List owner = new ArrayList(Arrays.asList("34770610173021"));
group112.setValue(owner);
groupList11.add(group112);
group11.setGroup(groupList11);
criteriaList.add(group11);
Criteria group12 = new Criteria();
group12.setGroupOperator(new Choice("or"));
List groupList12 = new ArrayList();
Criteria group121 = new Criteria();
group121.setAPIName("Paid");
group121.setComparator(new Choice("equal"));
group121.setValue(true);
groupList12.add(group121);
Criteria group122 = new Criteria();
// To set API name of a field.
group122.setAPIName("Created_Time");
// To set comparator(eg: equal, greater_than.).
group122.setComparator(new Choice("between"));
List createdTime = new ArrayList(Arrays.asList("2020-06-03T17:31:48+05:30", "2020-06-03T17:31:48+05:30"));
// To set the value to be compare.
group122.setValue(createdTime);
groupList12.add(group122);
group12.setGroup(groupList12);
criteriaList.add(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"));
//Sample request body
// {"query":{"criteria":{"group_operator":"or","group":[{"group_operator":"and","group":[{"comparator":"equal","api_name":"All_day","value":false},
// {"comparator":"in","api_name":"Owner","value":["3477061001"]}]},{"group_operator":"or","group":[{"comparator":"equal","api_name":"Event_Title","value":"New Event"},
// {"comparator":"between","api_name":"Created_Time","value":["2020-06-03T17:31:48+05:30","2020-06-03T17:31:48+05:30"]}]}]},"module":"Events","page":1},
// "callback":{"method":"post","url":"https://www.example.com/callback"}}
//
//Call createBulkReadJob method that takes RequestWrapper instance as parameter
APIResponse response = bulkReadOperations.createBulkReadJob(requestWrapper);
if(response != null)
{
//Get the status code from response
System.out.println("Status Code: " + response.getStatusCode());
//Check if expected response is received
if(response.isExpected())
{
//Get object from response
ActionHandler actionHandler = response.getObject();
if(actionHandler instanceof ActionWrapper)
{
//Get the received ActionWrapper instance
ActionWrapper actionWrapper = (ActionWrapper) actionHandler;
//Get the list of obtained ActionResponse instances
List actionResponses = actionWrapper.getData();
for(ActionResponse actionResponse : actionResponses)
{
//Check if the request is successful
if(actionResponse instanceof SuccessResponse)
{
//Get the received SuccessResponse instance
SuccessResponse successResponse = (SuccessResponse)actionResponse;
//Get the Status
System.out.println("Status: " + successResponse.getStatus().getValue());
//Get the Code
System.out.println("Code: " + successResponse.getCode().getValue());
System.out.println("Details: " );
//Get the details map
for(Map.Entry entry : successResponse.getDetails().entrySet())
{
//Get each value in the map
System.out.println(entry.getKey() + ": " + entry.getValue());
}
//Get the Message
System.out.println("Message: " + successResponse.getMessage().getValue());
}
//Check if the request returned an exception
else if(actionResponse instanceof APIException)
{
//Get the received APIException instance
APIException exception = (APIException) actionResponse;
//Get the Status
System.out.println("Status: " + exception.getStatus().getValue());
//Get the Code
System.out.println("Code: " + exception.getCode().getValue());
System.out.println("Details: " );
//Get the details map
for(Map.Entry entry : exception.getDetails().entrySet())
{
//Get each value in the map
System.out.println(entry.getKey() + ": " + entry.getValue());
}
//Get the Message
System.out.println("Message: " + exception.getMessage().getValue());
}
}
}
//Check if the request returned an exception
else if(actionHandler instanceof APIException)
{
//Get the received APIException instance
APIException exception = (APIException) actionHandler;
//Get the Status
System.out.println("Status: " + exception.getStatus().getValue());
//Get the Code
System.out.println("Code: " + exception.getCode().getValue());
System.out.println("Details: " );
//Get the details map
for(Map.Entry entry : exception.getDetails().entrySet())
{
//Get each value in the map
System.out.println(entry.getKey() + ": " + entry.getValue());
}
//Get the Message
System.out.println("Message: " + exception.getMessage().getValue());
}
}
else
{//If response is not as expected
//Get model object from response
Model responseObject = response.getModel();
//Get the response object's class
Class extends Model> clas = responseObject.getClass();
//Get all declared fields of the response class
Field[] fields = clas.getDeclaredFields();
for(Field field : fields)
{
//Get each value
System.out.println(field.getName() + ":" + field.get(responseObject));
}
}
}
}
}
package samples.src.com.zoho.crm.api.bulkread;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.zoho.crm.api.bulkread.APIException;
import com.zoho.crm.api.bulkread.ActionHandler;
import com.zoho.crm.api.bulkread.ActionResponse;
import com.zoho.crm.api.bulkread.ActionWrapper;
import com.zoho.crm.api.bulkread.BulkReadOperations;
import com.zoho.crm.api.bulkread.CallBack;
import com.zoho.crm.api.bulkread.Criteria;
import com.zoho.crm.api.bulkread.FileBodyWrapper;
import com.zoho.crm.api.bulkread.JobDetail;
import com.zoho.crm.api.bulkread.Query;
import com.zoho.crm.api.bulkread.RequestWrapper;
import com.zoho.crm.api.bulkread.ResponseHandler;
import com.zoho.crm.api.bulkread.ResponseWrapper;
import com.zoho.crm.api.bulkread.Result;
import com.zoho.crm.api.bulkread.SuccessResponse;
import com.zoho.crm.api.util.Choice;
import com.zoho.crm.api.util.APIResponse;
import com.zoho.crm.api.util.Model;
import com.zoho.crm.api.util.StreamWrapper;
public class BulkRead
{
/**
* Get BulkRead Job Details
* This method is used to get the details of a bulk read job performed previously.
* @param jobId The unique ID of the bulk read job.
* @throws Exception
*/
public static void getBulkReadJobDetails(Long jobId) throws Exception
{
//example
//Long jobId = 34770615177002l;
//Get instance of BulkReadOperations Class
BulkReadOperations bulkReadOperations = new BulkReadOperations();
//Call getBulkReadJobDetails method that takes jobId as parameter
APIResponse response = bulkReadOperations.getBulkReadJobDetails(jobId);
if(response != null)
{
//Get the status code from response
System.out.println("Status Code: " + response.getStatusCode());
if(Arrays.asList(204,304).contains(response.getStatusCode()))
{
System.out.println(response.getStatusCode() == 204? "No Content" : "Not Modified");
return;
}
//Check if expected response is received
if(response.isExpected())
{
//Get object from response
ResponseHandler responseHandler = response.getObject();
if(responseHandler instanceof ResponseWrapper)
{
//Get the received ResponseWrapper instance
ResponseWrapper responseWrapper = (ResponseWrapper) responseHandler;
//Get the list of obtained jobDetail instances
List jobDetails = responseWrapper.getData();
for(JobDetail jobDetail : jobDetails)
{
//Get the Job ID of each jobDetail
System.out.println("Bulk read Job ID: " + jobDetail.getId());
//Get the Operation of each jobDetail
System.out.println("Bulk read Operation: " + jobDetail.getOperation());
//Get the Operation of each jobDetail
System.out.println("Bulk read State: " + jobDetail.getState().getValue());
//Get the Result instance of each jobDetail
Result result = jobDetail.getResult();
//Check if Result is not null
if(result != null)
{
//Get the Page of the Result
System.out.println("Bulkread Result Page: " + result.getPage().toString());
//Get the Count of the Result
System.out.println("Bulkread Result Count: "+ result.getCount().toString());
//Get the Download URL of the Result
System.out.println("Bulkread Result Download URL: "+ result.getDownloadUrl());
//Get the Per_Page of the Result
System.out.println("Bulkread Result Per_Page: "+ result.getPerPage().toString());
//Get the MoreRecords of the Result
System.out.println("Bulkread Result MoreRecords: "+ result.getMoreRecords().toString());
}
// Get the Query instance of each jobDetail
Query query = jobDetail.getQuery();
if(query != null)
{
//Get the Module Name of the Query
System.out.println("Bulk read Query Module: " + query.getModule());
//Get the Page of the Query
System.out.println("Bulk read Query Page: " + query.getPage().toString());
//Get the cvid of the Query
System.out.println("Bulk read Query cvid: " + query.getCvid());
//Get the fields List of each Query
List fields = query.getFields();
//Check if fields is not null
if(fields != null)
{
for(Object fieldName : fields)
{
//Get the Field Name of the Query
System.out.println("Bulk read Query Fields: " + fieldName);
}
}
// Get the Criteria instance of each Query
Criteria criteria = query.getCriteria();
//Check if criteria is not null
if(criteria != null)
{
printCriteria(criteria);
}
}
//Get the CreatedBy User instance of each jobDetail
com.zoho.crm.api.users.User createdBy = jobDetail.getCreatedBy();
//Check if createdBy is not null
if(createdBy != null)
{
//Get the ID of the CreatedBy User
System.out.println("Bulkread Created By User-ID: " + createdBy.getId());
//Get the Name of the CreatedBy User
System.out.println("Bulkread Created By user-Name: " + createdBy.getName());
}
//Get the CreatedTime of each jobDetail
System.out.println("Bulkread CreatedTime: " + jobDetail.getCreatedTime());
//Get the ID of each jobDetail
System.out.println("Bulkread File Type: " + jobDetail.getFileType());
}
}
//Check if the request returned an exception
else if(responseHandler instanceof APIException)
{
//Get the received APIException instance
APIException exception = (APIException) responseHandler;
//Get the Status
System.out.println("Status: " + exception.getStatus().getValue());
//Get the Code
System.out.println("Code: " + exception.getCode().getValue());
System.out.println("Details: " );
//Get the details map
for(Map.Entry entry : exception.getDetails().entrySet())
{
//Get each value in the map
System.out.println(entry.getKey() + ": " + entry.getValue());
}
//Get the Message
System.out.println("Message: " + exception.getMessage().getValue());
}
}
else
{//If response is not as expected
//Get model object from response
Model responseObject = response.getModel();
//Get the response object's class
Class extends Model> clas = responseObject.getClass();
//Get all declared fields of the response class
java.lang.reflect.Field[] fields = clas.getDeclaredFields();
for(java.lang.reflect.Field field : fields)
{
//Get each value
System.out.println(field.getName() + ":" + field.get(responseObject));
}
}
}
}
private static void printCriteria(Criteria criteria)
{
//Get the APIName of the Criteria
System.out.println("Bulk read Query Criteria APIName: " + criteria.getAPIName());
if(criteria.getComparator() != null)
{
//Get the Comparator of the Criteria
System.out.println("Bulk read Query Criteria Comparator: " + criteria.getComparator().getValue());
}
if(criteria.getValue() != null)
{
//Get the Value of the Criteria
System.out.println("Bulk read Query Criteria Value: " + criteria.getValue().toString());
}
//Get the List of Criteria instance of each Criteria
List criteriaGroup = criteria.getGroup();
if(criteriaGroup != null)
{
for(Criteria criteria1 : criteriaGroup)
{
printCriteria(criteria1);
}
}
if(criteria.getGroupOperator() != null)
{
//Get the Group Operator of the Criteria
System.out.println("Bulk read Query Criteria Group Operator: " + criteria.getGroupOperator().getValue());
}
}
}