0

I have already implement auto number with plugin, and it is through another entity type to ensure all operations are in one transaction. but I have another question, that is can we use process lock(eg. mutex) to fix it? this will more flexible than before, isn't it?

Richard
  • 53
  • 7
  • here is my old resolution https://www.akaes.com/blog/how-to-implement-robust-auto-numbering-using-transactions-in-microsoft-dynamics-crm/ – Richard Apr 23 '18 at 07:59

2 Answers2

1

Dynamics 365 has native support for auto-numbering fields since v9.0, with the minor inconvenience of having to manipulate them through code only. Unless it's a broken feature (it happens), there's no need for custom autonumbers anymore.

https://learn.microsoft.com/en-us/dynamics365/customer-engagement/developer/create-auto-number-attributes

Example:

CreateAttributeRequest widgetSerialNumberAttributeRequest = new CreateAttributeRequest
{
    EntityName = "newWidget",
    Attribute = new StringAttributeMetadata
    {
        //Define the format of the attribute
        AutoNumberFormat = "WID-{SEQNUM:5}-{RANDSTRING:6}-{DATETIMEUTC:yyyyMMddhhmmss}",
        LogicalName = "new_serialnumber",
        SchemaName = "new_SerialNumber",
        RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
        MaxLength = 100, // The MaxLength defined for the string attribute must be greater than the length of the AutoNumberFormat value, that is, it should be able to fit in the generated value.
        DisplayName = new Label("Serial Number", 1033),
        Description = new Label("Serial Number of the widget.", 1033)
    }
};
_serviceProxy.Execute(widgetSerialNumberAttributeRequest);

Docs point out that

The sequential segment is generated by SQL and hence uniqueness is guaranteed by SQL.

XrmToolbox should have a plugin to manage auto number fields (thus making it easier), but I haven't tried it.

Alex
  • 23,004
  • 4
  • 39
  • 73
  • yes, thanks Alex, I known it, but for my project I have a more common function to wrap it, so that I just do some configurations it will be ok. – Richard Apr 24 '18 at 02:03
  • also, I just want to know a approach to fix this kind of issues, the auto number just a classic issue for concurrent in dynamics. – Richard Apr 24 '18 at 02:05
0

Plugin's can be run on multiple machines concurrently depending on your installation - that's why the entity (database) lock is regularly used.

James Wood
  • 17,286
  • 4
  • 46
  • 89
  • ah, thanks James a lot. Mutex just work in one machine to control multiple processes, I forgot the NLB environment.. thanks. – Richard Apr 24 '18 at 02:22