public QueryExpression GetMatchQueryExpression(Entity thisEntity)
        {
            var thisTypesConfig = XrmRecordService.GetTypeConfigs().GetFor(thisEntity.LogicalName);

            if (thisTypesConfig != null)
            {
                var matchQuery = XrmService.BuildQuery(thisTypesConfig.Type, null, null, null);
                var parentAndUniqueFieldsToMatch = new List <string>();
                if (thisTypesConfig.ParentLookupField != null)
                {
                    parentAndUniqueFieldsToMatch.Add(thisTypesConfig.ParentLookupField);
                }
                if (thisTypesConfig.UniqueChildFields != null)
                {
                    parentAndUniqueFieldsToMatch.AddRange(thisTypesConfig.UniqueChildFields);
                }
                if (!parentAndUniqueFieldsToMatch.Any())
                {
                    throw new Exception($"Type {thisTypesConfig.Type} has a type config but neither of {nameof(TypeConfigs.Config.ParentLookupField)} or {nameof(TypeConfigs.Config.UniqueChildFields)} has fields configured for matching");
                }

                AddUniqueFieldConfigJoins(thisEntity, matchQuery, parentAndUniqueFieldsToMatch);
                return(matchQuery);
            }
            else
            {
                var primaryKey  = XrmService.GetPrimaryKeyField(thisEntity.LogicalName);
                var primaryName = XrmService.GetPrimaryNameField(thisEntity.LogicalName);
                var matchQuery  = XrmService.BuildQuery(thisEntity.LogicalName, null, null, null);
                if (AltMatchKeyDictionary.ContainsKey(thisEntity.LogicalName))
                {
                    var matchKeyFieldDictionary = AltMatchKeyDictionary[thisEntity.LogicalName]
                                                  .Distinct().ToDictionary(f => f, f => thisEntity.GetField(f));
                    matchQuery.Criteria.Conditions.AddRange(matchKeyFieldDictionary.Select(kv =>
                                                                                           new ConditionExpression(kv.Key, ConditionOperator.Equal, XrmService.ConvertToQueryValue(kv.Key, thisEntity.LogicalName, kv.Value))));
                }
                else if (MatchOption == MatchOption.PrimaryKeyThenName || thisTypesConfig != null)
                {
                    matchQuery.Criteria.FilterOperator = LogicalOperator.Or;
                    matchQuery.Criteria.Conditions.Add(
                        new ConditionExpression(primaryKey, ConditionOperator.Equal, thisEntity.Id));
                    if (primaryName != null && thisEntity.GetStringField(primaryName) != null)
                    {
                        matchQuery.Criteria.Conditions.Add(
                            new ConditionExpression(primaryName, ConditionOperator.Equal, thisEntity.GetStringField(primaryName)));
                    }
                }
                else if (MatchOption == MatchOption.PrimaryKeyOnly)
                {
                    matchQuery.Criteria.Conditions.Add(
                        new ConditionExpression(primaryKey, ConditionOperator.Equal, thisEntity.Id));
                }
                return(matchQuery);
            }
        }
예제 #2
0
        public QueryExpression GetMatchQueryExpression(Entity thisEntity)
        {
            var thisTypesConfig = XrmRecordService.GetTypeConfigs().GetFor(thisEntity.LogicalName);

            if (thisTypesConfig != null)
            {
                var matchQuery = XrmService.BuildQuery(thisTypesConfig.Type, null, null, null);
                var parentAndUniqueFieldsToMatch = new List <string>();
                if (thisTypesConfig.ParentLookupField != null)
                {
                    parentAndUniqueFieldsToMatch.Add(thisTypesConfig.ParentLookupField);
                }
                if (thisTypesConfig.UniqueChildFields != null)
                {
                    parentAndUniqueFieldsToMatch.AddRange(thisTypesConfig.UniqueChildFields);
                }
                if (!parentAndUniqueFieldsToMatch.Any())
                {
                    throw new Exception($"Type {thisTypesConfig.Type} has a type config but neither of {nameof(TypeConfigs.Config.ParentLookupField)} or {nameof(TypeConfigs.Config.UniqueChildFields)} has fields configured for matching");
                }

                AddUniqueFieldConfigJoins(thisEntity, matchQuery, parentAndUniqueFieldsToMatch);
                return(matchQuery);
            }
            else
            {
                var primaryKey  = XrmService.GetPrimaryKeyField(thisEntity.LogicalName);
                var primaryName = XrmService.GetPrimaryNameField(thisEntity.LogicalName);
                var matchQuery  = XrmService.BuildQuery(thisEntity.LogicalName, null, null, null);
                if (AltMatchKeyDictionary.ContainsKey(thisEntity.LogicalName))
                {
                    var matchKeyFieldDictionary = AltMatchKeyDictionary[thisEntity.LogicalName]
                                                  .Distinct().ToDictionary(f => f, f => thisEntity.GetField(f));

                    foreach (var matchKeyField in matchKeyFieldDictionary)
                    {
                        if (matchKeyField.Value is EntityReference er &&
                            er.Id == Guid.Empty &&
                            !string.IsNullOrWhiteSpace(er.Name) &&
                            !string.IsNullOrWhiteSpace(er.LogicalName) &&
                            XrmService.EntityExists(er.LogicalName))
                        {
                            var linkTo = matchQuery.AddLink(er.LogicalName, matchKeyField.Key, XrmService.GetPrimaryKeyField(er.LogicalName));
                            linkTo.LinkCriteria.AddCondition(new ConditionExpression(XrmService.GetPrimaryNameField(er.LogicalName), ConditionOperator.Equal, er.Name));
                        }
                        else
                        {
                            matchQuery.Criteria.Conditions.Add(new ConditionExpression(matchKeyField.Key, ConditionOperator.Equal, XrmService.ConvertToQueryValue(matchKeyField.Key, thisEntity.LogicalName, matchKeyField.Value)));
                        }
                    }
                }
예제 #3
0
        public void LogEntityError(Entity entity, Exception ex)
        {
            if (_fieldsToRetry.ContainsKey(entity))
            {
                _fieldsToRetry.Remove(entity);
                Response.RemoveFieldForRetry(entity);
            }
            var field = AltMatchKeyDictionary.ContainsKey(entity.LogicalName)
                ? string.Join("|", AltMatchKeyDictionary[entity.LogicalName])
                : null;
            var value = AltMatchKeyDictionary.ContainsKey(entity.LogicalName)
                ? string.Join("|", AltMatchKeyDictionary[entity.LogicalName].Select(k => XrmService.GetFieldAsDisplayString(entity.LogicalName, k, entity.GetField(k))))
                : null;
            var rowNumber = entity.Contains("Sheet.RowNumber")
                ? entity.GetInt("Sheet.RowNumber")
                : (int?)null;
            var primaryField = XrmService.GetPrimaryNameField(entity.LogicalName);

            Response.AddImportError(entity,
                                    new DataImportResponseItem(entity.LogicalName, field, entity.GetStringField(primaryField), value,
                                                               ex.Message + (entity.Id != Guid.Empty ? " Id=" + entity.Id : ""),
                                                               ex, rowNumber: rowNumber));
        }