Beispiel #1
0
        private int FormatAndSetAutonumber(Entity entity, int?currentPosition, int numberOfCharacters, string prefix,
                                           string separator, XrmAutonumber autonumber)
        {
            if (!currentPosition.HasValue)
            {
                currentPosition = 1;
            }
            else
            {
                currentPosition = currentPosition.Value + 1;
            }
            object newValue = null;

            if (autonumber.AutonumberFieldType == OptionSets.Autonumber.AutonumberFieldType.String)
            {
                string autonumberString;
                if (numberOfCharacters > 0)
                {
                    autonumberString = prefix + separator +
                                       currentPosition.Value.ToString("D" + numberOfCharacters);
                }
                else
                {
                    autonumberString = prefix + separator + currentPosition;
                }
                newValue = autonumberString;
            }
            else
            {
                newValue = currentPosition;
            }
            entity.SetField(autonumber.AutonumberField, newValue);
            return(currentPosition.Value);
        }
Beispiel #2
0
        /// <summary>
        ///     Gets the previous child autonumber of this records parent then sets this records autonumber to the next logical
        ///     value
        /// </summary>
        private void SetParentedAutonumber(Entity entity, XrmAutonumber autonumber)
        {
            var parentRecord = GetParentEntity(entity, autonumber, new[] { autonumber.ParentAutonumberField });

            if (parentRecord != null && parentRecord.Contains(autonumber.ParentAutonumberField))
            {
                var maxId = 0;

                var parentNumber = GetNumberPartFromParent(parentRecord, autonumber);

                var parentPrefix = autonumber.AutonumberPrefix + autonumber.ParentSeparator + parentNumber + autonumber.Separator;

                var conditions = new[]
                {
                    new ConditionExpression(autonumber.AutonumberField, ConditionOperator.BeginsWith,
                                            parentPrefix)
                };
                var getHighestExistingQuery = XrmService.BuildQueryActive(autonumber.EntityType,
                                                                          new[] { autonumber.AutonumberField },
                                                                          conditions, null);
                getHighestExistingQuery.AddOrder(autonumber.AutonumberField, OrderType.Descending);
                var highestExisting = XrmService.RetrieveFirst(getHighestExistingQuery);

                if (highestExisting != null)
                {
                    var id = highestExisting.GetStringField(autonumber.AutonumberField);
                    maxId = int.Parse(id.Substring(id.LastIndexOf(autonumber.Separator) + 1));
                }

                if ((maxId + 1).ToString().Length > autonumber.AutonumberCharacters)
                {
                    //if the next autonumber length is greater than the configured length then we cannot rely on a string sort (e.g. 999>1000)
                    //in this case we need to get all, parse into an int and then sort descending
                    var query = new QueryExpression(entity.LogicalName);
                    query.ColumnSet.AddColumn(autonumber.AutonumberField);
                    query.Criteria.AddCondition(autonumber.AutonumberField, ConditionOperator.BeginsWith,
                                                autonumber.AutonumberPrefix + autonumber.ParentSeparator + parentNumber + autonumber.Separator);
                    var allSameParentIds = XrmService.RetrieveAll(query);
                    var autonumbers      = new List <int>();
                    foreach (var item in allSameParentIds)
                    {
                        var id = item.GetStringField(autonumber.AutonumberField);
                        autonumbers.Add(int.Parse(id.Substring(id.LastIndexOf(autonumber.Separator) + 1)));
                    }
                    if (autonumbers.Count > 0)
                    {
                        autonumbers.Sort();
                        maxId = autonumbers.Last();
                    }
                }
                var newId = autonumber.AutonumberPrefix + autonumber.ParentSeparator + parentNumber + autonumber.Separator +
                            (maxId + 1).ToString("D" + autonumber.AutonumberCharacters);
                entity.SetField(autonumber.AutonumberField, newId);
            }
            else
            {
                throw new InvalidPluginExecutionException(
                          "Autonumber error - this records autonumber is configured to include the parent records autonumber but no parent autonumber value was found");
            }
        }
Beispiel #3
0
        public string GetNumberPartFromThisType(Entity entity, XrmAutonumber autonumber)
        {
            var temp = entity.GetStringField(autonumber.AutonumberField);

            if (string.IsNullOrWhiteSpace(temp))
            {
                throw new NullReferenceException("Error The Parent Autonumber Is empty");
            }

            var startIndex = temp.IndexOfAny(new[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });

            return(temp.Substring(startIndex));
        }
Beispiel #4
0
 /// <summary>
 ///     Sets the autonumber fields configured for this record type with the next logical value
 /// </summary>
 public void SetAutonumber(Entity entity, XrmAutonumber autonumber)
 {
     //lock the autonumber record for the current plugin
     XrmService.SetField(Entities.jmcg_autonumber, autonumber.AutonumberId, Fields.jmcg_autonumber_.modifiedon, DateTime.UtcNow);
     if (autonumber.HasParent)
     {
         SetParentedAutonumber(entity, autonumber);
     }
     else
     {
         //for child autonumber need to refresh the autonumber record for the current position
         SetIndividualAutonumber(entity, autonumber);
     }
 }
Beispiel #5
0
 public void ValidateLinksToParent(XrmAutonumber autonumber)
 {
     try
     {
         var dummyEntity = new Entity(autonumber.EntityType);
         var firstLookup = autonumber.FirstLinkLookup;
         if (!XrmService.FieldExists(firstLookup, autonumber.EntityType))
         {
             throw new NullReferenceException(string.Format("There Is No Field Named {0} On The {1} Record Type", firstLookup, autonumber.EntityType));
         }
         if (XrmService.IsLookup(firstLookup, autonumber.EntityType))
         {
             dummyEntity.SetLookupField(firstLookup, Guid.Empty, autonumber.FirstLinkLookup);
             GetParentEntity(dummyEntity, autonumber, new string[0]);
         }
     }
     catch (Exception ex)
     {
         throw new Exception(string.Format("There Was An Error Validating The {0} Field - {1}", XrmService.GetFieldLabel(Fields.jmcg_autonumber_.jmcg_parentautonumberlinks, Entities.jmcg_autonumber), ex.Message), ex);
     }
 }
Beispiel #6
0
        /// <summary>
        ///     Sets this records autonumber field and increments the counter in the autonumber configuration
        /// </summary>
        private void SetIndividualAutonumber(Entity entity, XrmAutonumber autonumber)
        {
            var currentPosition = (int?)
                                  XrmService.LookupField(Entities.jmcg_autonumber, autonumber.AutonumberId,
                                                         Fields.jmcg_autonumber_.jmcg_currentnumberposition);

            currentPosition = FormatAndSetAutonumber(entity, currentPosition, autonumber.AutonumberCharacters, autonumber.AutonumberPrefix, autonumber.Separator, autonumber);
            XrmService.SetField(Entities.jmcg_autonumber, autonumber.AutonumberId,
                                Fields.jmcg_autonumber_.jmcg_currentnumberposition, currentPosition);
            //var useAutonumber = !PrefixFromParent;
            //if (PrefixFromParent)
            //{
            //    //need to get the parent records prefix
            //    var parentRecord = GetParentEntity(entity, service);
            //    if (parentRecord == null && IfNotLinkedUseAutonumberConfig)
            //        useAutonumber = true;
            //    else
            //    {
            //        if (parentRecord == null)
            //            throw new NullReferenceException(
            //                string.Format(
            //                    "Error Generating {0}. The {0} For Field {1} Is Configured To Autonumber From A Parent Record But The Links To That Parent Did Not Return A Record"
            //                    , "Autonumber", service.GetFieldLabel(AutonumberFieldName, EntityType)));
            //        //lock for the autonumber
            //        service.SetField(parentRecord.LogicalName, parentRecord.Id, "modifiedon", DateTime.Now);
            //        parentRecord = GetParentEntity(entity, service);
            //        //then get the current max
            //        var prefix = !ParentPrefixField.IsNullOrWhiteSpace() &&
            //                     !parentRecord.GetStringField(ParentPrefixField).IsNullOrWhiteSpace()
            //            ? parentRecord.GetStringField(ParentPrefixField)
            //            : AutonumberPrefix;
            //        var separator = !ParentSeparatorField.IsNullOrWhiteSpace() &&
            //                        !parentRecord.GetStringField(ParentSeparatorField).IsNullOrWhiteSpace()
            //            ? parentRecord.GetStringField(ParentSeparatorField)
            //            : Separator;
            //        var numberOfCharacters = !ParentNumberOfCharactersField.IsNullOrWhiteSpace() &&
            //                                 parentRecord.GetField(ParentPrefixField) != null
            //            ? parentRecord.GetField(ParentNumberOfCharactersField)
            //            : AutonumberCharacters;
            //        if (numberOfCharacters == null)
            //            numberOfCharacters = AutonumberCharacters;
            //        if (ParentCurrentPositionField.IsNullOrWhiteSpace())
            //            throw new NullReferenceException(string.Format(
            //                "Error Generating {0}. The {0} For Field {1} Is Configured To Autonumber From A Parent Record But {2} Is Empty On The {0} Record"
            //                , "Autonumber", service.GetFieldLabel(AutonumberFieldName, EntityType),
            //                service.GetFieldLabel(AutonumberFields.ParentCurrentPositionField,
            //                    AutonumberFields.AutonumberEntityName)));
            //        var currentPosition = (int?)parentRecord.GetField(ParentCurrentPositionField);
            //        currentPosition = FormatAndSetAutonumber(entity, currentPosition, (int)numberOfCharacters, prefix,
            //            separator);
            //        parentRecord.SetField(ParentCurrentPositionField, currentPosition);
            //        service.Update(parentRecord, new[] { ParentCurrentPositionField });
            //    }
            //}
            //if (useAutonumber)
            //{
            //    var prefix = AutonumberPrefix;
            //    var separator = Separator;
            //    var numberOfCharacters = AutonumberCharacters;
            //    var currentPosition = (int?)
            //            service.LookupField(autonumberFields.AutonumberEntityName, AutonumberId,
            //                autonumberFields.AutonumberEntityCurrentPositionField);
            //    currentPosition = FormatAndSetAutonumber(entity, currentPosition, numberOfCharacters, prefix, separator);
            //    service.SetField(autonumberFields.AutonumberEntityName, AutonumberId,
            //        autonumberFields.AutonumberEntityCurrentPositionField, currentPosition);
            //}
        }
Beispiel #7
0
        /// <summary>
        ///     Return the entity record which is configured as the logical parent of this records autonumber
        /// </summary>
        public Entity GetParentEntity(Entity entity, XrmAutonumber autonumber, IEnumerable <string> fields)
        {
            //Need to split the links to the parent and create a query which returns the parent record via the links
            if (!entity.Contains(autonumber.FirstLinkLookup))
            {
                return(null);
            }

            //Create a query which traverses through the keys and entities we defined in our autonumber links
            var query = new QueryExpression();

            query.ColumnSet = XrmService.CreateColumnSet(fields);
            LinkEntity carry = null;

            //for each foreign key entity pairing work from last to first
            for (var i = autonumber.LinksToParent.LinkToParents.Count(); i > 0; i--)
            {
                var thisLink = autonumber.LinksToParent.LinkToParents.ElementAt(i - 1);
                //if this is the last item we need to create it as the type of entity we are returning
                if (i == autonumber.LinksToParent.LinkToParents.Count())
                {
                    query.EntityName = thisLink.LinkTarget;
                }
                //otherwise if this is not the last item we need to add a link from the previous type to this type
                else
                {
                    var previousPair = autonumber.LinksToParent.LinkToParents.ElementAt(i);
                    if (carry == null)
                    {
                        carry = query.AddLink(thisLink.LinkTarget, previousPair.LinkFieldTarget, previousPair.LinkFieldSource);
                    }
                    else
                    {
                        carry = carry.AddLink(thisLink.LinkTarget, previousPair.LinkFieldTarget, previousPair.LinkFieldSource);
                    }
                }
                //if this is the first item we need to add a filter on the first id with the value in the lookup from the record we are creating the autonumber for
                if (i == 1)
                {
                    var thisLookupId = entity.GetLookupGuid(thisLink.LinkFieldSource);
                    if (!thisLookupId.HasValue)
                    {
                        return(null);
                    }
                    if (autonumber.LinksToParent.LinkToParents.Count() != 1)
                    {
                        carry.LinkCriteria.AddCondition(carry.LinkToEntityName + "id", ConditionOperator.Equal, thisLookupId.Value);
                    }
                    else
                    {
                        query.Criteria.AddCondition(query.EntityName + "id", ConditionOperator.Equal, thisLookupId.Value);
                    }
                }
            }
            //Run the query and if a result return it
            var parent = XrmService.RetrieveMultiple(query);

            if (parent.Entities.Count > 0)
            {
                return(parent.Entities[0]);
            }
            return(null);
        }
        public string GetNumberPartFromThisType(Entity entity, XrmAutonumber autonumber)
        {
            var temp = entity.GetStringField(autonumber.AutonumberField);
            if (string.IsNullOrWhiteSpace(temp))
                throw new NullReferenceException("Error The Parent Autonumber Is empty");

            var startIndex = temp.IndexOfAny(new[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
            return temp.Substring(startIndex);
        }
 public void ValidateLinksToParent(XrmAutonumber autonumber)
 {
     try
     {
         var dummyEntity = new Entity(autonumber.EntityType);
         var firstLookup = autonumber.FirstLinkLookup;
         if (!XrmService.FieldExists(firstLookup, autonumber.EntityType))
             throw new NullReferenceException(string.Format("There Is No Field Named {0} On The {1} Record Type", firstLookup, autonumber.EntityType));
         if (XrmService.IsLookup(firstLookup, autonumber.EntityType))
         {
             dummyEntity.SetLookupField(firstLookup, Guid.Empty, autonumber.FirstLinkLookup);
             GetParentEntity(dummyEntity, autonumber, new string[0]);
         }
     }
     catch (Exception ex)
     {
         throw new Exception(string.Format("There Was An Error Validating The {0} Field - {1}", XrmService.GetFieldLabel(Fields.jmcg_autonumber_.jmcg_parentautonumberlinks, Entities.jmcg_autonumber), ex.Message), ex);
     }
 }
 private int FormatAndSetAutonumber(Entity entity, int? currentPosition, int numberOfCharacters, string prefix,
     string separator, XrmAutonumber autonumber)
 {
     if (!currentPosition.HasValue)
         currentPosition = 1;
     else
         currentPosition = currentPosition.Value + 1;
     object newValue = null;
     if (autonumber.AutonumberFieldType == OptionSets.Autonumber.AutonumberFieldType.String)
     {
         string autonumberString;
         if (numberOfCharacters > 0)
             autonumberString = prefix + separator +
                                currentPosition.Value.ToString("D" + numberOfCharacters);
         else
             autonumberString = prefix + separator + currentPosition;
         newValue = autonumberString;
     }
     else
     {
         newValue = currentPosition;
     }
     entity.SetField(autonumber.AutonumberField, newValue);
     return currentPosition.Value;
 }
 /// <summary>
 ///     Sets this records autonumber field and increments the counter in the autonumber configuration
 /// </summary>
 private void SetIndividualAutonumber(Entity entity, XrmAutonumber autonumber)
 {
     var currentPosition = (int?)
             XrmService.LookupField(Entities.jmcg_autonumber, autonumber.AutonumberId,
                 Fields.jmcg_autonumber_.jmcg_currentnumberposition);
     currentPosition = FormatAndSetAutonumber(entity, currentPosition, autonumber.AutonumberCharacters, autonumber.AutonumberPrefix, autonumber.Separator, autonumber);
     XrmService.SetField(Entities.jmcg_autonumber, autonumber.AutonumberId,
         Fields.jmcg_autonumber_.jmcg_currentnumberposition, currentPosition);
     //var useAutonumber = !PrefixFromParent;
     //if (PrefixFromParent)
     //{
     //    //need to get the parent records prefix
     //    var parentRecord = GetParentEntity(entity, service);
     //    if (parentRecord == null && IfNotLinkedUseAutonumberConfig)
     //        useAutonumber = true;
     //    else
     //    {
     //        if (parentRecord == null)
     //            throw new NullReferenceException(
     //                string.Format(
     //                    "Error Generating {0}. The {0} For Field {1} Is Configured To Autonumber From A Parent Record But The Links To That Parent Did Not Return A Record"
     //                    , "Autonumber", service.GetFieldLabel(AutonumberFieldName, EntityType)));
     //        //lock for the autonumber
     //        service.SetField(parentRecord.LogicalName, parentRecord.Id, "modifiedon", DateTime.Now);
     //        parentRecord = GetParentEntity(entity, service);
     //        //then get the current max
     //        var prefix = !ParentPrefixField.IsNullOrWhiteSpace() &&
     //                     !parentRecord.GetStringField(ParentPrefixField).IsNullOrWhiteSpace()
     //            ? parentRecord.GetStringField(ParentPrefixField)
     //            : AutonumberPrefix;
     //        var separator = !ParentSeparatorField.IsNullOrWhiteSpace() &&
     //                        !parentRecord.GetStringField(ParentSeparatorField).IsNullOrWhiteSpace()
     //            ? parentRecord.GetStringField(ParentSeparatorField)
     //            : Separator;
     //        var numberOfCharacters = !ParentNumberOfCharactersField.IsNullOrWhiteSpace() &&
     //                                 parentRecord.GetField(ParentPrefixField) != null
     //            ? parentRecord.GetField(ParentNumberOfCharactersField)
     //            : AutonumberCharacters;
     //        if (numberOfCharacters == null)
     //            numberOfCharacters = AutonumberCharacters;
     //        if (ParentCurrentPositionField.IsNullOrWhiteSpace())
     //            throw new NullReferenceException(string.Format(
     //                "Error Generating {0}. The {0} For Field {1} Is Configured To Autonumber From A Parent Record But {2} Is Empty On The {0} Record"
     //                , "Autonumber", service.GetFieldLabel(AutonumberFieldName, EntityType),
     //                service.GetFieldLabel(AutonumberFields.ParentCurrentPositionField,
     //                    AutonumberFields.AutonumberEntityName)));
     //        var currentPosition = (int?)parentRecord.GetField(ParentCurrentPositionField);
     //        currentPosition = FormatAndSetAutonumber(entity, currentPosition, (int)numberOfCharacters, prefix,
     //            separator);
     //        parentRecord.SetField(ParentCurrentPositionField, currentPosition);
     //        service.Update(parentRecord, new[] { ParentCurrentPositionField });
     //    }
     //}
     //if (useAutonumber)
     //{
     //    var prefix = AutonumberPrefix;
     //    var separator = Separator;
     //    var numberOfCharacters = AutonumberCharacters;
     //    var currentPosition = (int?)
     //            service.LookupField(autonumberFields.AutonumberEntityName, AutonumberId,
     //                autonumberFields.AutonumberEntityCurrentPositionField);
     //    currentPosition = FormatAndSetAutonumber(entity, currentPosition, numberOfCharacters, prefix, separator);
     //    service.SetField(autonumberFields.AutonumberEntityName, AutonumberId,
     //        autonumberFields.AutonumberEntityCurrentPositionField, currentPosition);
     //}
 }
        /// <summary>
        ///     Gets the previous child autonumber of this records parent then sets this records autonumber to the next logical
        ///     value
        /// </summary>
        private void SetParentedAutonumber(Entity entity, XrmAutonumber autonumber)
        {
            var parentRecord = GetParentEntity(entity, autonumber, new[] { autonumber.ParentAutonumberField });
            if (parentRecord != null && parentRecord.Contains(autonumber.ParentAutonumberField))
            {
                var maxId = 0;

                var parentNumber = GetNumberPartFromParent(parentRecord, autonumber);

                var parentPrefix = autonumber.AutonumberPrefix + autonumber.ParentSeparator + parentNumber + autonumber.Separator;

                var conditions = new[]
                {
                    new ConditionExpression(autonumber.AutonumberField, ConditionOperator.BeginsWith,
                        parentPrefix)
                };
                var getHighestExistingQuery = XrmService.BuildQueryActive(autonumber.EntityType,
                    new[] { autonumber.AutonumberField },
                    conditions, null);
                getHighestExistingQuery.AddOrder(autonumber.AutonumberField, OrderType.Descending);
                var highestExisting = XrmService.RetrieveFirst(getHighestExistingQuery);

                if (highestExisting != null)
                {
                    var id = highestExisting.GetStringField(autonumber.AutonumberField);
                    maxId = int.Parse(id.Substring(id.LastIndexOf(autonumber.Separator) + 1));
                }

                if ((maxId + 1).ToString().Length > autonumber.AutonumberCharacters)
                {
                    //if the next autonumber length is greater than the configured length then we cannot rely on a string sort (e.g. 999>1000)
                    //in this case we need to get all, parse into an int and then sort descending
                    var query = new QueryExpression(entity.LogicalName);
                    query.ColumnSet.AddColumn(autonumber.AutonumberField);
                    query.Criteria.AddCondition(autonumber.AutonumberField, ConditionOperator.BeginsWith,
                        autonumber.AutonumberPrefix + autonumber.ParentSeparator + parentNumber + autonumber.Separator);
                    var allSameParentIds = XrmService.RetrieveAll(query);
                    var autonumbers = new List<int>();
                    foreach (var item in allSameParentIds)
                    {
                        var id = item.GetStringField(autonumber.AutonumberField);
                        autonumbers.Add(int.Parse(id.Substring(id.LastIndexOf(autonumber.Separator) + 1)));
                    }
                    if (autonumbers.Count > 0)
                    {
                        autonumbers.Sort();
                        maxId = autonumbers.Last();
                    }
                }
                var newId = autonumber.AutonumberPrefix + autonumber.ParentSeparator + parentNumber + autonumber.Separator +
                            (maxId + 1).ToString("D" + autonumber.AutonumberCharacters);
                entity.SetField(autonumber.AutonumberField, newId);
            }
            else
            {
                throw new InvalidPluginExecutionException(
                    "Autonumber error - this records autonumber is configured to include the parent records autonumber but no parent autonumber value was found");
            }
        }
 /// <summary>
 ///     Sets the autonumber fields configured for this record type with the next logical value
 /// </summary>
 public void SetAutonumber(Entity entity, XrmAutonumber autonumber)
 {
     //lock the autonumber record for the current plugin
     XrmService.SetField(Entities.jmcg_autonumber, autonumber.AutonumberId, Fields.jmcg_autonumber_.modifiedon, DateTime.UtcNow);
     if (autonumber.HasParent)
     {
         SetParentedAutonumber(entity, autonumber);
     }
     else
     {
         //for child autonumber need to refresh the autonumber record for the current position
         SetIndividualAutonumber(entity, autonumber);
     }
 }
        /// <summary>
        ///     Return the entity record which is configured as the logical parent of this records autonumber
        /// </summary>
        public Entity GetParentEntity(Entity entity, XrmAutonumber autonumber, IEnumerable<string> fields)
        {
            //Need to split the links to the parent and create a query which returns the parent record via the links
            if (!entity.Contains(autonumber.FirstLinkLookup))
                return null;

            //Create a query which traverses through the keys and entities we defined in our autonumber links
            var query = new QueryExpression();
            query.ColumnSet = XrmService.CreateColumnSet(fields);
            LinkEntity carry = null;
            //for each foreign key entity pairing work from last to first
            for (var i = autonumber.LinksToParent.LinkToParents.Count(); i > 0; i--)
            {
                var thisLink = autonumber.LinksToParent.LinkToParents.ElementAt(i - 1);
                //if this is the last item we need to create it as the type of entity we are returning
                if (i == autonumber.LinksToParent.LinkToParents.Count())
                    query.EntityName = thisLink.LinkTarget;
                //otherwise if this is not the last item we need to add a link from the previous type to this type
                else
                {
                    var previousPair = autonumber.LinksToParent.LinkToParents.ElementAt(i);
                    if (carry == null)
                        carry = query.AddLink(thisLink.LinkTarget, previousPair.LinkFieldTarget, previousPair.LinkFieldSource);
                    else
                        carry = carry.AddLink(thisLink.LinkTarget, previousPair.LinkFieldTarget, previousPair.LinkFieldSource);
                }
                //if this is the first item we need to add a filter on the first id with the value in the lookup from the record we are creating the autonumber for
                if (i == 1)
                {
                    var thisLookupId = entity.GetLookupGuid(thisLink.LinkFieldSource);
                    if (!thisLookupId.HasValue)
                        return null;
                    if (autonumber.LinksToParent.LinkToParents.Count() != 1)
                        carry.LinkCriteria.AddCondition(carry.LinkToEntityName + "id", ConditionOperator.Equal, thisLookupId.Value);
                    else
                        query.Criteria.AddCondition(query.EntityName + "id", ConditionOperator.Equal, thisLookupId.Value);
                }
            }
            //Run the query and if a result return it
            var parent = XrmService.RetrieveMultiple(query);
            if (parent.Entities.Count > 0)
                return parent.Entities[0];
            return null;
        }