Example #1
0
        public override void ExecuteExtention(BulkUpdateRequest request, BulkUpdateResponse response,
                                              ServiceRequestController controller)
        {
            var countToUpdate = request.RecordCount;
            var countUpdated  = 0;

            controller.UpdateProgress(0, countToUpdate, "Executing Updates");
            var estimator = new TaskEstimator(countToUpdate);

            foreach (var record in request.GetRecordsToUpdate())
            {
                try
                {
                    var newRecord = RecordService.NewRecord(request.RecordType.Key);
                    newRecord.Id = record.Id;
                    if (request.ClearValue)
                    {
                        newRecord.SetField(request.FieldToSet.Key, null, RecordService);
                    }
                    else
                    {
                        newRecord.SetField(request.FieldToSet.Key, request.ValueToSet, RecordService);
                    }
                    RecordService.Update(newRecord, new[] { request.FieldToSet.Key });
                }
                catch (Exception ex)
                {
                    response.AddResponseItem(new BulkUpdateResponseItem(record.Id, record.GetStringField(RecordService.GetPrimaryField(record.Type)), ex));
                }
                countUpdated++;
                controller.UpdateProgress(countUpdated, countToUpdate, estimator.GetProgressString(countUpdated, taskName: "Executing Updates"));
            }
        }
Example #2
0
        public override void ExecuteExtention(BulkDeleteRequest request, BulkDeleteResponse response,
                                              ServiceRequestController controller)
        {
            var countToUpdate = request.RecordCount;
            var countUpdated  = 0;

            controller.UpdateProgress(0, countToUpdate, "Executing Deletions");
            var estimator = new TaskEstimator(countToUpdate);

            foreach (var record in request.GetRecordsToUpdate())
            {
                try
                {
                    var newRecord = RecordService.NewRecord(request.RecordType.Key);
                    newRecord.Id = record.Id;
                    RecordService.Delete(newRecord);
                }
                catch (Exception ex)
                {
                    response.AddResponseItem(new BulkDeleteResponseItem(record.Id, record.GetStringField(RecordService.GetPrimaryField(record.Type)), ex));
                }
                countUpdated++;
                controller.UpdateProgress(countUpdated, countToUpdate, estimator.GetProgressString(countUpdated, taskName: "Executing Deletions"));
            }
        }
Example #3
0
        public override void ExecuteExtention(BulkReplaceRequest request, BulkReplaceResponse response,
                                              LogController controller)
        {
            var countToUpdate = request.RecordCount;
            var countUpdated  = 0;

            controller.UpdateProgress(0, countToUpdate, "Executing Replacements");
            var estimator = new TaskEstimator(countToUpdate);

            foreach (var record in request.GetRecordsToUpdate())
            {
                try
                {
                    var newRecord = RecordService.NewRecord(request.RecordType.Key);
                    newRecord.Id = record.Id;
                    var previousValue = record.GetStringField(request.FieldToReplaceIn.Key);
                    var newValue      = previousValue == null ? null : previousValue.Replace(request.OldValue, request.NewValue);
                    if (previousValue != newValue)
                    {
                        newRecord.SetField(request.FieldToReplaceIn.Key, newValue, RecordService);
                        RecordService.Update(newRecord, new[] { request.FieldToReplaceIn.Key });
                    }
                }
                catch (Exception ex)
                {
                    response.AddResponseItem(new BulkReplaceResponseItem(record.Id, record.GetStringField(RecordService.GetPrimaryField(record.Type)), ex));
                }
                countUpdated++;
                controller.UpdateProgress(countUpdated, countToUpdate, estimator.GetProgressString(countUpdated, taskName: "Executing Replacements"));
            }
        }
Example #4
0
        public override void ExecuteExtention(BulkCopyFieldValueRequest request, BulkCopyFieldValueResponse response,
                                              ServiceRequestController controller)
        {
            var countToUpdate = request.RecordCount;
            var countUpdated  = 0;

            controller.UpdateProgress(0, countToUpdate, "Executing Updates");
            var estimator = new TaskEstimator(countToUpdate);

            foreach (var record in request.GetRecordsToUpdate())
            {
                try
                {
                    var reloadRecord = RecordService.Get(record.Type, record.Id, new[] { request.SourceField.Key, request.TargetField.Key });
                    if ((request.CopyIfNull || reloadRecord.GetField(request.SourceField.Key) != null) &&
                        (request.OverwriteIfPopulated || reloadRecord.GetField(request.TargetField.Key) == null))
                    {
                        reloadRecord.SetField(request.TargetField.Key, reloadRecord.GetField(request.SourceField.Key), RecordService);
                        RecordService.Update(reloadRecord, new[] { request.TargetField.Key });
                    }
                }
                catch (Exception ex)
                {
                    response.AddResponseItem(new BulkCopyFieldValueResponseItem(record.Id, record.GetStringField(RecordService.GetPrimaryField(record.Type)), ex));
                }
                countUpdated++;
                controller.UpdateProgress(countUpdated, countToUpdate, estimator.GetProgressString(countUpdated, taskName: "Executing Updates"));
            }
            response.Message = "Copies Completed";
        }
        public void TaskEstimatorTestsTest()
        {
            var iterations    = 2;
            var taskEstimator = new TaskEstimator(iterations);

            for (var i = 0; i < iterations; i++)
            {
                Thread.Sleep(1000);
                Debug.WriteLine($"Remaining {taskEstimator.GetProgressString(i + 1, taskName: "Executing Test")}");
            }
        }
        public override void ExecuteExtention(BulkUpdateRequest request, BulkUpdateResponse response,
                                              ServiceRequestController controller)
        {
            var countToUpdate = request.RecordCount;
            var countUpdated  = 0;

            controller.UpdateProgress(0, countToUpdate, "Executing Updates");
            var estimator        = new TaskEstimator(countToUpdate);
            var recordsRemaining = request.GetRecordsToUpdate().ToList();

            while (recordsRemaining.Any())
            {
                controller.UpdateProgress(countUpdated, countToUpdate, estimator.GetProgressString(countUpdated, taskName: "Executing Updates"));

                var thisSetOfRecords = recordsRemaining
                                       .Take(request.ExecuteMultipleSetSize ?? 50)
                                       .ToList();

                recordsRemaining.RemoveRange(0, thisSetOfRecords.Count);

                var thisSetOfRecordsNew = thisSetOfRecords
                                          .Select(r =>
                {
                    var newRecord = RecordService.NewRecord(request.RecordType.Key);
                    newRecord.Id  = r.Id;
                    if (request.ClearValue)
                    {
                        newRecord.SetField(request.FieldToSet.Key, null, RecordService);
                    }
                    else
                    {
                        newRecord.SetField(request.FieldToSet.Key, request.ValueToSet, RecordService);
                    }
                    if (request.AddUpdateField2)
                    {
                        if (request.ClearValue2)
                        {
                            newRecord.SetField(request.FieldToSet2.Key, null, RecordService);
                        }
                        else
                        {
                            newRecord.SetField(request.FieldToSet2.Key, request.ValueToSet2, RecordService);
                        }
                    }
                    if (request.AddUpdateField3)
                    {
                        if (request.ClearValue3)
                        {
                            newRecord.SetField(request.FieldToSet3.Key, null, RecordService);
                        }
                        else
                        {
                            newRecord.SetField(request.FieldToSet3.Key, request.ValueToSet3, RecordService);
                        }
                    }
                    if (request.AddUpdateField4)
                    {
                        if (request.ClearValue4)
                        {
                            newRecord.SetField(request.FieldToSet4.Key, null, RecordService);
                        }
                        else
                        {
                            newRecord.SetField(request.FieldToSet4.Key, request.ValueToSet4, RecordService);
                        }
                    }
                    if (request.AddUpdateField5)
                    {
                        if (request.ClearValue5)
                        {
                            newRecord.SetField(request.FieldToSet5.Key, null, RecordService);
                        }
                        else
                        {
                            newRecord.SetField(request.FieldToSet5.Key, request.ValueToSet5, RecordService);
                        }
                    }
                    return(newRecord);
                })
                                          .ToArray();

                var errorsThisIteration = 0;

                //old versions dont have execute multiple so if 1 then do each request
                if (thisSetOfRecordsNew.Count() == 1)
                {
                    var record = thisSetOfRecordsNew.First();
                    try
                    {
                        RecordService.Update(record);
                    }
                    catch (Exception ex)
                    {
                        response.AddResponseItem(new BulkUpdateResponseItem(record.Id, record.GetStringField(RecordService.GetPrimaryField(record.Type)), ex));
                        errorsThisIteration++;
                    }
                }
                else
                {
                    var multipleResponse = RecordService.UpdateMultiple(thisSetOfRecordsNew);
                    foreach (var item in multipleResponse)
                    {
                        var originalRecord = thisSetOfRecords[item.Key];
                        response.AddResponseItem(new BulkUpdateResponseItem(originalRecord.Id, originalRecord.GetStringField(RecordService.GetPrimaryField(originalRecord.Type)), item.Value));
                    }
                    errorsThisIteration += multipleResponse.Count;
                }

                countUpdated                  += thisSetOfRecords.Count();
                response.NumberOfErrors       += errorsThisIteration;
                response.TotalRecordsProcessed = countUpdated;
            }
            controller.UpdateProgress(1, 1, "Updates Completed");
            response.Message = "Updates Completed";
        }
        public override void ExecuteExtention(BulkReplaceRequest request, BulkReplaceResponse response,
                                              ServiceRequestController controller)
        {
            var countToUpdate = request.RecordCount;
            var countUpdated  = 0;

            controller.UpdateProgress(0, countToUpdate, "Executing Replacements");
            var estimator = new TaskEstimator(countToUpdate);

            var recordsRemaining = request.GetRecordsToUpdate().ToList();

            while (recordsRemaining.Any())
            {
                controller.UpdateProgress(countUpdated, countToUpdate, estimator.GetProgressString(countUpdated, taskName: "Executing Replacements"));

                var thisSetOfRecords = recordsRemaining
                                       .Take(request.ExecuteMultipleSetSize ?? 50)
                                       .ToList();

                recordsRemaining.RemoveRange(0, thisSetOfRecords.Count);

                var thisSetOfRecordsNew = RecordService.GetMultiple(thisSetOfRecords.First().Type,
                                                                    thisSetOfRecords.Select(r => r.Id),
                                                                    request.FieldsToReplace.Select(f => f.RecordField.Key).ToArray())
                                          .ToArray();

                var errorsThisIteration = 0;

                var recordsToUpdate = new List <IRecord>();
                foreach (var record in thisSetOfRecordsNew)
                {
                    try
                    {
                        var newRecord = RecordService.NewRecord(record.Type);
                        newRecord.Id = record.Id;
                        var isUpdate = false;
                        foreach (var field in request.FieldsToReplace)
                        {
                            var previousValue = record.GetStringField(field.RecordField.Key);
                            var newValue      = previousValue;
                            foreach (var replacement in request.ReplacementTexts)
                            {
                                newValue = newValue == null ? null : newValue.Replace(replacement.OldText, replacement.NewText);
                            }
                            if (previousValue != newValue)
                            {
                                newRecord.SetField(field.RecordField.Key, newValue, RecordService);
                                isUpdate = true;
                            }
                        }
                        if (isUpdate)
                        {
                            recordsToUpdate.Add(newRecord);
                        }
                    }
                    catch (Exception ex)
                    {
                        errorsThisIteration++;
                        response.AddResponseItem(new BulkReplaceResponseItem(record.Id, record.GetStringField(RecordService.GetPrimaryField(record.Type)), ex));
                    }
                }

                //old versions dont have execute multiple so if 1 then do each request
                if (recordsToUpdate.Count() == 1)
                {
                    var record = recordsToUpdate.First();
                    try
                    {
                        RecordService.Update(record);
                    }
                    catch (Exception ex)
                    {
                        errorsThisIteration++;
                        response.AddResponseItem(new BulkReplaceResponseItem(record.Id, null, ex));
                    }
                }
                else
                {
                    var multipleResponse = RecordService.UpdateMultiple(recordsToUpdate);
                    foreach (var item in multipleResponse)
                    {
                        var originalRecord = recordsToUpdate[item.Key];
                        response.AddResponseItem(new BulkReplaceResponseItem(originalRecord.Id, null, item.Value));
                    }
                    errorsThisIteration += multipleResponse.Count;
                }

                countUpdated += thisSetOfRecords.Count();
                response.TotalRecordsProcessed = countUpdated;
                response.TotalRecordsUpdated  += recordsToUpdate.Count();
                response.NumberOfErrors       += errorsThisIteration;
            }

            controller.UpdateProgress(1, 1, "Replaces Completed");
            response.Message = "Replaces Completed";
        }
        public IEnumerable <DataImportResponseItem> DoImport(IEnumerable <Entity> entities, LogController controller, bool maskEmails)
        {
            controller.LogLiteral("Preparing Import");
            var response = new List <DataImportResponseItem>();

            var fieldsToRetry = new Dictionary <Entity, List <string> >();
            var typesToImport = entities.Select(e => e.LogicalName).Distinct();

            var allNNRelationships = XrmService.GetAllNnRelationshipEntityNames();
            var associationTypes   = typesToImport.Where(allNNRelationships.Contains).ToArray();

            typesToImport = typesToImport.Where(t => !associationTypes.Contains(t)).ToArray();

            var orderedTypes = new List <string>();

            var idSwitches = new Dictionary <string, Dictionary <Guid, Guid> >();

            foreach (var item in typesToImport)
            {
                idSwitches.Add(item, new Dictionary <Guid, Guid>());
            }

            #region tryordertypes

            //lets put team first because some other records
            //may reference the queue which only gets created
            //when the team does
            typesToImport = typesToImport.OrderBy(s => s == Entities.team ? 0 : 1).ToArray();

            foreach (var type in typesToImport)
            {
                //iterate through the types and if any of them have a lookup which references this type
                //then insert this one before it for import first
                //otherwise just append to the end
                foreach (var type2 in orderedTypes)
                {
                    var thatType         = type2;
                    var thatTypeEntities = entities.Where(e => e.LogicalName == thatType).ToList();
                    var fields           = GetFieldsToImport(thatTypeEntities, thatType)
                                           .Where(f => XrmService.FieldExists(f, thatType) && XrmService.IsLookup(f, thatType));

                    foreach (var field in fields)
                    {
                        if (thatTypeEntities.Any(e => e.GetLookupType(field) == type))
                        {
                            orderedTypes.Insert(orderedTypes.IndexOf(type2), type);
                            break;
                        }
                    }
                    if (orderedTypes.Contains(type))
                    {
                        break;
                    }
                }
                if (!orderedTypes.Contains(type))
                {
                    orderedTypes.Add(type);
                }
            }

            #endregion tryordertypes
            var estimator = new TaskEstimator(1);

            var countToImport = orderedTypes.Count;
            var countImported = 0;
            foreach (var recordType in orderedTypes)
            {
                try
                {
                    var thisRecordType = recordType;
                    var displayPrefix  = $"Importing {recordType} Records ({countImported + 1}/{countToImport})";
                    controller.UpdateProgress(countImported++, countToImport, string.Format("Importing {0} Records", recordType));
                    controller.UpdateLevel2Progress(0, 1, "Loading");
                    var primaryField     = XrmService.GetPrimaryNameField(recordType);
                    var thisTypeEntities = entities.Where(e => e.LogicalName == recordType).ToList();

                    var orConditions = thisTypeEntities
                                       .Select(
                        e =>
                        new ConditionExpression(XrmService.GetPrimaryKeyField(e.LogicalName),
                                                ConditionOperator.Equal, e.Id))
                                       .ToArray();
                    var existingEntities = XrmService.RetrieveAllOrClauses(recordType, orConditions);

                    var orderedEntities = new List <Entity>();

                    #region tryorderentities

                    var importFieldsForEntity = GetFieldsToImport(thisTypeEntities, recordType).ToArray();
                    var fieldsDontExist       = GetFieldsInEntities(thisTypeEntities)
                                                .Where(f => !XrmService.FieldExists(f, thisRecordType))
                                                .Where(f => !HardcodedIgnoreFields.Contains(f))
                                                .Distinct()
                                                .ToArray();
                    foreach (var field in fieldsDontExist)
                    {
                        response.Add(
                            new DataImportResponseItem(recordType, field, null,
                                                       string.Format("Field {0} On Entity {1} Doesn't Exist In Target Instance And Will Be Ignored", field, recordType),
                                                       new NullReferenceException(string.Format("Field {0} On Entity {1} Doesn't Exist In Target Instance And Will Be Ignored", field, recordType))));
                    }

                    var selfReferenceFields = importFieldsForEntity.Where(
                        f =>
                        XrmService.IsLookup(f, recordType) &&
                        XrmService.GetLookupTargetEntity(f, recordType) == recordType).ToArray();

                    foreach (var entity in thisTypeEntities)
                    {
                        foreach (var entity2 in orderedEntities)
                        {
                            if (selfReferenceFields.Any(f => entity2.GetLookupGuid(f) == entity.Id || (entity2.GetLookupGuid(f) == Guid.Empty && entity2.GetLookupName(f) == entity.GetStringField(primaryField))))
                            {
                                orderedEntities.Insert(orderedEntities.IndexOf(entity2), entity);
                                break;
                            }
                        }
                        if (!orderedEntities.Contains(entity))
                        {
                            orderedEntities.Add(entity);
                        }
                    }

                    #endregion tryorderentities

                    var countRecordsToImport = orderedEntities.Count;
                    var countRecordsImported = 0;
                    estimator = new TaskEstimator(countRecordsToImport);

                    foreach (var entity in orderedEntities)
                    {
                        var thisEntity = entity;
                        try
                        {
                            var existingMatchingIds = GetMatchForExistingRecord(existingEntities, thisEntity);
                            if (existingMatchingIds.Any())
                            {
                                var matchRecord = existingMatchingIds.First();
                                idSwitches[recordType].Add(thisEntity.Id, matchRecord.Id);
                                thisEntity.Id = matchRecord.Id;
                                thisEntity.SetField(XrmService.GetPrimaryKeyField(thisEntity.LogicalName), thisEntity.Id);
                            }
                            var isUpdate = existingMatchingIds.Any();
                            foreach (var field in thisEntity.GetFieldsInEntity().ToArray())
                            {
                                if (importFieldsForEntity.Contains(field) &&
                                    XrmService.IsLookup(field, thisEntity.LogicalName) &&
                                    thisEntity.GetField(field) != null)
                                {
                                    var idNullable = thisEntity.GetLookupGuid(field);
                                    if (idNullable.HasValue)
                                    {
                                        var targetTypesToTry = GetTargetTypesToTry(thisEntity, field);
                                        var name             = thisEntity.GetLookupName(field);
                                        var fieldResolved    = false;
                                        foreach (var lookupEntity in targetTypesToTry)
                                        {
                                            var targetPrimaryKey   = XrmRecordService.GetPrimaryKey(lookupEntity);
                                            var targetPrimaryField = XrmRecordService.GetPrimaryField(lookupEntity);
                                            var matchRecord        = XrmService.GetFirst(lookupEntity, targetPrimaryKey,
                                                                                         idNullable.Value);
                                            if (matchRecord != null)
                                            {
                                                thisEntity.SetLookupField(field, matchRecord);
                                                fieldResolved = true;
                                            }
                                            else
                                            {
                                                var matchRecords = name.IsNullOrWhiteSpace() ?
                                                                   new Entity[0] :
                                                                   GetMatchingEntities(lookupEntity,
                                                                                       targetPrimaryField,
                                                                                       name);
                                                if (matchRecords.Count() == 1)
                                                {
                                                    thisEntity.SetLookupField(field, matchRecords.First());
                                                    fieldResolved = true;
                                                }
                                            }
                                            if (!fieldResolved)
                                            {
                                                if (!fieldsToRetry.ContainsKey(thisEntity))
                                                {
                                                    fieldsToRetry.Add(thisEntity, new List <string>());
                                                }
                                                fieldsToRetry[thisEntity].Add(field);
                                            }
                                        }
                                    }
                                }
                            }
                            var fieldsToSet = new List <string>();
                            fieldsToSet.AddRange(thisEntity.GetFieldsInEntity()
                                                 .Where(importFieldsForEntity.Contains));
                            if (fieldsToRetry.ContainsKey(thisEntity))
                            {
                                fieldsToSet.RemoveAll(f => fieldsToRetry[thisEntity].Contains(f));
                            }

                            if (maskEmails)
                            {
                                var emailFields = new[] { "emailaddress1", "emailaddress2", "emailaddress3" };
                                foreach (var field in emailFields)
                                {
                                    var theEmail = thisEntity.GetStringField(field);
                                    if (!string.IsNullOrWhiteSpace(theEmail))
                                    {
                                        thisEntity.SetField(field, theEmail.Replace("@", "_AT_") + "*****@*****.**");
                                    }
                                }
                            }

                            if (isUpdate)
                            {
                                var existingRecord = existingMatchingIds.First();
                                XrmService.Update(thisEntity, fieldsToSet.Where(f => !XrmEntity.FieldsEqual(existingRecord.GetField(f), thisEntity.GetField(f))));
                            }
                            else
                            {
                                PopulateRequiredCreateFields(fieldsToRetry, thisEntity, fieldsToSet);
                                CheckThrowValidForCreate(thisEntity, fieldsToSet);
                                thisEntity.Id = XrmService.Create(thisEntity, fieldsToSet);
                            }
                            if (!isUpdate && thisEntity.GetOptionSetValue("statecode") > 0)
                            {
                                XrmService.SetState(thisEntity, thisEntity.GetOptionSetValue("statecode"), thisEntity.GetOptionSetValue("statuscode"));
                            }
                            else if (isUpdate && existingMatchingIds.Any())
                            {
                                var matchRecord = existingMatchingIds.First();
                                var thisState   = thisEntity.GetOptionSetValue("statecode");
                                var thisStatus  = thisEntity.GetOptionSetValue("statuscode");
                                var matchState  = matchRecord.GetOptionSetValue("statecode");
                                var matchStatus = matchRecord.GetOptionSetValue("statuscode");
                                if ((thisState != -1 && thisState != matchState) ||
                                    (thisStatus != -1 && thisState != matchStatus))
                                {
                                    XrmService.SetState(thisEntity, thisEntity.GetOptionSetValue("statecode"), thisEntity.GetOptionSetValue("statuscode"));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            if (fieldsToRetry.ContainsKey(thisEntity))
                            {
                                fieldsToRetry.Remove(thisEntity);
                            }
                            response.Add(
                                new DataImportResponseItem(recordType, null, entity.GetStringField(primaryField),
                                                           string.Format("Error Importing Record Id={0}", entity.Id),
                                                           ex));
                        }
                        countRecordsImported++;
                        controller.UpdateLevel2Progress(countRecordsImported, countRecordsToImport, estimator.GetProgressString(countRecordsImported));
                    }
                }
                catch (Exception ex)
                {
                    response.Add(
                        new DataImportResponseItem(recordType, null, null, string.Format("Error Importing Type {0}", recordType), ex));
                }
            }
            controller.TurnOffLevel2();
            countToImport = fieldsToRetry.Count;
            countImported = 0;
            controller.UpdateProgress(countImported, countToImport, "Retrying Unresolved Fields");
            estimator = new TaskEstimator(countToImport);
            foreach (var item in fieldsToRetry)
            {
                var thisEntity = item.Key;
                controller.UpdateProgress(countImported++, countToImport, string.Format("Retrying Unresolved Fields {0}", thisEntity.LogicalName));
                var thisPrimaryField = XrmService.GetPrimaryNameField(thisEntity.LogicalName);
                try
                {
                    foreach (var field in item.Value)
                    {
                        if (XrmService.IsLookup(field, thisEntity.LogicalName) && thisEntity.GetField(field) != null)
                        {
                            try
                            {
                                var targetTypesToTry = GetTargetTypesToTry(thisEntity, field);
                                var name             = thisEntity.GetLookupName(field);
                                var idNullable       = thisEntity.GetLookupGuid(field);
                                var fieldResolved    = false;
                                foreach (var lookupEntity in targetTypesToTry)
                                {
                                    var targetPrimaryKey   = XrmRecordService.GetPrimaryKey(lookupEntity);
                                    var targetPrimaryField = XrmRecordService.GetPrimaryField(lookupEntity);
                                    var matchRecord        = idNullable.HasValue ? XrmService.GetFirst(lookupEntity, targetPrimaryKey,
                                                                                                       idNullable.Value) : null;
                                    if (matchRecord != null)
                                    {
                                        thisEntity.SetLookupField(field, matchRecord);
                                        fieldResolved = true;
                                    }
                                    else
                                    {
                                        var matchRecords = name.IsNullOrWhiteSpace() ?
                                                           new Entity[0] :
                                                           GetMatchingEntities(lookupEntity,
                                                                               targetPrimaryField,
                                                                               name);
                                        if (matchRecords.Count() == 1)
                                        {
                                            thisEntity.SetLookupField(field, matchRecords.First());
                                            fieldResolved = true;
                                        }
                                    }
                                    if (!fieldResolved)
                                    {
                                        throw new Exception(string.Format("Could not find matching record for field {0}.{1} {2}", thisEntity.LogicalName, field, name));
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                if (thisEntity.Contains(field))
                                {
                                    thisEntity.Attributes.Remove(field);
                                }
                                response.Add(
                                    new DataImportResponseItem(thisEntity.LogicalName, field, thisEntity.GetStringField(thisPrimaryField),
                                                               string.Format("Error Setting Lookup Field Id={0}", thisEntity.Id), ex));
                            }
                        }
                    }
                    XrmService.Update(thisEntity, item.Value);
                }
                catch (Exception ex)
                {
                    response.Add(
                        new DataImportResponseItem(thisEntity.LogicalName, null, thisEntity.GetStringField(thisPrimaryField),
                                                   string.Format("Error Importing Record Id={0}", thisEntity.Id),
                                                   ex));
                }
                countImported++;
                controller.UpdateProgress(countImported, countToImport, estimator.GetProgressString(countImported, taskName: $"Retrying Unresolved Fields {thisEntity.LogicalName}"));
            }
            countToImport = associationTypes.Count();
            countImported = 0;
            foreach (var relationshipEntityName in associationTypes)
            {
                var thisEntityName = relationshipEntityName;
                controller.UpdateProgress(countImported++, countToImport, $"Associating {thisEntityName} Records");
                controller.UpdateLevel2Progress(0, 1, "Loading");
                var thisTypeEntities     = entities.Where(e => e.LogicalName == thisEntityName).ToList();
                var countRecordsToImport = thisTypeEntities.Count;
                var countRecordsImported = 0;
                estimator = new TaskEstimator(countRecordsToImport);
                foreach (var thisEntity in thisTypeEntities)
                {
                    try
                    {
                        var relationship = XrmService.GetRelationshipMetadataForEntityName(thisEntityName);
                        var type1        = relationship.Entity1LogicalName;
                        var field1       = relationship.Entity1IntersectAttribute;
                        var type2        = relationship.Entity2LogicalName;
                        var field2       = relationship.Entity2IntersectAttribute;

                        //bit of hack
                        //when importing from csv just set the fields to the string name of the referenced record
                        //so either string when csv or guid when xml import/export
                        var value1 = thisEntity.GetField(relationship.Entity1IntersectAttribute);
                        var id1    = value1 is string
                                     ?GetUniqueMatchingEntity(type1, XrmRecordService.GetPrimaryField(type1), (string)value1).Id
                                     : thisEntity.GetGuidField(relationship.Entity1IntersectAttribute);

                        var value2 = thisEntity.GetField(relationship.Entity2IntersectAttribute);
                        var id2    = value2 is string
                                     ?GetUniqueMatchingEntity(type2, XrmRecordService.GetPrimaryField(type2), (string)value2).Id
                                     : thisEntity.GetGuidField(relationship.Entity2IntersectAttribute);

                        //add a where field lookup reference then look it up
                        if (idSwitches.ContainsKey(type1) && idSwitches[type1].ContainsKey(id1))
                        {
                            id1 = idSwitches[type1][id1];
                        }
                        if (idSwitches.ContainsKey(type2) && idSwitches[type2].ContainsKey(id2))
                        {
                            id2 = idSwitches[type2][id2];
                        }
                        XrmService.AssociateSafe(relationship.SchemaName, type1, field1, id1, type2, field2, new[] { id2 });
                    }
                    catch (Exception ex)
                    {
                        response.Add(
                            new DataImportResponseItem(
                                string.Format("Error Associating Record Of Type {0} Id {1}", thisEntity.LogicalName,
                                              thisEntity.Id),
                                ex));
                    }
                    countRecordsImported++;
                    controller.UpdateLevel2Progress(countRecordsImported, countRecordsToImport, estimator.GetProgressString(countRecordsImported));
                }
            }
            return(response);
        }
Example #9
0
        public override void ExecuteExtention(BulkWorkflowRequest request, BulkWorkflowResponse response,
                                              ServiceRequestController controller)
        {
            var countToUpdate = request.RecordCount;
            var countUpdated  = 0;

            controller.UpdateProgress(0, countToUpdate, "Executing Workflows");
            var estimator        = new TaskEstimator(countToUpdate);
            var recordsRemaining = request.GetRecordsToUpdate().ToList();

            while (recordsRemaining.Any())
            {
                controller.UpdateProgress(countUpdated, countToUpdate, estimator.GetProgressString(countUpdated, taskName: "Executing Updates"));

                var thisSetOfRecords = recordsRemaining
                                       .Take(request.ExecuteMultipleSetSize ?? 50)
                                       .ToList();

                recordsRemaining.RemoveRange(0, thisSetOfRecords.Count);

                var errorsThisIteration = 0;

                //old versions dont have execute multiple so if 1 then do each request
                if (thisSetOfRecords.Count() == 1)
                {
                    var record = thisSetOfRecords.First();
                    try
                    {
                        RecordService.XrmService.Execute(new ExecuteWorkflowRequest
                        {
                            EntityId   = new Guid(record.Id),
                            WorkflowId = new Guid(request.Workflow.Id)
                        });
                    }
                    catch (Exception ex)
                    {
                        response.AddResponseItem(new BulkWorkflowResponseItem(record.Id, record.GetStringField(RecordService.GetPrimaryField(record.Type)), ex));
                        errorsThisIteration++;
                    }
                }
                else
                {
                    var requests = thisSetOfRecords.Select(r => new ExecuteWorkflowRequest
                    {
                        EntityId   = new Guid(r.Id),
                        WorkflowId = new Guid(request.Workflow.Id)
                    });
                    var multipleResponse = RecordService.XrmService.ExecuteMultiple(requests);
                    var key = 0;
                    foreach (var item in multipleResponse)
                    {
                        var originalRecord = thisSetOfRecords[key];
                        if (item.Fault != null)
                        {
                            response.AddResponseItem(new BulkWorkflowResponseItem(originalRecord.Id, originalRecord.GetStringField(RecordService.GetPrimaryField(originalRecord.Type)), new FaultException <OrganizationServiceFault>(item.Fault, item.Fault.Message)));
                            errorsThisIteration++;
                        }
                        key++;
                    }
                }

                countUpdated                  += thisSetOfRecords.Count();
                response.NumberOfErrors       += errorsThisIteration;
                response.TotalRecordsProcessed = countUpdated;

                Thread.Sleep(request.WaitPerMessage * 1000);
            }
            controller.UpdateProgress(1, 1, "All Workflows Requests Have Completed");
            response.Message = "Workflows Requests Completed";
        }
Example #10
0
        public override void ExecuteExtention(BulkCopyFieldValueRequest request, BulkCopyFieldValueResponse response,
                                              ServiceRequestController controller)
        {
            var countToUpdate = request.RecordCount;
            var countUpdated  = 0;

            controller.UpdateProgress(0, countToUpdate, "Executing Updates");
            var estimator = new TaskEstimator(countToUpdate);

            var recordsRemaining = request.GetRecordsToUpdate().ToList();

            while (recordsRemaining.Any())
            {
                controller.UpdateProgress(countUpdated, countToUpdate, estimator.GetProgressString(countUpdated, taskName: "Executing Updates"));

                var thisSetOfRecords = recordsRemaining
                                       .Take(request.ExecuteMultipleSetSize ?? 50)
                                       .ToList();

                recordsRemaining.RemoveRange(0, thisSetOfRecords.Count);

                var thisSetOfRecordsNew = RecordService.GetMultiple(thisSetOfRecords.First().Type,
                                                                    thisSetOfRecords.Select(r => r.Id),
                                                                    new[] { request.SourceField.Key, request.TargetField.Key })
                                          .ToArray();

                var recordsToUpdate = new List <IRecord>();
                foreach (var record in thisSetOfRecordsNew)
                {
                    try
                    {
                        var sourceValue           = record.GetField(request.SourceField.Key);
                        var targetValue           = record.GetField(request.TargetField.Key);
                        var parseSourceintoTarget = RecordService.ParseField(request.SourceField.Key, record.Type, sourceValue);
                        if (!record.FieldsEqual(request.TargetField.Key, parseSourceintoTarget))
                        {
                            if ((request.CopyIfNull || sourceValue != null) &&
                                (request.OverwriteIfPopulated || targetValue == null))
                            {
                                record.SetField(request.TargetField.Key, parseSourceintoTarget, RecordService);
                                recordsToUpdate.Add(record);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        response.AddResponseItem(new BulkCopyFieldValueResponseItem(record.Id, record.GetStringField(RecordService.GetPrimaryField(record.Type)), ex));
                    }
                }

                var errorsThisIteration = 0;

                //old versions dont have execute multiple so if 1 then do each request
                if (recordsToUpdate.Count() == 1)
                {
                    var record = recordsToUpdate.First();
                    try
                    {
                        RecordService.Update(record, new[] { request.TargetField.Key });
                    }
                    catch (Exception ex)
                    {
                        response.AddResponseItem(new BulkCopyFieldValueResponseItem(record.Id, record.GetStringField(RecordService.GetPrimaryField(record.Type)), ex));
                    }
                }
                else
                {
                    var multipleResponse = RecordService.UpdateMultiple(recordsToUpdate, new[] { request.TargetField.Key });
                    foreach (var item in multipleResponse)
                    {
                        var originalRecord = recordsToUpdate[item.Key];
                        response.AddResponseItem(new BulkCopyFieldValueResponseItem(originalRecord.Id, originalRecord.GetStringField(RecordService.GetPrimaryField(originalRecord.Type)), item.Value));
                    }
                    errorsThisIteration += multipleResponse.Count;
                }

                countUpdated += thisSetOfRecords.Count();
                response.TotalRecordsProcessed = countUpdated;
                response.TotalRecordsUpdated  += recordsToUpdate.Count();
                response.NumberOfErrors       += errorsThisIteration;
            }

            controller.UpdateProgress(1, 1, "Copies Completed");
            response.Message = "Copies Completed";
        }