Пример #1
0
 public static RelatedListAPIHandler GetInstance(ZCRMRecord parentRecord, ZCRMJunctionRecord junctionRecord)
 {
     return(new RelatedListAPIHandler(parentRecord, junctionRecord));
 }
Пример #2
0
 public static RelatedListAPIHandler GetInstance(ZCRMRecord parentRecord, ZCRMModuleRelation relatedList)
 {
     return(new RelatedListAPIHandler(parentRecord, relatedList));
 }
Пример #3
0
 private RelatedListAPIHandler(ZCRMRecord parentRecord, ZCRMJunctionRecord junctionRecord)
 {
     this.parentRecord   = parentRecord;
     this.junctionRecord = junctionRecord;
 }
Пример #4
0
        public ZCRMInventoryLineItem GetZCRMInventoryLineItem(JObject lineItemJSON)
        {
            JObject productDetails = (JObject)lineItemJSON["product"];

            long lineItemId = Convert.ToInt64(lineItemJSON["id"]);

            ZCRMInventoryLineItem lineItem = ZCRMInventoryLineItem.GetInstance(lineItemId);

            ZCRMRecord product = ZCRMRecord.GetInstance("Products", Convert.ToInt64(productDetails["id"]));

            product.LookupLabel = (string)productDetails["name"];

            if (productDetails.ContainsKey("Product_Code") && productDetails["Product_Code"].Type != JTokenType.Null)
            {
                product.SetFieldValue("Product_Code", (string)productDetails["name"]);
            }

            lineItem.Product = product;

            if (lineItemJSON.ContainsKey("quantity") && lineItemJSON["quantity"].Type != JTokenType.Null)
            {
                lineItem.Quantity = Convert.ToDouble(lineItemJSON["quantity"]);
            }

            if (lineItemJSON.ContainsKey("Discount") && lineItemJSON["Discount"].Type != JTokenType.Null)
            {
                lineItem.Discount = Convert.ToDouble(lineItemJSON["Discount"]);
            }

            if (lineItemJSON.ContainsKey("total_after_discount") && lineItemJSON["total_after_discount"].Type != JTokenType.Null)
            {
                lineItem.TotalAfterDiscount = Convert.ToDouble(lineItemJSON["total_after_discount"]);
            }

            if (lineItemJSON.ContainsKey("net_total") && lineItemJSON["net_total"].Type != JTokenType.Null)
            {
                lineItem.NetTotal = Convert.ToDouble(lineItemJSON["net_total"]);
            }

            if (lineItemJSON.ContainsKey("Tax") && lineItemJSON["Tax"].Type != JTokenType.Null)
            {
                lineItem.TaxAmount = Convert.ToDouble(lineItemJSON["Tax"]);
            }

            if (lineItemJSON.ContainsKey("list_price") && lineItemJSON["list_price"].Type != JTokenType.Null)
            {
                lineItem.ListPrice = Convert.ToDouble(lineItemJSON["list_price"]);
            }

            if (lineItemJSON.ContainsKey("unit_price") && lineItemJSON["unit_price"].Type != JTokenType.Null)
            {
                lineItem.UnitPrice = Convert.ToDouble(lineItemJSON["unit_price"]);
            }

            if (lineItemJSON.ContainsKey("quantity_in_stock") && lineItemJSON["quantity_in_stock"].Type != JTokenType.Null)
            {
                lineItem.QuantityInStock = Convert.ToInt32(lineItemJSON["quantity_in_stock"]);
            }

            if (lineItemJSON.ContainsKey("total") && lineItemJSON["total"].Type != JTokenType.Null)
            {
                lineItem.Total = Convert.ToDouble(lineItemJSON["total"]);
            }

            if (lineItemJSON.ContainsKey("product_description") && lineItemJSON["product_description"].Type != JTokenType.Null)
            {
                lineItem.Description = (string)lineItemJSON["product_description"];
            }

            if (lineItemJSON.ContainsKey("line_tax") && lineItemJSON["line_tax"].Type != JTokenType.Null)
            {
                JArray lineTaxes = (JArray)lineItemJSON["line_tax"];

                foreach (JObject lineTax in lineTaxes)
                {
                    ZCRMTax tax = ZCRMTax.GetInstance((string)lineTax["name"]);

                    tax.Percentage = Convert.ToDouble(lineTax["percentage"]);

                    tax.Value = Convert.ToDouble(lineTax["value"]);

                    lineItem.AddLineTax(tax);
                }
            }

            return(lineItem);
        }
Пример #5
0
 private RelatedListAPIHandler(ZCRMRecord parentRecord, ZCRMModuleRelation relatedList)
 {
     this.parentRecord = parentRecord;
     this.relatedList  = relatedList;
 }
Пример #6
0
        public void SetRecordProperties(JObject recordJSON, ZCRMRecord record)
        {
            foreach (KeyValuePair <string, JToken> token in recordJSON)
            {
                string fieldAPIName = token.Key;

                if (fieldAPIName.Equals("id"))
                {
                    record.EntityId = Convert.ToInt64(token.Value);
                }
                else if (fieldAPIName.Equals("Product_Details") && APIConstants.INVENTORY_MODULES.Contains(this.record.ModuleAPIName) && token.Value.Type != JTokenType.Null)
                {
                    SetInventoryLineItems(token.Value);
                }
                else if (fieldAPIName.Equals("Participants") && this.record.ModuleAPIName.Equals("Events") && token.Value.Type != JTokenType.Null)
                {
                    SetParticipants(token.Value);
                }
                else if (fieldAPIName.Equals("Pricing_Details") && this.record.ModuleAPIName.Equals("Price_Books") && token.Value.Type != JTokenType.Null)
                {
                    SetPriceDetails((JArray)token.Value);
                }
                else if (fieldAPIName.Equals("Created_By") && token.Value.Type != JTokenType.Null)
                {
                    JObject createdObject = (JObject)token.Value;

                    ZCRMUser createdUser = ZCRMUser.GetInstance(Convert.ToInt64(createdObject["id"]), (string)createdObject["name"]);

                    record.CreatedBy = createdUser;
                }
                else if (fieldAPIName.Equals("Modified_By") && token.Value.Type != JTokenType.Null)
                {
                    JObject modifiedObject = (JObject)token.Value;

                    ZCRMUser modifiedBy = ZCRMUser.GetInstance(Convert.ToInt64(modifiedObject["id"]), (string)modifiedObject["name"]);

                    record.ModifiedBy = modifiedBy;
                }
                else if (fieldAPIName.Equals("Created_Time"))
                {
                    record.CreatedTime = CommonUtil.RemoveEscaping((string)JsonConvert.SerializeObject(token.Value));
                }
                else if (fieldAPIName.Equals("Modified_Time"))
                {
                    record.ModifiedTime = CommonUtil.RemoveEscaping((string)JsonConvert.SerializeObject(token.Value));
                }
                else if (fieldAPIName.Equals("Owner") && token.Value.Type != JTokenType.Null)
                {
                    JObject ownerObject = (JObject)token.Value;

                    ZCRMUser ownerUser = ZCRMUser.GetInstance(Convert.ToInt64(ownerObject["id"]), (string)ownerObject["name"]);

                    record.Owner = ownerUser;
                }
                else if (fieldAPIName.Equals("Layout") && token.Value.Type != JTokenType.Null)
                {
                    JObject layoutObject = (JObject)token.Value;

                    ZCRMLayout layout = ZCRMLayout.GetInstance(Convert.ToInt64(layoutObject["id"]));

                    layout.Name = (string)layoutObject["name"];
                }
                else if (fieldAPIName.Equals("Handler") && token.Value.Type != JTokenType.Null)
                {
                    JObject handlerObject = (JObject)token.Value;

                    ZCRMUser handler = ZCRMUser.GetInstance(Convert.ToInt64(handlerObject["id"]), (string)handlerObject["name"]);

                    record.SetFieldValue(fieldAPIName, handler);
                }

                else if (fieldAPIName.Equals("Remind_At") && token.Value.Type != JTokenType.Null)
                {
                    if (token.Value is JObject)
                    {
                        JObject remindObject = (JObject)token.Value;

                        record.SetFieldValue(fieldAPIName, remindObject["ALARM"]);
                    }
                    else
                    {
                        record.SetFieldValue(fieldAPIName, token.Value);
                    }
                }
                else if (fieldAPIName.Equals("Recurring_Activity") && token.Value.Type != JTokenType.Null)
                {
                    JObject recurringActivityObject = (JObject)token.Value;

                    record.SetFieldValue(fieldAPIName, recurringActivityObject["RRULE"]);
                }
                else if (fieldAPIName.Equals("$line_tax") && token.Value.Type != JTokenType.Null)
                {
                    JArray taxDetails = (JArray)token.Value;

                    foreach (JObject taxDetail in taxDetails)
                    {
                        ZCRMTax tax = ZCRMTax.GetInstance((string)taxDetail["name"]);

                        tax.Percentage = Convert.ToDouble(taxDetail["percentage"]);

                        tax.Value = Convert.ToDouble(taxDetail["value"]);

                        record.AddTax(tax);
                    }
                }
                else if (fieldAPIName.Equals("Tax") && token.Value.Type != JTokenType.Null)
                {
                    var taxNames = token.Value;

                    foreach (string data in taxNames)
                    {
                        ZCRMTax tax = ZCRMTax.GetInstance(data);

                        record.AddTax(tax);
                    }
                }
                else if (fieldAPIName.Equals("tags") && token.Value.Type != JTokenType.Null)
                {
                    JArray jsonArray = (JArray)token.Value;

                    List <string> tags = new List <string>();

                    foreach (string tag in jsonArray)
                    {
                        tags.Add(tag);
                    }

                    record.TagNames = tags;
                }
                else if (fieldAPIName.Equals("Tag") && token.Value.Type != JTokenType.Null)
                {
                    JArray jsonArray = (JArray)token.Value;

                    foreach (JObject tag in jsonArray)
                    {
                        ZCRMTag tagIns = ZCRMTag.GetInstance(Convert.ToInt64(tag.GetValue("id")));

                        tagIns.Name = tag.GetValue("name").ToString();

                        record.Tags.Add(tagIns);
                    }
                }
                else if (fieldAPIName.StartsWith("$", StringComparison.CurrentCulture))
                {
                    fieldAPIName = fieldAPIName.TrimStart('\\', '$');

                    if (APIConstants.PROPERTIES_AS_FILEDS.Contains(fieldAPIName))
                    {
                        record.SetFieldValue(fieldAPIName, token.Value);
                    }
                    else
                    {
                        record.SetProperty(fieldAPIName, token.Value);
                    }
                }
                else if (token.Value is JObject)
                {
                    JObject lookupDetails = (JObject)token.Value;

                    ZCRMRecord lookupRecord = ZCRMRecord.GetInstance(fieldAPIName, Convert.ToInt64(lookupDetails["id"]));

                    lookupRecord.LookupLabel = (string)lookupDetails["name"];

                    record.SetFieldValue(fieldAPIName, lookupRecord);
                }
                else if (token.Value is JArray)
                {
                    JArray jsonArray = (JArray)token.Value;

                    List <ZCRMFiles> fileValues = new List <ZCRMFiles>();

                    List <object> values = new List <object>();

                    foreach (Object obj in jsonArray)
                    {
                        if (obj is JObject)
                        {
                            JObject value = (JObject)obj;

                            if (value.ContainsKey("file_Id") && value.ContainsKey("attachment_Id"))
                            {
                                fileValues.Add(this.GetZCRMFileObject(value));
                            }
                            else
                            {
                                values.Add(values);
                            }
                        }
                        else
                        {
                            values.Add(obj);
                        }
                    }
                    if (fileValues.Count > 0)
                    {
                        record.SetFieldValue(fieldAPIName, fileValues);
                    }
                    else
                    {
                        record.SetFieldValue(fieldAPIName, values);
                    }
                }
                else
                {
                    if (token.Value.Type.ToString().Equals("Date"))
                    {
                        record.SetFieldValue(fieldAPIName, CommonUtil.RemoveEscaping((string)JsonConvert.SerializeObject(token.Value)));
                    }
                    else
                    {
                        record.SetFieldValue(fieldAPIName, token.Value);
                    }
                }
            }
        }
Пример #7
0
 public static EntityAPIHandler GetInstance(ZCRMRecord zcrmRecord)
 {
     return(new EntityAPIHandler(zcrmRecord));
 }
Пример #8
0
 protected EntityAPIHandler(ZCRMRecord zcrmRecord)
 {
     record = zcrmRecord;
 }
 /// <summary>
 /// To get ZohoCRM record instance by passing module API name and entity(record) Id.
 /// </summary>
 /// <returns>ZCRMRecord class instance.</returns>
 /// <param name="moduleAPIName">modules' API name (String)</param>
 /// <param name="entityId">entity(record) Id (Long)</param>
 public ZCRMRecord GetRecordInstance(string moduleAPIName, long entityId)
 {
     return(ZCRMRecord.GetInstance(moduleAPIName, entityId));
 }
Пример #10
0
        public BulkAPIResponse <ZCRMRecord> CreateRecords(List <ZCRMRecord> records, List <string> trigger, string lar_id)
        {
            try
            {
                if (records.Count > 100)
                {
                    throw new ZCRMException(APIConstants.API_MAX_RECORDS_MSG);
                }

                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/empty for CreateRecords operation.");
                    }
                }

                requestBodyObject.Add(APIConstants.DATA, dataArray);

                if (trigger != null && trigger.Count > 0)
                {
                    requestBodyObject.Add("trigger", JArray.FromObject(trigger));
                }

                if (lar_id != null)
                {
                    requestBodyObject.Add("lar_id", lar_id);
                }

                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(APIConstants.CODE_SUCCESS))
                    {
                        JObject responseData = entityResponse.ResponseJSON;

                        JObject recordDetails = (JObject)responseData[APIConstants.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) when(!(e is ZCRMException))
            {
                ZCRMLogger.LogError(e);

                throw new ZCRMException(APIConstants.SDK_ERROR, e);
            }
        }
Пример #11
0
        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());
                }

                if (sortByField != null)
                {
                    requestQueryParams.Add("sort_by", sortByField);
                }

                if (sortOrder != null)
                {
                    requestQueryParams.Add("sort_order", sortOrder.ToString());
                }

                requestQueryParams.Add(APIConstants.PAGE, page.ToString());

                requestQueryParams.Add(APIConstants.PER_PAGE, perPage.ToString());

                if (isApproved != null && isApproved != "")
                {
                    requestQueryParams.Add("approved", isApproved);
                }

                if (isConverted != null && isConverted != "")
                {
                    requestQueryParams.Add("converted", isConverted);
                }

                if (fields != null)
                {
                    requestQueryParams.Add("fields", CommonUtil.CollectionToCommaDelimitedString(fields));
                }

                if (modifiedSince != null && modifiedSince != "")
                {
                    requestHeaders.Add("If-Modified-Since", modifiedSince);
                }

                BulkAPIResponse <ZCRMRecord> response = APIRequest.GetInstance(this).GetBulkAPIResponse <ZCRMRecord>();

                List <ZCRMRecord> records = new List <ZCRMRecord>();

                JObject responseJSON = response.ResponseJSON;

                if (responseJSON.ContainsKey(APIConstants.DATA))
                {
                    JArray recordsArray = (JArray)responseJSON[APIConstants.DATA];

                    foreach (JObject recordDetails in recordsArray)
                    {
                        ZCRMRecord record = ZCRMRecord.GetInstance(module.ApiName, Convert.ToInt64(recordDetails["id"]));

                        EntityAPIHandler.GetInstance(record).SetRecordProperties(recordDetails);

                        records.Add(record);
                    }
                }

                response.BulkData = records;

                return(response);
            }
            catch (Exception e) when(!(e is ZCRMException))
            {
                ZCRMLogger.LogError(e);

                throw new ZCRMException(APIConstants.SDK_ERROR, e);
            }
        }
Пример #12
0
        public BulkAPIResponse <ZCRMRecord> MassUpdateRecords(List <long> entityIds, string fieldAPIName, Object value)
        {
            try
            {
                if (entityIds.Count > 100)
                {
                    throw new ZCRMException(APIConstants.API_MAX_RECORDS_MSG);
                }

                //NOTE: null value is not converted to JObject of type null;
                requestMethod = APIConstants.RequestMethod.PUT;

                urlPath = module.ApiName;

                JObject requestBodyObject = new JObject();

                JArray dataArray = new JArray();

                foreach (long id in entityIds)
                {
                    JObject updateJSON = new JObject
                    {
                        { fieldAPIName, JToken.FromObject(EntityAPIHandler.GetInstance(new ZCRMRecord(module.ApiName)).ObjectToJSONData(value)) },
                        { "id", Convert.ToString(id) }
                    };

                    dataArray.Add(updateJSON);
                }

                requestBodyObject.Add(APIConstants.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(APIConstants.CODE_SUCCESS))
                    {
                        JObject responseData = entityResponse.ResponseJSON;

                        JObject recordDetails = (JObject)responseData[APIConstants.DETAILS];

                        ZCRMRecord updatedRecord = ZCRMRecord.GetInstance(module.ApiName, Convert.ToInt64(recordDetails["id"]));

                        EntityAPIHandler.GetInstance(updatedRecord).SetRecordProperties(recordDetails);

                        updatedRecords.Add(updatedRecord);

                        entityResponse.Data = updatedRecord;
                    }
                    else
                    {
                        entityResponse.Data = null;
                    }
                }

                response.BulkData = updatedRecords;

                return(response);
            }
            catch (Exception e) when(!(e is ZCRMException))
            {
                ZCRMLogger.LogError(e);

                throw new ZCRMException(APIConstants.SDK_ERROR, e);
            }
        }