At times we may have a requirement to update the format of the Auto Number dynamically / programmatically so that the new set of records take up the new format.
Let us understand through a simple example.
Right now for the below field, we have the auto-number format set as
{SEQNUM:4}

Below is the sample code to update the auto-number format programmatically.
It makes use of our RetrieveAttribute and UpdateAttribute requests.
We have retrieved the attribute then have specified a new auto number format, followed by update request.

After executing the above code successfully, and creating a new contact record we can see the new number generated as per the specified format.

Sample Code –
string ConnectionString = "AuthType = OAuth; " +
"AppId=51f81489-12ee-4a9e-aaae-a2591f45987d; " +
"Username=test@test.onmicrosoft.com; " +
"Password=test; " +
"RedirectUri=app://58145B91-0C36-4500-8554-080854F2AC97;" +
"Url = https://test.crm.dynamics.com/;";
CrmServiceClient svc = new CrmServiceClient(ConnectionString);
if (svc.IsReady)
{
var retAttributeRequest = ((RetrieveAttributeResponse)
svc.Execute(new RetrieveAttributeRequest
{
EntityLogicalName = "contact",
LogicalName = "cr59f_myautonumber",
RetrieveAsIfPublished = true
})).AttributeMetadata;
retAttributeRequest.AutoNumberFormat = "MyFormat-{SEQNUM:6}-{DATETIMEUTC:yyyyMMddhh}-{RANDSTRING:6}";
var updateAttrRequest = new UpdateAttributeRequest
{
EntityName = "contact",
Attribute = retAttributeRequest
};
var updateAttrResponse = (UpdateAttributeResponse)
svc.Execute(updateAttrRequest);
Check other posts on Auto Number attribute –
- How to – Use SetAutoNumberSeed Action to reset/update the seed for the Autonumber attribute
- Few Key Points – Auto Number Field
- Set Auto Number field’s value conditionally.
Hope it helps..
One thought on “Sample Code – Update format of Autonumber attribute in Dataverse (Dynamics 365 / CRM)”