private BulkAPIResponse <ZCRMRecord> SearchRecords(string searchKey, string searchValue, int page, int perPage) { try{ requestMethod = APIConstants.RequestMethod.GET; urlPath = module.ApiName + "/search"; requestQueryParams.Add(searchKey, searchValue); requestQueryParams.Add("page", page.ToString()); requestQueryParams.Add("per_page", perPage.ToString()); BulkAPIResponse <ZCRMRecord> response = APIRequest.GetInstance(this).GetBulkAPIResponse <ZCRMRecord>(); List <ZCRMRecord> recordsList = new List <ZCRMRecord>(); JObject responseJSON = response.ResponseJSON; JArray recordsArray = (JArray)responseJSON.GetValue("data"); foreach (JObject recordDetails in recordsArray) { ZCRMRecord record = ZCRMRecord.GetInstance(module.ApiName, Convert.ToInt64(recordDetails.GetValue("id"))); EntityAPIHandler.GetInstance(record).SetRecordProperties(recordDetails); recordsList.Add(record); } response.BulkData = recordsList; return(response); }catch (Exception e) { //TODO: Log the exception; Console.WriteLine("Exception caught at searchRecords"); Console.WriteLine(e); throw new ZCRMException("ZCRM_INTERNAL_ERROR"); } }
public BulkAPIResponse <ZCRMRecord> CreateRecords(List <ZCRMRecord> records) { if (records.Count > 100) { throw new ZCRMException("Cannot process more than 100 records at a time"); } try{ requestMethod = APIConstants.RequestMethod.POST; urlPath = module.ApiName; JObject requestBodyObject = new JObject(); JArray dataArray = new JArray(); foreach (ZCRMRecord record in records) { if (record.EntityId == null) { dataArray.Add(EntityAPIHandler.GetInstance(record).GetZCRMRecordAsJSON()); } else { throw new ZCRMException("Entity ID MUST be null for create operation."); } } requestBodyObject.Add("data", dataArray); requestBody = requestBodyObject; BulkAPIResponse <ZCRMRecord> response = APIRequest.GetInstance(this).GetBulkAPIResponse <ZCRMRecord>(); List <ZCRMRecord> createdRecords = new List <ZCRMRecord>(); List <EntityResponse> responses = response.BulkEntitiesResponse; int responseSize = responses.Count; for (int i = 0; i < responseSize; i++) { EntityResponse entityResponse = responses[i]; if (entityResponse.Status.Equals("success")) { JObject responseData = entityResponse.ResponseJSON; JObject recordDetails = (JObject)responseData.GetValue("details"); ZCRMRecord newRecord = records[i]; EntityAPIHandler.GetInstance(newRecord).SetRecordProperties(recordDetails); createdRecords.Add(newRecord); entityResponse.Data = newRecord; } else { entityResponse.Data = null; } } response.BulkData = createdRecords; return(response); }catch (Exception e) { //TODO: Log the info; Console.WriteLine("Exception at createRecords"); Console.WriteLine(e); throw new ZCRMException("ZCRM_INTERNAL_ERROR"); } }
public BulkAPIResponse <ZCRMRecord> UpdateRecords(List <long> entityIds, string fieldAPIName, Object value) { if (entityIds.Count > 100) { throw new ZCRMException("Cannot process more than 100 records at a time"); } //NOTE: null value is not converted to JObject of type null; try { requestMethod = APIConstants.RequestMethod.PUT; urlPath = module.ApiName; JObject requestBodyObject = new JObject(); JArray dataArray = new JArray(); foreach (long id in entityIds) { JObject updateJSON = new JObject(); //TODO: Check the below line on the usage of arrays; updateJSON.Add(fieldAPIName, value.ToString()); updateJSON.Add("id", Convert.ToString(id)); dataArray.Add(updateJSON); } requestBodyObject.Add("data", dataArray); requestBody = requestBodyObject; BulkAPIResponse <ZCRMRecord> response = APIRequest.GetInstance(this).GetBulkAPIResponse <ZCRMRecord>(); List <ZCRMRecord> updatedRecords = new List <ZCRMRecord>(); List <EntityResponse> responses = response.BulkEntitiesResponse; foreach (EntityResponse entityResponse in responses) { if (entityResponse.Status.Equals("success")) { JObject responseData = entityResponse.ResponseJSON; JObject recordDetails = (JObject)responseData.GetValue("details"); ZCRMRecord updatedRecord = ZCRMRecord.GetInstance(module.ApiName, Convert.ToInt64(recordDetails.GetValue("id"))); EntityAPIHandler.GetInstance(updatedRecord).SetRecordProperties(recordDetails); updatedRecords.Add(updatedRecord); entityResponse.Data = updatedRecord; } else { entityResponse.Data = null; } } response.BulkData = updatedRecords; return(response); }catch (Exception e) { //TODO: Log the info; Console.WriteLine("Exception at updateRecords"); Console.WriteLine(e); throw new ZCRMException("ZCRM_INTERNAL_ERROR"); } }
public BulkAPIResponse <ZCRMRecord> GetRecords(long?cvId, string sortByField, CommonUtil.SortOrder?sortOrder, int page, int perPage, string modifiedSince, string isConverted, string isApproved, List <string> fields) { try{ requestMethod = APIConstants.RequestMethod.GET; urlPath = module.ApiName; if (cvId != null) { requestQueryParams.Add("cvid", cvId.ToString()); } requestQueryParams.Add("sort_by", sortByField); if (sortOrder != null) { requestQueryParams.Add("sort_order", sortOrder.ToString()); } requestQueryParams.Add("page", page.ToString()); requestQueryParams.Add("per_page", perPage.ToString()); requestQueryParams.Add("coneverted", isConverted); requestQueryParams.Add("approved", isApproved); if (fields != null) { requestQueryParams.Add("fields", CommonUtil.CollectionToCommaDelimitedString(fields)); } requestHeaders.Add("If-Modified-Since", modifiedSince); BulkAPIResponse <ZCRMRecord> response = APIRequest.GetInstance(this).GetBulkAPIResponse <ZCRMRecord>(); List <ZCRMRecord> records = new List <ZCRMRecord>(); JObject responseJSON = response.ResponseJSON; JArray recordsArray = (JArray)responseJSON.GetValue("data"); foreach (JObject recordDetails in recordsArray) { ZCRMRecord record = ZCRMRecord.GetInstance(module.ApiName, Convert.ToInt64(recordDetails.GetValue("id"))); EntityAPIHandler.GetInstance(record).SetRecordProperties(recordDetails); records.Add(record); } response.BulkData = records; return(response); }catch (Exception e) { //TODO: Log the info; Console.WriteLine("Exception in MassEntityAPIHandler.GetRecords"); Console.WriteLine(e); throw new ZCRMException("ZCRM_INTERNA_ERROR", e.ToString()); } }
//TODO: Handle exceptions; public BulkAPIResponse <ZCRMRecord> GetRecords(string sortByField, CommonUtil.SortOrder?sortOrder, int page, int perPage, string modifiedSince) { try { requestMethod = APIConstants.RequestMethod.GET; urlPath = $"{parentRecord.ModuleAPIName}/{parentRecord.EntityId}/{relatedList.ApiName}"; requestQueryParams.Add("sort_by", sortByField); if (sortOrder != null) { requestQueryParams.Add("sort_order", sortOrder.ToString()); } requestQueryParams.Add("page", page.ToString()); requestQueryParams.Add("per_page", perPage.ToString()); requestHeaders.Add("If-Modified-Since", modifiedSince); BulkAPIResponse <ZCRMRecord> response = APIRequest.GetInstance(this).GetBulkAPIResponse <ZCRMRecord>(); List <ZCRMRecord> recordsList = new List <ZCRMRecord>(); JObject responseJSON = response.ResponseJSON; JArray recordsArray = (JArray)responseJSON.GetValue("data"); foreach (JObject recordDetails in recordsArray) { ZCRMRecord record = ZCRMRecord.GetInstance(relatedList.ApiName, Convert.ToInt64(recordDetails.GetValue("id"))); EntityAPIHandler.GetInstance(record).SetRecordProperties(recordDetails); recordsList.Add(record); } response.BulkData = recordsList; return(response); } catch (Exception e) { //TODO: Handle the exceptions; Console.WriteLine("Exception in GetRecords"); Console.WriteLine(e); throw new ZCRMException("ZCRM_INTERNAL_ERROR"); } }