public Response RunUpdateCRMOpportunities(OpportunityDto opportunityDto) { // Initialise variables Response response = Authenticate(opportunityDto.LoginInfo); if (response.Errors.Count > 0) { return(response); } var opportunitiesToUpdate = opportunityDto.OpportunityTable; var xRefDef = opportunityDto.CRMXrefDefinition; CreateSqlAndMapper(xRefDef); try { var opportunityBL = new OpportunityBL(); var utilityBl = new UtilityBL(); // Create a list of opportunities // mapping the values of the opportunity table with the ones on the Salesforce Opportunity object var recordsSdaUpdate = new List <OpportunityDto>(); var recordsSdaAdd = new List <OpportunityDto>(); int crmClientId = -1; foreach (DataRow row in opportunitiesToUpdate.Rows) { int clientID = -1; if (row.Table.Columns.Contains("ClientID") && row["ClientID"] != DBNull.Value) { int.TryParse(row["ClientID"].ToString(), out clientID); crmClientId = clientID; } else { response.Errors.Add("Invalid Client Id."); } var strCRMOppId = string.Empty; if (row.Table.Columns.Contains("CRMOppID") && row["CRMOppID"] != DBNull.Value) { strCRMOppId = row["CRMOppID"].ToString(); } List <OppStatusDto> lstat = utilityBl.GetStatuses(clientID); bool hasStatus = false; if (row.Table.Columns.Contains("OppStatus") && row["OppStatus"] != DBNull.Value) { var strStatus = row["OppStatus"].ToString(); int statValue = 0; foreach (var stat in lstat) { if (stat.OppStatus.ToLower().Trim().Equals(strStatus.ToLower().Trim())) { statValue = stat.ID; break; } } if (statValue > 0) { row["OppStatus"] = statValue; hasStatus = true; } } if (!hasStatus) { if (lstat.Count > 0) { var defaultStatus = lstat.Where(s => s.Default.Equals("Y")).FirstOrDefault(); if (defaultStatus != null) { row["OppStatus"] = defaultStatus.ID; hasStatus = true; } } if (!hasStatus) { response.Errors.Add("Invalid Opp Status."); } } OpportunityDto opportunity = null; if (!string.IsNullOrEmpty(strCRMOppId)) { opportunity = opportunityBL.GetOpportunityByClientIDAndCRMOppID(clientID, strCRMOppId); } bool isNewOpportunity; if (opportunity == null) { // Create a new opportunity opportunity = new OpportunityDto(); isNewOpportunity = true; } else { isNewOpportunity = false; } // Copy the fields to the opportunity var type = opportunity.GetType(); //loop over the rows mapping the database field with the corresponding field on the CRM foreach (var mappingObject in DatabaseToCRMMap) { var propertyInfo = type.GetProperty(mappingObject.CRMField); if (propertyInfo != null) { if (row[mappingObject.SdaField] != DBNull.Value) { utilityBl.SetProperty(opportunity, propertyInfo, row[mappingObject.SdaField]); } } } if (isNewOpportunity) { recordsSdaAdd.Add(opportunity); } else { recordsSdaUpdate.Add(opportunity); } } // Update the list of opportunities on Salesforce response = new Response(); Response responseUpdate = opportunityBL.UpdateSdaCloudOpportunity(recordsSdaUpdate); Response responseAdd = opportunityBL.AddSdaCloudOpportunity(recordsSdaAdd); // Merge both results foreach (string result in responseUpdate.Results) { response.Results.Add(result); } foreach (string result in responseAdd.Results) { response.Results.Add(result); } //Merge both errors foreach (string error in responseUpdate.Errors) { response.Errors.Add(error); } foreach (string error in responseAdd.Errors) { response.Errors.Add(error); } } catch (Exception ex) { response.Errors.Add(ex.Message); if (ex.InnerException != null) { response.Errors.Add(ex.InnerException.Message); } } return(response); }
/// <summary> /// Runs the update CRM quotes. /// </summary> /// <param name="quoteDto">The quote dto.</param> /// <returns></returns> public Response RunUpdateCRMQuotes(QuoteDto quoteDto) { // Initialise variables Response response = Authenticate(quoteDto.LoginInfo); if (response.Errors.Count > 0) { return(response); } var quotesToUpdate = quoteDto.QuoteTable; var xRefDef = quoteDto.CRMXrefDefinition; CreateSqlAndMapper(xRefDef); try { var quoteBl = new QuoteBL(); var utilityBl = new UtilityBL(); // Create a list of opportunities // mapping the values of the opportunity table with the ones on the Salesforce Opportunity object var recordsSdaUpdate = new List <QuoteDto>(); var recordsSdaAdd = new List <QuoteDto>(); int crmClientId = -1; foreach (DataRow row in quotesToUpdate.Rows) { int clientID = -1; if (row["ClientID"] != DBNull.Value) { int.TryParse(row["ClientID"].ToString(), out clientID); crmClientId = clientID; } else { response.Errors.Add("Invalid Client Id."); } var strQuoteId = string.Empty; if (row["QuoteID"] != DBNull.Value) { strQuoteId = row["QuoteID"].ToString(); } else { response.Errors.Add("Invalid QuoteId."); } var quote = quoteBl.GetQuoteByClientIDAndQuoteID(clientID, strQuoteId); bool isNewQuote; if (quote == null) { // Create a new opportunity quote = new QuoteDto(); isNewQuote = true; } else { isNewQuote = false; } // Copy the fields to the opportunity var type = quote.GetType(); //loop over the rows mapping the database field with the corresponding field on the CRM foreach (var mappingObject in DatabaseToCRMMap) { var propertyInfo = type.GetProperty(mappingObject.CRMField); if (propertyInfo != null) { if (row[mappingObject.SdaField] != DBNull.Value) { utilityBl.SetProperty(quote, propertyInfo, row[mappingObject.SdaField]); } } } if (isNewQuote) { recordsSdaAdd.Add(quote); } else { recordsSdaUpdate.Add(quote); } } // Update the list of opportunities on Salesforce response = new Response(); Response responseUpdate = quoteBl.UpdateSdaCloudQuote(recordsSdaUpdate); Response responseAdd = quoteBl.AddSdaCloudQuote(recordsSdaAdd); // Merge both results foreach (string result in responseUpdate.Results) { response.Results.Add(result); } foreach (string result in responseAdd.Results) { response.Results.Add(result); } //Merge both errors foreach (string error in responseUpdate.Errors) { response.Errors.Add(error); } foreach (string error in responseAdd.Errors) { response.Errors.Add(error); } } catch (Exception ex) { response.Errors.Add(ex.Message); } return(response); }