public Entity FormTargetEntityObject(string targetEntity, Entity entityStaging, List <Common.Mapping> mappings, IOrganizationService service, ref bool ValidationStatus, ref bool canReturn, ref string errorMessage) { Entity objTargetEntity = new Entity(targetEntity); foreach (Common.Mapping objMapping in mappings) { try { Common.Mapping mapping = objMapping; GetValueFromSourceEntity(entityStaging, ref mapping, ref ValidationStatus, ref canReturn, ref errorMessage); if (!string.IsNullOrEmpty(mapping.value)) { SetValueToTargetEntity(ref objTargetEntity, mapping, service, ref ValidationStatus, ref canReturn, ref errorMessage); } } catch (Exception ex) { //validationStatus-> False canReturn->true Add log UpdateValidationMessage UpdateValidationMessage("Error While FormTargetEntityObject " + ex.Message, ref errorMessage); ValidationStatus = false; canReturn = true; } } return(objTargetEntity); }
public void GetValueFromSourceEntity(Entity entityStaging, ref Common.Mapping mapping, ref bool ValidationStatus, ref bool canReturn, ref string errorMessage) { try { if (mapping.SourceDatatype == (int)ImportDetailsMapping.SourceDataType_OptionSet.SingleLineOfText || mapping.SourceDatatype == (int)ImportDetailsMapping.SourceDataType_OptionSet.MultipleLineOfText) { mapping.value = entityStaging.GetAttributeValue <string>(mapping.Source); } else if (mapping.SourceDatatype == (int)ImportDetailsMapping.SourceDataType_OptionSet.WholeNumber) { mapping.value = entityStaging.GetAttributeValue <int>(mapping.Source).ToString(); } else if (mapping.SourceDatatype == (int)ImportDetailsMapping.SourceDataType_OptionSet.DateTime) { mapping.value = entityStaging.GetAttributeValue <DateTime>(mapping.Source).ToShortDateString(); } else { mapping.value = string.Empty; } } catch (Exception ex) { //validationStatus-> False canReturn->true Add log UpdateValidationMessage UpdateValidationMessage("Error While GetValueFromSourceEntity " + ex.Message, ref errorMessage); ValidationStatus = false; canReturn = true; } }
public void SetValueToTargetEntity(ref Entity entity, Common.Mapping mapping, IOrganizationService service, ref bool ValidationStatus, ref bool canReturn, ref string errorMessage) { try { if (!string.IsNullOrEmpty(mapping.value)) { if (mapping.DataType == (int)ImportDetailsMapping.TargetDataType_OptionSet.SingleLineOfText || mapping.DataType == (int)ImportDetailsMapping.TargetDataType_OptionSet.MultipleLineOfText) { entity[mapping.Target] = mapping.value; } else if (mapping.DataType == (int)ImportDetailsMapping.TargetDataType_OptionSet.WholeNumber) { mapping.value = mapping.value.Replace('%', ' ').Trim(); entity[mapping.Target] = Convert.ToInt32(mapping.value); } else if (mapping.DataType == (int)ImportDetailsMapping.TargetDataType_OptionSet.DateTime) { DateTime dt; if (DateTime.TryParse(mapping.value, out dt)) { //dt = DateTime.SpecifyKind(dt, DateTimeKind.Utc); entity[mapping.Target] = dt; } } else if (mapping.DataType == (int)ImportDetailsMapping.TargetDataType_OptionSet.Optonset) { bool matchFound = false; Dictionary <string, string> dcOptionSet = Common.FetchOptionSetList(service, mapping.TargetEntity, mapping.Target); KeyValuePair <string, string> optionSetVal = dcOptionSet.Where(x => x.Key.ToUpper() == mapping.value.ToUpper()).FirstOrDefault(); if (optionSetVal.Value != null) { entity[mapping.Target] = new OptionSetValue(Convert.ToInt32(optionSetVal.Value)); matchFound = true; } if (!matchFound) { entity[mapping.Target] = null; //Add Log To UpdateValidationMessage(); UpdateValidationMessage("Info: No Matching Optionset found in Target Entity " + mapping.TargetEntity + " in Target Field " + mapping.CrmDisplayName, ref errorMessage); } } else if (mapping.DataType == (int)ImportDetailsMapping.TargetDataType_OptionSet.Lookup) { string sourceValue = mapping.value; mapping.value = Common.GetId(mapping.LookupEntityName, mapping.LookupEntityAttribute, mapping, service); if (!string.IsNullOrEmpty(mapping.value)) { entity[mapping.Target] = new EntityReference(mapping.LookupEntityName, new Guid(mapping.value)); } else { //Add Log To UpdateValidationMessage(); UpdateValidationMessage("Info: No Matching Master Data found in Target Entity " + mapping.LookupEntityName + " For Target Field " + mapping.CrmDisplayName, ref errorMessage); } } else if (mapping.DataType == (int)ImportDetailsMapping.TargetDataType_OptionSet.TwoOptions) { if (mapping.value.Equals("Y", StringComparison.OrdinalIgnoreCase) || mapping.value.Equals("1", StringComparison.OrdinalIgnoreCase) || mapping.value.Equals("Yes", StringComparison.OrdinalIgnoreCase)) { entity[mapping.Target] = true; } else if (mapping.value.Equals("N", StringComparison.OrdinalIgnoreCase) || mapping.value.Equals("0", StringComparison.OrdinalIgnoreCase) || mapping.value.Equals("No", StringComparison.OrdinalIgnoreCase)) { entity[mapping.Target] = false; } else { //Add Log To UpdateValidationMessage(); UpdateValidationMessage("Info: No Matching Two Option found in Target Entity " + mapping.TargetEntity + " in Target Field " + mapping.CrmDisplayName, ref errorMessage); } } else if (mapping.DataType == (int)ImportDetailsMapping.TargetDataType_OptionSet.Currency) { //Replace $ and , with Space mapping.value = mapping.value.Replace('$', ' ').Replace(',', ' '); //Remove Space from string mapping.value = Regex.Replace(mapping.value, @"\s+", ""); Money currencyField = new Money(); currencyField.Value = Convert.ToDecimal(mapping.value); entity[mapping.Target] = currencyField; } } } catch (Exception ex) { //validationStatus-> False canReturn->true Add log UpdateValidationMessage UpdateValidationMessage("Error while setting value to Target " + ex.Message, ref errorMessage); ValidationStatus = false; canReturn = true; } }