using System;
using Partner;
using System.IO;
using System.Xml;
using System.Text;
using System.Linq;
using RMANotification;
using System.Web.Services;
using sObject = Partner.sObject;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class SFRMAWebService : INotificationBinding
{
    #region Declaration
    RMA__c con = null;
    public SforceService binding;
    public static LoginResult lr;
    CommonService objCommonService = new CommonService();
    public bool Is_Label_Generated__c_field_updated = false;
    notificationsResponse response = new notificationsResponse();
    #endregion

    public SFRMAWebService()
    {
        // constructor logic here 
    }

    #region INotificationBinding Members
    notificationsResponse INotificationBinding.notifications(notifications notifications)
    {
        string rmaId = string.Empty;
        try
        {
            if (!Directory.Exists(Logger.APP_PATH))
                Directory.CreateDirectory(Logger.APP_PATH);

            if (notifications.Notification == null)
            {
                Logger.WriteToLog(Logger.LogType.Info, Constant.SF_TO_FEX, rmaId, "notifications", Constant.CONNECTION_FAILED);
                response.Ack = false;
                return response;
            }

            Is_Label_Generated__c_field_updated = false;

            Logger.WriteToLog(Logger.LogType.Info, Constant.SF_TO_FEX, rmaId, "notifications", Constant.WEB_SERVICE_STARTED);

            RMANotification.RMANotification[] rMANotifications = notifications.Notification;
            if (rMANotifications != null)
            {
                #region Salesforce - Return merchandise authorization code
                for (int i = 0; i < rMANotifications.Length; i++)
                {
                    RMANotification.RMANotification notification = rMANotifications[i];
                    con = notification.sObject;
                    rmaId = con.Id;
                    if (string.IsNullOrEmpty(rmaId))
                    {
                        Logger.WriteToLog(Logger.LogType.Info, Constant.SF_TO_FEX, rmaId, "notifications", Convert.ToString("Info - RMA object is null or empty"));
                        Logger.WriteToLog(Logger.LogType.Info, Constant.SF_TO_FEX, rmaId, "notifications", Constant.WEB_SERVICE_ENDED);
                        response.Ack = false;
                        return response;
                    }
                }
                #endregion

                #region Salesforce Connect
                binding = objCommonService.SalesforceConnect();
                if (binding == null)
                {
                    Logger.WriteToLog(Logger.LogType.Info, Constant.SF_TO_FEX, rmaId, "notifications", Convert.ToString("Salesforce " + Constant.CONNECTION_FAILED));
                    Logger.WriteToLog(Logger.LogType.Info, Constant.SF_TO_FEX, rmaId, "notifications", Constant.WEB_SERVICE_ENDED);
                    response.Ack = false;
                    return response;
                }
                #endregion

                #region FedEx Connect
                if (!string.IsNullOrEmpty(rmaId))
                {
                    Constant.Id = rmaId;

                    string Query = GenerateSalesforceQuery();
                    
                    if (!string.IsNullOrEmpty(Query))
                    {
                        string fedexErrorMessage = string.Empty;
                        sObject mysObject = QueryGenericObject(Query);
                        if (mysObject != null)
                        {
                            var JsonContent = JsonConvert.SerializeObject(mysObject);

                            Logger.WriteToLog(Logger.LogType.Info, Constant.SF_TO_FEX, "Salesforce Request as Response", "notifications", Convert.ToString(JsonContent));

                            JObject jObject = JObject.Parse(JsonContent);
                            if (jObject != null)
                            {
                                List<string> lstOfFile = new List<string>();
                                List<string> lstOfError = new List<string>();

                                FedexHelper fedexHelper = objCommonService.BuildFedexHelper(jObject, ref fedexErrorMessage);

                                int itemCount = 0;
                                if (fedexHelper != null && fedexHelper.any != null && string.IsNullOrEmpty(fedexErrorMessage))
                                {
                                    itemCount = fedexHelper.any.RMA_Lists__r.records.Count();

                                    if (fedexHelper.any.Fedex_TriggerPoint__c == true)
                                    {
                                        if (itemCount > 0)
                                        {
                                            List<decimal> lstOfdecimal = new List<decimal>();
                                            objCommonService.GenerateFedexModelRate(fedexHelper, ref lstOfError, ref lstOfdecimal);

                                            if (lstOfdecimal != null && lstOfdecimal.Count > 0)
                                            {
                                                decimal totalamount = lstOfdecimal.Sum(x => x);
                                                bool isSuccess = UpdateRatedCalculated(rmaId, Constant.Name, totalamount);
                                                if (isSuccess)
                                                {
                                                    // response.Ack = true;
                                                    Logger.WriteToLog(Logger.LogType.Info, Constant.FEX_TO_SF, rmaId, "notifications", Convert.ToString("RMA Object " + Constant.UPDATED_SUCCESSFULLY));
                                                }
                                                response.Ack = true;
                                            }

                                            if (lstOfError != null && lstOfError.Count > 0)
                                            {
                                                int errorCount = 0;
                                                StringBuilder errorBuilder = new StringBuilder();
                                                lstOfError.ForEach(x =>
                                                {
                                                    errorCount++;
                                                    errorBuilder.Append(Convert.ToString(errorCount));
                                                    errorBuilder.Append(Convert.ToString(".)"));
                                                    errorBuilder.Append(x);
                                                    errorBuilder.Append(Convert.ToString(" "));
                                                });

                                                // Attachment Error message
                                                fedexErrorMessage = Convert.ToString(errorBuilder);
                                                bool isSuccess = UpdateErrorMessage(rmaId, Constant.Name, fedexErrorMessage);
                                                if (isSuccess)
                                                {
                                                    response.Ack = true;
                                                    Logger.WriteToLog(Logger.LogType.Info, Constant.FEX_TO_SF, rmaId, "notifications", Convert.ToString("RMA Object " + Constant.UPDATED_SUCCESSFULLY));
                                                }
                                                // End
                                            }
                                        }
                                        else
                                        {
                                            fedexErrorMessage = "Info - No item found";
                                            // Attachment Error message
                                            bool isSuccess = UpdateErrorMessage(rmaId, Constant.Name, fedexErrorMessage);
                                            if (isSuccess)
                                            {
                                                Logger.WriteToLog(Logger.LogType.Info, Constant.FEX_TO_SF, rmaId, "notifications", Convert.ToString("RMA Object " + Constant.UPDATED_SUCCESSFULLY));
                                            }
                                            // End
                                            response.Ack = true;
                                        }
                                    }
                                    else if (fedexHelper.any.Is_Label_Generated__c)
                                    {
                                        fedexErrorMessage = "Label is already generated for requested RMA";
                                        // Attachment Error message
                                        bool isSuccess = UpdateErrorMessage(rmaId, Constant.Name, fedexErrorMessage);
                                        if (isSuccess)
                                        {
                                            Logger.WriteToLog(Logger.LogType.Info, Constant.FEX_TO_SF, rmaId, "notifications", Convert.ToString("RMA Object " + Constant.UPDATED_SUCCESSFULLY));
                                        }
                                        // End
                                        response.Ack = true;
                                    }
                                    else
                                    {
                                        if (itemCount > 0)
                                        {
                                            objCommonService.GenerateFedexModel(fedexHelper, ref lstOfError, ref lstOfFile);

                                            if (lstOfFile != null && lstOfFile.Count > 0)
                                            {
                                                foreach (var x in lstOfFile)
                                                {
                                                    Logger.WriteToLog(Logger.LogType.Info, Constant.FEX_TO_SF, rmaId, "notifications", "Binding FedEx label to Salesforce started for" + x);
                                                    // Attachment data
                                                    bool isSuccess = BindSalesforceLable(x, rmaId);
                                                    //End
                                                    if (isSuccess)
                                                    {
                                                        Logger.WriteToLog(Logger.LogType.Info, Constant.FEX_TO_SF, rmaId, "notifications", "Binding FedEx label to Salesforce completed for" + x);
                                                    }
                                                    else
                                                    {
                                                        Logger.WriteToLog(Logger.LogType.Info, Constant.FEX_TO_SF, rmaId, "notifications", "Binding FedEx label to Salesforce failed for" + x);
                                                    }
                                                }

                                                UpdateLabelGenerated(rmaId, Constant.Name);
                                                response.Ack = true;
                                            }

                                            if (lstOfError != null && lstOfError.Count > 0)
                                            {
                                                int errorCount = 0;
                                                StringBuilder errorBuilder = new StringBuilder();
                                                lstOfError.ForEach(x =>
                                                {
                                                    errorCount++;
                                                    errorBuilder.Append(Convert.ToString(errorCount));
                                                    errorBuilder.Append(Convert.ToString(".)"));
                                                    errorBuilder.Append(x);
                                                    errorBuilder.Append(Convert.ToString(" "));
                                                });

                                                // Attachment Error message
                                                fedexErrorMessage = Convert.ToString(errorBuilder);
                                                bool isSuccess = UpdateErrorMessage(rmaId, Constant.Name, fedexErrorMessage);
                                                if (isSuccess)
                                                {
                                                    response.Ack = true;
                                                    Logger.WriteToLog(Logger.LogType.Info, Constant.FEX_TO_SF, rmaId, "notifications", Convert.ToString("RMA Object " + Constant.UPDATED_SUCCESSFULLY));
                                                }
                                                // End
                                            }
                                            else
                                            {
                                                if (string.IsNullOrEmpty(fedexErrorMessage))
                                                {
                                                    fedexErrorMessage = " ";
                                                    // Attachment Error message
                                                    bool isSuccess = UpdateErrorMessage(rmaId, Constant.Name, fedexErrorMessage);
                                                    if (isSuccess)
                                                    {
                                                        response.Ack = true;
                                                        Logger.WriteToLog(Logger.LogType.Info, Constant.FEX_TO_SF, rmaId, "notifications", Convert.ToString("RMA Object " + Constant.UPDATED_SUCCESSFULLY));
                                                    }
                                                    // End
                                                }
                                            }
                                        }
                                        else
                                        {
                                            fedexErrorMessage = "Info - No item found";
                                            // Attachment Error message
                                            bool isSuccess = UpdateErrorMessage(rmaId, Constant.Name, fedexErrorMessage);
                                            if (isSuccess)
                                            {
                                                Logger.WriteToLog(Logger.LogType.Info, Constant.FEX_TO_SF, rmaId, "notifications", Convert.ToString("RMA Object " + Constant.UPDATED_SUCCESSFULLY));
                                            }
                                            // End
                                            response.Ack = true;
                                        }
                                    }
                                }
                                else
                                {
                                    // Attachment Error message
                                    bool isSuccess = UpdateErrorMessage(rmaId, Constant.Name, fedexErrorMessage);
                                    if (isSuccess)
                                    {
                                        response.Ack = true;
                                        Logger.WriteToLog(Logger.LogType.Info, Constant.FEX_TO_SF, rmaId, "notifications", Convert.ToString("RMA Object " + Constant.UPDATED_SUCCESSFULLY));
                                    }
                                    // End
                                }
                            }
                        }
                        else
                        {
                            fedexErrorMessage = "Info - no data found";
                            // Attachment Error message
                            bool isSuccess = UpdateErrorMessage(rmaId, Constant.Name, fedexErrorMessage);
                            if (isSuccess)
                            {
                                response.Ack = true;
                                Logger.WriteToLog(Logger.LogType.Info, Constant.FEX_TO_SF, rmaId, "notifications", Convert.ToString("RMA Object " + Constant.UPDATED_SUCCESSFULLY));
                            }
                            // End
                        }
                    }
                    else
                    {
                        Logger.WriteToLog(Logger.LogType.Info, Constant.SF_TO_FEX, rmaId, "notifications", Convert.ToString("Invalid Query"));
                        response.Ack = false;
                    }
                }
                #endregion

                Logger.WriteToLog(Logger.LogType.Info, Constant.SF_TO_FEX, rmaId, "notifications", Constant.WEB_SERVICE_ENDED);
            }
        }
        catch (Exception ex)
        {
            Logger.WriteToLog(Logger.LogType.Error, Constant.SF_TO_FEX, rmaId, "notifications", ex.Message + " \n " + Constant.STACKTRACE + ex.StackTrace);

            Logger.WriteToLog(Logger.LogType.Info, Constant.SF_TO_FEX, rmaId, "notifications", Constant.WEB_SERVICE_ENDED);
            response.Ack = false;
        }
        return response;
    }
    #endregion

    [WebMethod]
    public notificationsResponse notifications(notifications notifications)
    {
        return ((INotificationBinding)this).notifications(notifications);
    }

    private string GenerateSalesforceQuery()
    {
        string strQuery = string.Empty;
        try
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("SELECT");
            stringBuilder.Append(" Name");
            stringBuilder.Append(",FedexDetail__r.ClientProductID__c");
            stringBuilder.Append(",FedexDetail__r.UserCredentialKey__c");
            stringBuilder.Append(",FedexDetail__r.UserCredentialPassword__c");
            stringBuilder.Append(",FedexDetail__r.ToFirstName__c");
            stringBuilder.Append(",FedexDetail__r.ToLastName__c");
            stringBuilder.Append(",FedexDetail__r.ToFullName__c");
            stringBuilder.Append(",FedexDetail__r.ToAddress1__c");
            stringBuilder.Append(",FedexDetail__r.ToCity__c");
            stringBuilder.Append(",FedexDetail__r.ToPostalcode__c");
            stringBuilder.Append(",FedexDetail__r.ToState__c");
            stringBuilder.Append(",FedexDetail__r.ToCountry__c");
            stringBuilder.Append(",FedexDetail__r.ToPhoneNumber__c");
            stringBuilder.Append(",FedexDetail__r.AccountNumber__c");
            stringBuilder.Append(",FedexDetail__r.MeterNumber__c");
            stringBuilder.Append(",FedexDetail__r.IntegratorID__c");
            stringBuilder.Append(",FedexDetail__r.ClientProductVersion__c");
            stringBuilder.Append(",Contact__r.FirstName");
            stringBuilder.Append(",Contact__r.LastName");
            stringBuilder.Append(",Contact__r.MobilePhone");
            stringBuilder.Append(",Account__r.ShippingCity");
            stringBuilder.Append(",Account__r.ShippingCountry");
            stringBuilder.Append(",Account__r.ShippingState");
            stringBuilder.Append(",Account__r.ShippingStreet");
            stringBuilder.Append(",Account__r.ShippingPostalCode");
            stringBuilder.Append(",FedexDetail__r.WeightLb__c");
            stringBuilder.Append(",FedexDetail__r.Width__c");
            stringBuilder.Append(",FedexDetail__r.Length__c");
            stringBuilder.Append(",FedexDetail__r.Height__c");
            stringBuilder.Append(",FedexDetail__r.Access_Token__c");
            stringBuilder.Append(",FedexDetail__r.RefreshToken__c");
            stringBuilder.Append(",FedexDetail__r.RefreshTokenExpire__c");
            stringBuilder.Append(",FedexDetail__r.AccessTokenExpire__c");
            stringBuilder.Append(",FedexDetail__r.Code__c");
            stringBuilder.Append(",FedexDetail__r.Client_ID__c");
            stringBuilder.Append(",FedexDetail__r.Name");
            stringBuilder.Append(",FedexDetail__c");
            stringBuilder.Append(",Fedex_Error_Message__c");
            stringBuilder.Append(",Item__c");
            stringBuilder.Append(",(select Id, ProductCode__r.Name, RMANumber__c,Serial_Number__c,CreatedDate from RMA_Lists__r)");
            stringBuilder.Append(",Is_Label_Generated__c");
            stringBuilder.Append(",Customer_Reference__c");
            stringBuilder.Append(",Fedex_TriggerPoint__c");
            stringBuilder.Append(",Fedex_CostPrice__c");
            stringBuilder.Append(" FROM RMA__c WHERE Id = '" + Constant.Id + "'");

            strQuery = Convert.ToString(stringBuilder);
        }
        catch (Exception ex)
        {
            strQuery = string.Empty;
            Logger.WriteToLog(Logger.LogType.Error, Constant.SF_TO_FEX, Constant.Id, "GenerateSalesforceQuery", ex.Message + " \n " + Constant.STACKTRACE + ex.StackTrace);
        }
        return strQuery;
    }

    private string GenerateSalesforceFedexQuery()
    {
        string strQuery = string.Empty;
        try
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("SELECT");
            stringBuilder.Append(" Id");            
            stringBuilder.Append(" FROM FedexDetail__c WHERE Name = 'FED-0002'");

            strQuery = Convert.ToString(stringBuilder);
        }
        catch (Exception ex)
        {
            strQuery = string.Empty;
            Logger.WriteToLog(Logger.LogType.Error, Constant.SF_TO_FEX, Constant.Id, "GenerateSalesforceQuery", ex.Message + " \n " + Constant.STACKTRACE + ex.StackTrace);
        }
        return strQuery;
    }


    private sObject QueryGenericObject(string query)
    {
        sObject mysObject = null;
        try
        {
            QueryResult queryResult = RunSalesforceQuery(query);
            if (queryResult != null)
            {
                mysObject = queryResult.records.FirstOrDefault();
            }
        }
        catch (Exception ex)
        {
            Logger.WriteToLog(Logger.LogType.Error, Constant.SF_TO_FEX, Constant.Id, "QueryGenericObject", ex.Message + " \n " + Constant.STACKTRACE + ex.StackTrace);
        }
        return mysObject;
    }

    private QueryResult RunSalesforceQuery(string query)
    {
        QueryResult result = new QueryResult();
        try
        {
            result = binding.query(query);
        }
        catch (Exception ex)
        {
            Logger.WriteToLog(Logger.LogType.Error, Constant.SF_TO_FEX, Constant.Id, "RunSalesforceQuery", ex.Message + " \n " + Constant.STACKTRACE + ex.StackTrace);
        }
        return result;
    }

    private bool BindSalesforceLable(string filepath, string rmaId)
    {
        bool isSuccess = false;
        try
        {
            if (!string.IsNullOrEmpty(filepath))
            {
                // Attachment
                FileInfo fi = new FileInfo(filepath);
                XmlDocument xmlDoc = new XmlDocument();
                FileStream fs = File.OpenRead(filepath);
                byte[] buffer = new byte[fi.Length];
                fs.Read(buffer, 0, buffer.Length);

                sObject attach = new sObject();
                attach.type = "FeedItem";
                attach.Any = new XmlElement[4];
                attach.Any[0] = xmlDoc.CreateElement("Body");
                attach.Any[0].InnerText = "File Upload";
                attach.Any[1] = xmlDoc.CreateElement("ParentId");
                attach.Any[1].InnerText = rmaId;
                SaveResult sr1 = binding.create(new sObject[] { attach })[0];
                string feedid = sr1.id;

                sObject attach1 = new sObject();
                attach1.type = "Contentversion";
                attach1.Any = new XmlElement[4];
                attach1.Any[0] = xmlDoc.CreateElement("Title");
                string[] strTempname = fi.Name.Split('.');
                attach1.Any[0].InnerText = strTempname[0];
                attach1.Any[1] = xmlDoc.CreateElement("pathOnClient");
                attach1.Any[1].InnerText = "/" + fi.Name;
                attach1.Any[2] = xmlDoc.CreateElement("VersionData");
                attach1.Any[2].InnerText = Convert.ToBase64String(buffer);
                SaveResult sr = binding.create(new sObject[] { attach1 })[0];
                string contentid = sr.id;

                sObject attach2 = new sObject();
                attach2.type = "FeedAttachment";
                attach2.Any = new XmlElement[4];
                attach2.Any[0] = xmlDoc.CreateElement("FeedEntityId");
                attach2.Any[0].InnerText = feedid;
                attach2.Any[1] = xmlDoc.CreateElement("Type");
                attach2.Any[1].InnerText = "Content";
                attach2.Any[2] = xmlDoc.CreateElement("RecordId");
                attach2.Any[2].InnerText = contentid;
                SaveResult sr2 = binding.create(new sObject[] { attach2 })[0];
                // End
                Logger.WriteToLog(Logger.LogType.Info, Constant.FEX_TO_SF, rmaId, "BindSalesforceLable", "FedEx lable attached to Salesforce - " + filepath);
                isSuccess = true;
            }
            else
            {
                Logger.WriteToLog(Logger.LogType.Info, Constant.FEX_TO_SF, rmaId, "BindSalesforceLable", "FedEx lable file path is invalid");
                isSuccess = false;
            }
        }
        catch (Exception ex)
        {
            isSuccess = false;
            Logger.WriteToLog(Logger.LogType.Error, Constant.FEX_TO_SF, rmaId, "BindSalesforceLable", ex.Message + " \n " + Constant.STACKTRACE + ex.StackTrace);
        }
        return isSuccess;
    }


    private void UpdateLabelGenerated(string rmaId, string rmaName)
    {
        try
        {
            List<sObject> sObjectList = new List<sObject>();
            sObjectList = new List<sObject>();
            sObject obj = new sObject();
            obj.type = "RMA__c";
            List<XmlElement> ContactFields = new List<XmlElement>();
            XmlDocument document = new XmlDocument();

            if (!string.IsNullOrEmpty(rmaId))
            {
                XmlElement elementId = document.CreateElement("Id");
                elementId.InnerText = Convert.ToString(rmaId);
                ContactFields.Add(elementId);
            }

            if (!string.IsNullOrEmpty(rmaName))
            {
                XmlElement elementName = document.CreateElement("Name");
                elementName.InnerText = Convert.ToString(rmaName);
                ContactFields.Add(elementName);
            }

            if (!Is_Label_Generated__c_field_updated)
            {
                Is_Label_Generated__c_field_updated = true;
                XmlElement elementFile = document.CreateElement("Is_Label_Generated__c");
                elementFile.InnerText = Convert.ToString(Is_Label_Generated__c_field_updated);
                ContactFields.Add(elementFile);
            }

            obj.Any = ContactFields.ToArray();
            sObjectList.Add(obj);
            SaveResult[] results = binding.update(sObjectList.ToArray());

            bool isSuccess = results[0].success;
        }
        catch (Exception ex)
        {
            Logger.WriteToLog(Logger.LogType.Error, Constant.FEX_TO_SF, Constant.Id, "UpdateLabelGenerated", ex.Message + " \n " + Constant.STACKTRACE + ex.StackTrace);
        }
    }

    private bool UpdateRatedCalculated(string rmaId, string rmaName, decimal amount)
    {
        bool isSuccess = false;
        try
        {
            List<sObject> sObjectList = new List<sObject>();
            sObjectList = new List<sObject>();
            sObject obj = new sObject();
            obj.type = "RMA__c";
            List<XmlElement> ContactFields = new List<XmlElement>();
            XmlDocument document = new XmlDocument();

            if (!string.IsNullOrEmpty(rmaId))
            {
                XmlElement elementId = document.CreateElement("Id");
                elementId.InnerText = Convert.ToString(rmaId);
                ContactFields.Add(elementId);
            }

            if (!string.IsNullOrEmpty(rmaName))
            {
                XmlElement elementName = document.CreateElement("Name");
                elementName.InnerText = Convert.ToString(rmaName);
                ContactFields.Add(elementName);
            }

            XmlElement elementRate = document.CreateElement("Fedex_CostPrice__c");
            elementRate.InnerText = Convert.ToString(amount);
            ContactFields.Add(elementRate);

            XmlElement elementCalculated = document.CreateElement("Fedex_TriggerPoint__c");
            elementCalculated.InnerText = "False";
            ContactFields.Add(elementCalculated);


            obj.Any = ContactFields.ToArray();
            sObjectList.Add(obj);
            SaveResult[] results = binding.update(sObjectList.ToArray());

            isSuccess = results[0].success;
        }
        catch (Exception ex)
        {
            Logger.WriteToLog(Logger.LogType.Error, Constant.FEX_TO_SF, Constant.Id, "UpdateRatedCalculated", ex.Message + " \n " + Constant.STACKTRACE + ex.StackTrace);
        }
        return isSuccess;
    }
    private bool UpdateErrorMessage(string rmaId, string rmaName, string fedexErrorMessage)
    {
        bool isSuccess = false;
        try
        {
            List<sObject> sObjectList = new List<sObject>();
            sObjectList = new List<sObject>();
            sObject obj = new sObject();
            obj.type = "RMA__c";
            List<XmlElement> lstXmlElement = new List<XmlElement>();
            XmlDocument document = new XmlDocument();

            if (!string.IsNullOrEmpty(rmaId))
            {
                XmlElement elementId = document.CreateElement("Id");
                elementId.InnerText = Convert.ToString(rmaId);
                lstXmlElement.Add(elementId);
            }

            if (!string.IsNullOrEmpty(rmaName))
            {
                XmlElement elementName = document.CreateElement("Name");
                elementName.InnerText = Convert.ToString(rmaName);
                lstXmlElement.Add(elementName);
            }

            if (!string.IsNullOrEmpty(fedexErrorMessage))
            {
                XmlElement elementError = document.CreateElement("Fedex_Error_Message__c");
                elementError.InnerText = Convert.ToString(fedexErrorMessage);
                lstXmlElement.Add(elementError);
            }

            obj.Any = lstXmlElement.ToArray();
            sObjectList.Add(obj);
            SaveResult[] results = binding.update(sObjectList.ToArray());

            isSuccess = results[0].success;
        }
        catch (Exception ex)
        {
            Logger.WriteToLog(Logger.LogType.Error, Constant.FEX_TO_SF, Constant.Id, "UpdateErrorMessage", ex.Message + " \n " + Constant.STACKTRACE + ex.StackTrace);
        }
        return isSuccess;
    }
}