Exemplo n.º 1
0
        private void ImportRecord(DynamicEntity entity)
        {
            string statecode  = "";
            int    statuscode = -1;

            if (entity.Properties.Contains("statecode"))
            {
                statecode = entity["statecode"].ToString();
                entity.Properties.Remove("statecode");
            }

            if (entity.Properties.Contains("statuscode"))
            {
                statuscode = ((Status)entity["statuscode"]).Value;
                entity.Properties.Remove("statuscode");
            }

            string primaryAttribute      = MetadataUtility.RetrievePrimaryAttribute(_metadataService, entity.Name);
            string primaryAttributeValue = entity.Properties.Contains(primaryAttribute) ? entity[primaryAttribute].ToString() : "";
            Guid   id                  = entity.Properties.OfType <KeyProperty>().First().Value.Value;
            Guid   potentialNewId      = Guid.Empty;
            int    matchingRecordCount = GetMatchingRecordCount(entity.Name, id, primaryAttributeValue, ref potentialNewId);

            switch (matchingRecordCount)
            {
            case 0:
                _crmService.Create(entity);
                ReportDetail(String.Format("Created {0} with id of {1}.", entity.Name, id));
                break;

            case 1:
                if (potentialNewId != Guid.Empty)
                {
                    id = potentialNewId;
                    entity.Properties.OfType <KeyProperty>().First().Value.Value = potentialNewId;
                }
                _crmService.Update(entity);
                ReportDetail(String.Format("Updated {0} with id of {1}.", entity.Name, id));
                break;

            default:
                ReportError(String.Format("Cannot import {0} with id of {1} because it matches more than one record in the target system based on attribute {2}.", entity.Name, id, primaryAttribute));
                break;
            }

            if (matchingRecordCount < 2 && !String.IsNullOrEmpty(statecode))
            {
                DynamicEntityUtility.SetStateDynamicEntity(_crmService, entity.Name, id, statecode, statuscode);
                ReportDetail(String.Format("Set state on {0} with id of {1}.", entity.Name, id));
            }

            _currentRecordCount++;
            _entityRecordCounts[entity.Name]++;

            int percent = (int)Math.Round(100.0 * _currentRecordCount / _totalRecordCount);

            percent = percent > 100 ? 100 : percent;
            OnImportProgressChanged(new ProgressChangedEventArgs(percent, null));
        }
Exemplo n.º 2
0
        private int GetMatchingRecordCount(string entityName, Guid id, string primaryAttributeValue, ref Guid newReferenceId)
        {
            List <DynamicEntity> ret = new List <DynamicEntity>(DynamicEntityUtility.GetByIdOrPrimaryAttribute(_crmService, _metadataService, entityName, id, primaryAttributeValue));

            if (ret.Count == 0)
            {
            }
            else if (ret.Count == 1)
            {
                Guid returnValueKey = ret[0].Properties.OfType <KeyProperty>().First().Value.Value;
                if (returnValueKey != id)
                {
                    newReferenceId = returnValueKey;
                }
            }
            return(ret.Count);
        }