Esempio n. 1
0
        internal static AutoNumbering GetAutoNumberingConfig(Entity target, string config,
                                                             IPluginExecutionContext context, IOrganizationService service, CrmLog log, out bool isBackLogged)
        {
            context.Require(nameof(context));

            var xrmContext = new XrmServiceContext(service)
            {
                MergeOption = MergeOption.NoTracking
            };

            var configIds      = config.Split(',').Select(item => item.Trim()).ToArray();
            var isInlineConfig = config.Contains(";;");

            AutoNumbering autoNumberConfig = null;

            if (isInlineConfig)
            {
                autoNumberConfig = GetInlineConfig(config, context.UserId);
            }

            // get it once to check for generator field update below
            log.Log("Getting auto-numbering config ...");

            // if a condition is found in any of the configs, then don't throw an error if none match
            // it simply means that the user wants the auto-numbering to work only if the condition is satisified
            // this is useful for multiple fields
            var isConditioned = false;

            // if using a backlog, then no need to lock
            isBackLogged = false;

            if (!isInlineConfig)
            {
                var autoNumberConfigTemp = GetBackloggedConfig(context, service, log);

                if (autoNumberConfigTemp == null)
                {
                    foreach (var configId in configIds)
                    {
                        autoNumberConfigTemp =
                            (from autoNumberQ in xrmContext.AutoNumberingSet
                             where autoNumberQ.UniqueID == configId || autoNumberQ.Name == configId &&
                             autoNumberQ.Status == AutoNumbering.StatusEnum.Active
                             select autoNumberQ).FirstOrDefault();

                        if (autoNumberConfigTemp == null)
                        {
                            continue;
                        }

                        if (autoNumberConfigTemp.Condition != null)
                        {
                            isConditioned = true;
                            log.Log($"Checking condition for '{autoNumberConfigTemp.Name}':'{autoNumberConfigTemp.Id}' ...");
                            var parsedCondition = ParseAttributeVariables(service, autoNumberConfigTemp.Condition, target,
                                                                          context.UserId, context.OrganizationId.ToString());
                            var isConditionMet = IsConditionMet(service, parsedCondition,
                                                                target.ToEntityReference(), context.OrganizationId.ToString());

                            if (isConditionMet)
                            {
                                log.Log("Condition met for auto-numbering record.");
                                autoNumberConfig = autoNumberConfigTemp;
                                break;
                            }

                            log.Log("Condition not met for auto-numbering record.");
                        }
                        else if (autoNumberConfig == null)
                        {
                            autoNumberConfig = autoNumberConfigTemp;
                        }
                    }
                }
                else
                {
                    autoNumberConfig = autoNumberConfigTemp;
                    isBackLogged     = true;
                }
            }

            return(PreValidation(service, target, autoNumberConfig, log, isConditioned, isBackLogged));
        }