private Entity GetParentEntity(IPluginContext context, Entity entity, AutoNumberingRule rule)
        {
            if (!rule.IsParented())
            {
                return(null);
            }

            if (rule.ParentAttributeName.IsNullOrEmpty())
            {
                throw new InvalidPluginExecutionException("Invalid auto-numbering configuration. Missing parent attribute name.");
            }

            if (rule.LastNumberAttributeName.IsNullOrEmpty())
            {
                throw new InvalidPluginExecutionException("Invalid auto-numbering configuration. Missing last number attribute name.");
            }

            var parentReference = entity.GetAttributeValue <EntityReference>(rule.ParentAttributeName);

            if (parentReference == null)
            {
                throw new InvalidPluginExecutionException($"You must provide a value for '{rule.ParentAttributeName}'.");
            }

            var parentAttributes = rule.ParentAttributes
                                   .Union(new[] { rule.LastNumberAttributeName, rule.LastYearAttributeName, rule.LastDayAttributeName })
                                   .Where(o => !o.IsNullOrEmpty())
                                   .Distinct()
                                   .ToArray();

            var parent = context.OrganizationService.Retrieve(
                parentReference.LogicalName,
                parentReference.Id,
                parentAttributes);

            return(parent);
        }
        private Int32 GetLastNumber(IPluginContext context, AutoNumberingRule rule, Entity parent, DateTime today)
        {
            var entityReference     = default(EntityReference);
            var lastNumberAttribute = default(String);
            var lastYearAttribute   = default(String);
            var lastDayAttribute    = default(String);

            if (rule.IsParented())
            {
                entityReference = parent.ToEntityReference();

                lastNumberAttribute = rule.LastNumberAttributeName;
                lastYearAttribute   = rule.LastYearAttributeName;
                lastDayAttribute    = rule.LastDayAttributeName;
            }
            else
            {
                entityReference = rule.ToEntityReference();

                lastNumberAttribute = AutoNumberingRuleEntity.LastNumberFieldName(_prefix);
                lastYearAttribute   = AutoNumberingRuleEntity.LastYearFieldName(_prefix);
                lastDayAttribute    = AutoNumberingRuleEntity.LastDayFieldName(_prefix);
            }

            if (lastNumberAttribute.IsNullOrEmpty())
            {
                throw new InvalidPluginExecutionException("Invalid auto-numbering configuration. Missing last number attribute name.");
            }

            if (rule.IsYearly() && lastYearAttribute.IsNullOrEmpty())
            {
                throw new InvalidPluginExecutionException("Invalid auto-numbering configuration. Missing last year attribute name.");
            }

            if (rule.IsDaily() && lastDayAttribute.IsNullOrEmpty())
            {
                throw new InvalidPluginExecutionException("Invalid auto-numbering configuration. Missing last day attribute name.");
            }

            var attributes = new[]
            {
                lastNumberAttribute,
                lastYearAttribute,
                lastDayAttribute
            };

            var entity = context.OrganizationService.Retrieve(
                entityReference.LogicalName,
                entityReference.Id,
                attributes.Where(o => !o.IsNullOrEmpty()).ToArray());

            var number = entity.GetAttributeValue <Int32?>(lastNumberAttribute);

            if (rule.IsYearly() && today.Year != entity.GetAttributeValue <Int32?>(lastYearAttribute))
            {
                return(0);
            }
            else if (rule.IsDaily() && today != entity.GetAttributeValue <DateTime?>(lastDayAttribute))
            {
                return(0);
            }

            return(number ?? 0);
        }