Beispiel #1
0
        public void SetSeed(string entityName, string attributeName, long value, bool force = false)
        {
            SetAutoNumberSeedRequest req = new SetAutoNumberSeedRequest
            {
                EntityName    = entityName,
                AttributeName = attributeName,
                Value         = value
            };

            OrganizationService.Execute(req);
        }
Beispiel #2
0
        public override void ExecuteExtention(ConfigureAutonumberRequest request, ConfigureAutonumberResponse response,
                                              ServiceRequestController controller)
        {
            controller.UpdateProgress(0, 5, "Processing Field Update");
            //okay we need to update the autonumber
            var fieldName  = request.Field?.Key;
            var recordType = request.RecordType?.Key;
            var xrmService = XrmRecordService.XrmService;

            controller.UpdateProgress(1, 5, "Loading Field metadata");
            var stringFieldMetadata = xrmService.GetFieldMetadata(fieldName, recordType) as StringAttributeMetadata;

            if (stringFieldMetadata == null)
            {
                throw new Exception($"Field {fieldName} In {recordType} Is Not Of Type {nameof(StringAttributeMetadata)}");
            }

            //apparently if null doesnt apply the clear so lets set as empty string if null
            request.AutonumberFormat = request.AutonumberFormat ?? string.Empty;

            if (stringFieldMetadata.AutoNumberFormat != request.AutonumberFormat)
            {
                //okay I have noted that the autonumber does not seem to always apply immediatelly
                //that is when querying the metadata after setting the autonumber config
                //sometimes it does not return the applied value
                //so lets do wait/retry

                var numberOfSecondsToCheckUpdated = 4;
                var retryAfterNumberOfChecks      = 3;
                var numberOfRetriesLeft           = 3;

                var isUpdated = false;
                while (true)
                {
                    controller.UpdateProgress(2, 5, "Setting Format");
                    stringFieldMetadata.AutoNumberFormat = request.AutonumberFormat;
                    xrmService.CreateOrUpdateAttribute(fieldName, recordType, stringFieldMetadata);
                    controller.UpdateProgress(3, 5, "Publishing");
                    var publishXml = $"<importexportxml><entities><entity>{recordType}</entity></entities></importexportxml>";
                    xrmService.Publish(publishXml);

                    for (var i = 0; i < retryAfterNumberOfChecks; i++)
                    {
                        var getAttributeResponse = (RetrieveAttributeResponse)xrmService.Execute(new RetrieveAttributeRequest()
                        {
                            EntityLogicalName = request.RecordType.Key,
                            LogicalName       = request.Field.Key
                        });
                        var stringAttributeMetadata = getAttributeResponse.AttributeMetadata as StringAttributeMetadata;
                        if (stringAttributeMetadata.AutoNumberFormat == request.AutonumberFormat)
                        {
                            isUpdated = true;
                            response.FormatUpdated = true;
                            xrmService.SetFieldMetadataCache(request.RecordType.Key, request.Field.Key, stringAttributeMetadata);
                            break;
                        }

                        Thread.Sleep(numberOfSecondsToCheckUpdated * 1000);
                    }
                    if (isUpdated)
                    {
                        break;
                    }

                    numberOfRetriesLeft--;
                    if (numberOfRetriesLeft == 0)
                    {
                        throw new Exception("The Autonumber Configuration May Not be Applied. The New Format Was Sent To The Web Service Several Times But Dynamics Has Not Applied The Settings According To The Metadata Provided By The Web Service. Perhaps Retry");
                    }
                }
            }

            if (request.SetSeed.HasValue)
            {
                controller.UpdateProgress(4, 5, "Updating Seed Field Update");
                var req = new SetAutoNumberSeedRequest
                {
                    AttributeName = fieldName,
                    EntityName    = recordType,
                    Value         = request.SetSeed.Value
                };
                xrmService.Execute(req);
                response.SeedUpdated = true;
            }

            controller.UpdateProgress(5, 5, "Finishing");
        }