Auto Number attributes in Microsoft Dynamics 365

With the release of Microsoft Dynamics 365 Customer Engagement October service update (v9.0), two of the most requested customization features were finally implemented in the platform:

  • Multi-select optionsets
  • Custom auto numbered attributes


The first one has full customization features available in the UI out of the box, but to create and manage auto numbering attributes you have to write code utilizing SDK-functions.

This article will describe how to use the Auto Number Manager tool for XrmToolBox giving an intuitive UI to access SDK-only features for auto numbering.

Auto Number Manager in XrmToolBox

Auto Number Features

The Auto Number attributes support three different types of placeholders – sequential numbers, random text, and date/time in different formats.

Syntax

Sequential number:  {SEQNUM:n} where n is the minimum number of digits.
Random text:  {RANDSTRING:n} where n is the number of characters in the random string. Maximum value for n is 6.
Date/time:  {DATETIMEUTC:fff} where fff is a standard datetime format string. See documentation.
Other text:    Any other text may be included in the number format. This text will be used as is.

Sample auto number formats:

  Number Format                                Example value
  CAR-{SEQNUM:3}-{RANDSTRING:6}                CAR-123-AB7LSF
  CNR-{RANDSTRING:4}-{SEQNUM:4}                CNR-WXYZ-1000
  {SEQNUM:6}-#-{RANDSTRING:3}                  123456-#-R3V
  KA-{SEQNUM:4}                                KA-0001
  {SEQNUM:10}                                  1234567890
  QUO-{SEQNUM:3}#{RANDSTRING:3}#{RANDSTRING:5} QUO-123#ABC#PQ2ST
  QUO-{SEQNUM:7}{RANDSTRING:5}                 QUO-0001000P9G3R
  CAS-{SEQNUM:6}-{DATETIMEUTC:yyyyMMddhhmmss}  CAS-002000-20170913091544
  CAS-{SEQNUM:6}-{DATETIMEUTC:yyyyMMddhh}      CAS-002002-2017091309

Installation

Update! XrmToolBox now supports SDK v9.0, so installation of Auto Number Manager is done through the Plugins Store in XrmToolBox.

The Auto Number Manager will be available in the Plugins Store of XrmToolBox once the SDK for Dynamics 365 v9.0 is released and XrmToolBox delivered using that version.Until then:

  1. Go to the Releases page on the GitHub repository: https://github.com/rappen/AutoNumManager/releases
  2. Download the ANM zip file (not the source code)
  3. Right-click the downloaded file and select Properties
  4. Tick the “Unblock” checkbox and click OK
  5. Open the zip file and copy all files and folders
  6. Open the folder where XrmToolBox is installed
  7. Paste the files from the zip
  8. Start XrmToolBox

The release package contains two SDK assemblies for v9.0, and a folder Plugins that contains the ANM plugin assembly.
Note: Information on how to install and run XrmToolBox, read the wiki: https://github.com/MscrmTools/XrmToolBox/wiki

Running ANM

Make sure you are connected to an organization with version 9.0 or above. Starting ANM connected to older versions will display an error message.

Opening ANM will load existing unmanaged solutions. Select the solution where you want to manage your auto number attributes.Selecting the solution will populate a list of entities included in the solution.

Select an entity, and any existing auto number attributes will be show in the table below. To create a new auto number attribute, click button New Attribute.

  • The prefix of the Logical Name is taken from the publisher of the selected solution, and cannot be changed.
  • Enter Logical Name, Display Name and Description for the attribute.
  • Make sure the Maximum Length is large enough to hold the generated auto number value. If the resulting string is too long, users will not be able to create new records.
  • The Language Id is retrieved from the current user, and is used to define for which language the Display Name and Description shall be created.
  • Number Format is the property where the magic is defined. See syntax and samples above. At the time of writing this article, it is somewhat unclear whether the SEQNUM placeholder should be required, but as this is the most probable definition, this is also implemented in ANM. Clicking the blue shortcuts below the number format will help composing the format.
  • The Seed property can be used to define the next value to be assigned to a new record for the SEQNUM placeholder of the auto number format. The seed can be set when creating a new auto number attribute, and when updating existing attributes.
  • To make it easier to create the Number Format, a Sample Number of the resulting values is displayed on the form.
  • Below the sample number a help text is displayed indicating if the Number Format is correctly formatted, or if there are any errors. This help text will let you know if the Maximum Length property is too small, as well as format errors.

The Techy Stuff

Metadata

Technically this is not a new attribute type. It is the same old “Single Line of Text” data type, with format “Text”. What has been added is a new property AutoNumberFormat on the StringAttributeMetadata class. This would indicate that existing attributes, both custom and standard, could be decorated with this new property to activate auto numbering for existing attributes. Initial tests succeed in doing that, but this is most probably a missing constraint in this version of the SDK, and should not be possible. ANM does therefore not support this action.

Stage

The platform core assigns the automatic numbers by an internal plugin at stage Initial Pre-operation (5). This means that custom plugins of all stages can read the assigned number. Another result of this is that custom plugins have no way of really knowing how the value was assigned – by the internal numbering or by programmatic creation of records.

Sequence counter

The technology behind the actual “counting” is based on the SQL Server SEQUENCE feature. There is currently no way of reading the current or last assigned value of the sequence, other than reading the most recently created record of the entity, and parsing the value of the numbered attribute.

Seed

The seed can be set to update the current value of the sequence number. This will, naturally, only affect records created after the seed has been updated. It MAY result in duplicated numbers, if the seed is set to a lower value than current sequence position.

Random characters

At this point it is not possible to define which character set the random string is generated from. This is entirely controlled by the core plugin assigning the auto numbers. But we sure do hope the SDK will be updated to allow us to define exactly which characters that can be included in the random strings!

Read more

After investigating the Auto Number features extensively, I wrote about some More Auto Numbering features.

 

Links

61 thoughts on “Auto Number attributes in Microsoft Dynamics 365

  1. I tired installing this Auto Number Manager tool for XrmToolBox but I get the below warring when adding from the plugins store. I have installed the latest version of XrmToolBox (v1.2017.7.18).

    "This plugin implements features form latest version of XrmToolBox. Please update your XrmToolBox to latest version to be able to install the plugin version."

    Please advise on possible cause of this warning and ways to troubleshoot it.

    1. Hi Patel,
      The official SDK for v9.0 is not released yet, so installation through XrmToolBox (that still use SDK v8.2) is not possible, that is why the warning is displayed.

      This is not mentioned in this article, but on the ANM webpage http://anm.xrmtoolbox.com you can read the following on how to install it before SDK and XTB is updated:

      NOTE!
      Until SDK for v9.0 is released and XrmToolBox updated to use it,
      beta version of Auto Number Manager can be downloaded from https://github.com/rappen/AutoNumManager/releases

      Let me know if this works or not 🙂

  2. Hi Jonas,

    I have installed ANM successfully and it works just fine.
    I'm using ANM with my Product entity and it will be used to automatically update the Product IDs when a record is created.
    I have installed the new attribute and ANM has created a new field and it is on my main form for the product entity. When I’m creating a new product, the new field works fine.
    So long, so good ��
    Inside the CRM there is the originally field for Product IDs on my existing products. The product IDs is filled with different values and there is no connection between the different product IDs. For example: one product ID is: Office 365 and next is PO-125499. Obviously, no cohesion between the existing product IDs.
    I’m using this number format: AA-{SEQNUM:5} with seed 1. So the structure are: AA-00001, AA-00002, AA-00003 and so on.
    So, my question is:
    How can I update all my existing products that already has the originally product ID into my new product ID structure on my new field?
    Best regards, Mathias

    1. Hi Mathias – so sorry about slow response time…
      The build-in feature for auto numbering only supports assigning numbers when records are created.
      So I guess you would have to write code to reassign numbers for existing records, and then reset the seed (current number) to make sure you do not get duplicates.

  3. Hi,

    I created one autonumber field to a custom entity. But when I import the solution to other CRM instance (v 9.0) auto number field does not work on new instance. Am I missing something? Where does/how does CRM manages the auto-calculation/auto-numbering…

    Any suggestion ?

    1. Microsoft have inexplicably not included the auto number format property in the customizations file when exporting a solution.
      This has been reported as a bug, I currently don't have information on when it will be fixed. But please open a support ticket for this – the more tickets they get, the faster it might get prioritized.

      Workaround for now is to apply the same format in target environment after solution is imported.

  4. HI Jon,Thanks for article and I tried through XRM-ANM created date field farmat and updated in my custom entity when I create on new record to create account .Its updating on save not on onload of record creation ..Any help much appreciated ….I working on crm365

    1. Hi,
      This is expected behavior, the Auto Numbering feature works server side, so it will generate the number according to the format when you save the record.
      /Jonas

  5. Hi Jonas, the text field created is not searchable neither is the option to make it searchable editable, is this a bug?

    Paul

    1. Hi Paul,
      Yes there is a bug that Microsoft have identified and fixed, it is waiting for release and might actually have been released if you are running the latest version online.
      Note that the bug is not in or caused by Auto Number Manager, it is a bug in core D365.
      There is an issue in the ANM repo regarding this, please update there if you have anything to add! https://github.com/rappen/AutoNumManager/issues/6

  6. Hi Jonas,
    I am a bit confused as to how is the seed property set. I have created an autonumber field with a {SEQNUM:10} format and it defaulted to a seed of 1000. Then I used your tool to set the seed to 0. I have noticed you are actually replacing the SEQNUM:10 to something like seed:000..00, but if I am trying to create the field directly with this format, it does not seem to work.
    Regards

    1. Hi Dark
      That sound strange… I just tried creating a new attribute with {SEQNUM:10} and no seed. Created a record, it got "0000001000". Updated the attribute with seed 0. Created a record, it got "0000000000". Created another, it got "0000000001".
      I don't quite get it what you mean that the number format is changed to "seed:000:00".
      Did you use this tool to create the number attribute from the beginning, or another tool?
      /Jonas

    2. Well, I wanted to check how you are setting the seed property and checked your code on github. Just looking thru the code I reached the point where you're setting up the seed and saw that you're replacing the format with the seed from the user followed by 0 replicated n times (based on initial format).
      Basically my question is: how do you set up the seed when first creating the autonumber field so it doesn't default to 1000?

    3. I think you are looking at the code where a sample result is composed.
      The setting of seed is done in this code.
      To set the seed to a specific value when creating a new attribute I just make sure to enter it in the Seed field here: see image.
      Hope this helps 🙂

    4. So there was a specific request for this :). I did look through the classes in the sdk.messages namespace but couldn't find anything relevant.
      Thank you for your guidance!
      Cheers

  7. Hi Jonas, great tool and thanks for creating it! I have it all working correctly on several entities, however the problem I face is every one of those attributes is now "hidden", in that I cannot add them to a form or a view? I found a workaround by creating the field, adding to a form, then enabling autonumbering. It doesnt look happy about being on the form afterwards but it does stay! Have you seen this behaviour before?

    Thanks,
    Nick

    1. Hi Nick – and thanks 🙂
      Which version (build) are you on?
      There was a bug (see issue #6) in the platform that was fixed with build 9.0.1.621 that caused this symptom.
      /Jonas

    2. Hi Jonas, we are on version 9.0.1.621, yet I still cannot see my autonumber fields to add to a view/form? Are there any steps I have to carry out now I am on this version? Thanks, Nick

  8. Hi Nick, you shouldn't have to do anything else to see the attribute…
    Have you tried just creating another one to see if that works?
    Also verify that both app and db versions are on build 621 or later, I have seen situations where only the app was updated.

    1. Hi Jonas, both the app and DB are on 621 (Version 1612 (9.0.1.621) (DB 9.0.1.621) – I will try creating another autonumber attribute and see if that now works. Thanks, Nick

  9. Hi Jonas, thanks for your help. It appears because I created the field while the Microsoft bug was in existence, it effectively broke all of my auto number fields, and I couldn't reference them anywhere. If I recreate them now, they all show correctly! Thanks, Nick

  10. Ha, I have not been quite so luck in implementing the fix, I cannot delete the attribute in Dynamics as I get a Generic SQL Error, I cannot remove autonumbering from the field in Auto Number Manager (it allows me and says successful, but when I go back to it it still has all the Autonumber seed properties.). When I try to delete the attribute through Auto Number Manager I get "System.Data.SqlClient.SqlException (0x80131904): The index 'ndx_QF_inc_referenceid' is dependent on column 'inc_referenceid'.

    Transaction (Process ID 169) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction."

    1. Indeed, I am one step ahead on that one! Just thought i would mention here in case it helps anyone else in the same boat. Thanks once again for your help.

  11. Hi Jonas. I created an autonumber field on a custom entity with a specific format and seed and when I imported the solution to another v9.0 instance, it works just perfectly fine even with the same number format but the seed is incorrect when I created a new record on the instance I imported the solution with. Instead of displaying abc-76585000 it displays abc-000001000. Any help on this? I know that we can reconfigure the autonumber again on the other instance before creating a record, but it would take a lot of work if a lot of autonumber attributes on different entities needs to be reconfigured.

    1. Hi Afafasf,
      To be honest it was news to me that the auto number format is actually now included in the solution file, MS must have fixed that quite recently. But I understand that the seed is not included, as it is just a method of saying what the next assigned value should be. It is not information stored on the attribute, this is just the "current value" on the Sequence way down in the SQL.
      I suggest you create an idea on the Ideas site to suggest MS implements this:
      https://experience.dynamics.com/ideas/

      /Jonas

      1. Hi,

        Does that mean that when I import the autonumber field to another CRM instance, the next value will be whatever the next value was in the instance where it is coming from or is going to start from 0?

        Thank you!

        1. Hi Mario,
          If it is a new attribute it will have next number = 1000 due to a rule implemented by Microsoft in the core of this feature, don’t ask me why they chose 1000…
          If it is an update to an existing attribute, it will use the next value.
          In one sense that is of course good, that solution import does not reset/overwrite current seed value as that could lead to inconsistent/duplicate numbers.

  12. Jonas, any idea how to get the current seed using metadata other than retrieving the entity records?
    The reason is I am a bit confused with seed. I set to 77, sometimes it starts from 1800077 but I set also 450, it starts from 1800451, so what this seeds mean? current or next number?

    thanks

    1. Hi Aileen,
      The SDK does not support getting current value/seed from auto number attributes.
      Technically it could be implemented, the technology behind the numbering is the Sequence feature in SQL Server, and that supports reading current value. But it is not exposed in the SDK.
      I encourage you to submit an idea for this on Ideas though 🙂
      Regarding the leading ‘1800’, I’d guess there is a ghost somewhere in your system… could there be a workflow or a plugin messing with this attribute?
      Jonas

  13. Hi Jonas,
    We created an autonumber for Contact and several other entities. Works great, but now we would like to change the format of C{SEQNUM:6} to C-{SEQNUM:6}. So including a dash in the number. But it is not possible to push this change to D365.
    After hitting “update” nothing happens and the format is changed back to the original.

    Please help.

    1. Hi Marcel,

      Does ANM react to the Update request at all, or exactly nothing happens?
      I know D365 sometimes has an issue with caching, so it takes “a while” for the change to have effect. Usually it is enough to close ANM and restart it again, and then the updated format will be visible to the tool.
      Can you check which build of D365 you are running?

      Jonas

  14. Hi Jonas, ANM is a great tool! Saved me from recreating the wheel, and so much more simple! Is there anyway to change within ANM the DATETIMEUTC to local time?

    Example: {DATETIMEUTC:MMddyy}-{DATETIMEUTC:hhmmss}
    Wanted outcome: EDT (032019-111401)
    Actual outcome: UTC (032019-031401)

    Thanks,
    DM831

  15. Hi Jonas, great tool! you saved me a huge time
    I setup a field as autonumber using your tool and now i want it reverse the operation, is there is a way to do that?
    i see the bouton delete but it doesn’t help because i want to keep the

    1. Hi Fouad, and thanks! ?
      You can absolutely remove the numbering by simply deleting the contents of the “Number Format” field and hitting the “Update” button.

      Jonas

  16. Hi Jonas. We recently implemented your autonumbering solution for a client using Dynamics 365 online for a custom entity. The sequence is skipping some numbers for example WH543113, WH543115 (WH543114 was skipped). This is happening randomly. Have you any idea what might be causing this or how to troubleshoot? We have auditing enabled and checked that records are not being deleted or deactivated.

    1. Hi Simon,
      Given the technology behind the auto numbering feature in the platform (SQL Sequence) I cannot see how it would be possible it skips numbers. I have seen this comment from someone else too, but we never managed to clearly prove that the platform feature actually skips. I simply should not be possible that numbers are assigned unless a new record is created.
      Could it be that you have some logic that overwrites the numbers for these “missing” ones to something else?

      Also note that my tool only consumes the APIs to configure the numbering, it is not “doing” the actual numbering.

      Jonas

  17. hi,

    I have a small doubt can any one please help me.
    create a plugin for updating account number with some random numbers in preoperation

    1. Hi Samba,

      Please elaborate – are you using the OOB feature to generate random auto numbers, or are you creating your own plugin?
      If the latter, please reach out on any of the Dynamics / Power Platforms forums available out there.

      Jonas

  18. Can I somehow set which records I want to have “autonumbered” programmatically? For example: Contact Type A I want to auto number. Contact Type B I do not.

    Great work btw!

    1. Hi Ykusuma,
      It is possible with some tricks, have a look at my other posts about Auto Number Manager: https://jonasr.app/tag/anm/
      None of them handle exactly your scenario, but should give you enough hints.
      Basically what you need to do is make sure the auto number field “contains a value” when the record hits the backend. Even setting an empty value is considered a value, and will prevent the numbering to be triggered.

  19. Hello Jonas, good post.
    I need this autonumber starts from 0 when month change.
    So if last number of October 2020 is 2020-10-157, first number of november should be 2020-11-001. Is this possible or not?

    1. Hi Carlo

      Unfortunately, that is not possible without some code written.
      The auto number feature in CDS is based on the Sequence functionality in SQL Server, and to reset that number you have to update the seed, which is currently only possible through the API or by manually setting it in the Maker Portal.

      Jonas

  20. Hi Jonas,

    First of all, thank you for your support. When I make automatic number identification, the last number increases by 2 more. What I want is 1 more increase. I defined the number of steps as 6. The first auto number I created was 001002, rather than 000001. What is the reason of this ? Can you support?

    Thanks, regards.

    1. Hi Onur,
      Actually the auto numbering feature in the platform does not support other increments than 1 at a time, so my only guess would be that you have some logic creating copies of your new records when creating them.
      Regarding the starting number, the OOB feature starts with a seed of 1000. You can change the seed using my Auto Number Manager tool. This and other features are described in this short tutorial: https://jonasr.app/xtb-school/anm

      Jonas

  21. Hi Jonas,
    Suppose in production like environment I use ANM to stop autonumbering for one of custom Autonumber field by clearing the Number Format for some period of time (we will be migrating records) and once the migration is completed we again set the seed and enable Autonumber through ANM again … So will this introduce an unmanaged layer on the top of that Autonumber field?

  22. Hi Jonas,
    How to package the seed from Dev to production. Is that possible.
    For example, I want my order number to be ABCD-10000001.
    I am able to specify the seed in Dev, but its not showing in production, after export and import.

    Please advise.
    Thanks and Regards
    George

    1. Sorry, no way. MS makes it impossible.
      Or if your prefix is “ABCD-1” and number is “0000001”, if you never need to flip the “1” 😏

  23. Jonas! How is your Health? I’ve been praying for your recovery!

    This tool has literally saved my butt this morning! I’ve sent you (the tan guy) a small donation in express of my Gratitude!

    1. Thanks Angie! 🙏
      I’m ok, but recovery takes looong time. I just want to be back soon…
      See here to read more: https://stroke.jonasr.app/

      And thanks for donating! But actually I am not the tan guy – so if you want to donate to the creators for the future reference: open the tool, then click this: 😊
      Donate Auto Number Manager

  24. Hi Jonas, I have one problem. If I put the number value in the format {RANDSTRING:1}{RANDSTRING:1}{RANDSTRING:1}{SEQNUM:4}, then in the sample number I get this example: XXX0001. But in the CRM itself I get in the X0001 format. Why can it cut off the first two {RANDSTRING:1}{RANDSTRING:1}.
    Thank you!

    1. (Sorry, late response…)

      Just wondering – why don’t you just use one {RANDSTRING:3} instead of three {RANDSTRING:1} ?
      There may be limits in Dataverse that you can only use one RANDSTRING… That’s my only idea.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.