Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

auto numbering cwa pulls same number twice. With code

(0) ShareShare
ReportReport
Posted on by 219

Hello together,

I wrote a cwa which creates an autonumber for an entity x. the CWA runs in a syncron workflow when an account is created or on demand for initial filling of the field.

i bulk updated around 300 accounts and it worked perfectly. the other day I updated 4 accounts and suddenly the number didnt increase and all got the same number.

I dont think the code is the problem. I belief the instances of the cwa where running simultaniously  and accessing the same variable before it was increased. But I dont know how I can avoid it.  Here is my Code anyway:

...
protected override void Execute(CodeActivityContext executionContext)

        {
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory =
                executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service =
                serviceFactory.CreateOrganizationService(context.UserId);




            QueryExpression query = new QueryExpression("vci_autonumbering");
            query.ColumnSet = new ColumnSet(true);
            query.Criteria.Conditions.Add(new ConditionExpression("vci_entity", ConditionOperator.Equal, context.PrimaryEntityName));
            EntityCollection results = service.RetrieveMultiple(query);

            foreach (Entity e in results.Entities)
            {
                //Update autonumber
                var autonumber_guid = e.Id;
                var prefix = e.Attributes["vci_prefix"];
                var fieldname = e.Attributes["vci_fieldname"].ToString();
                String number = e.Attributes["vci_number"].ToString();
                Int64 number1 = Convert.ToInt64(number);
                number1 = number1 + 1;

                var update = new Entity(e.LogicalName, e.Id);
                update.Attributes.Add("vci_number", number1.ToString());
                service.Update(update);

                //update entity
                var updateentity = new Entity(context.PrimaryEntityName, context.PrimaryEntityId);
                updateentity.Attributes.Add(fieldname, prefix+number1.ToString());
                service.Update(updateentity);

            }



        }

Any Idea why the workflow pulled the same number and any Idea how I can avoid it?

Greetings and thx

McDauly

  • McDauly Profile Picture
    219 on at
    RE: auto numbering cwa pulls same number twice. With code

    Yes, we are still on premise with d365. I hope we change the next 2 years into th ecloud.

    Thx for the feedback on the code

    greetings Mcdauly

  • Linn Zaw Win Profile Picture
    3,407 on at
    RE: auto numbering cwa pulls same number twice. With code

    Exactly!!! It should solve your problem of pulling the same number.

    Just out of curiosity, may I know why CWA is used to generate the auto number over creating out-of-the-box auto number field? Is that because your environment is the old version of D365 on-prem without custom auto number field feature?
    docs.microsoft.com/.../autonumber-fields

  • McDauly Profile Picture
    219 on at
    RE: auto numbering cwa pulls same number twice. With code

    Hey Linn Zaw Win,

    I updated the code like this, is this what you mean?

    protected override void Execute(CodeActivityContext executionContext)
            {
                IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
                IOrganizationServiceFactory serviceFactory =
                    executionContext.GetExtension<IOrganizationServiceFactory>();
                IOrganizationService service =
                    serviceFactory.CreateOrganizationService(context.UserId);




                QueryExpression query = new QueryExpression("vci_autonumbering");
                query.ColumnSet = new ColumnSet(true);
                query.Criteria.Conditions.Add(new ConditionExpression("vci_entity", ConditionOperator.Equal, context.PrimaryEntityName));
                EntityCollection results = service.RetrieveMultiple(query);

                foreach (Entity e in results.Entities)
                {


                    var update = new Entity(e.LogicalName, e.Id);
                    update.Attributes.Add("vci_dummyfield", Guid.NewGuid().ToString());
                    service.Update(update);

                    ColumnSet colset = new ColumnSet(true);
                    Entity retrievied = service.Retrieve("vci_autonumbering", e.Id, colset);



                    //Update autonumber
                    var prefix = retrievied.Attributes["vci_prefix"];
                    var fieldname = retrievied.Attributes["vci_fieldname"].ToString();
                    String number = retrievied.Attributes["vci_number"].ToString();
                    Int64 number1 = Convert.ToInt64(number);
                    number1 = number1 + 1;

                    var update1 = new Entity(retrievied.LogicalName, retrievied.Id);
                    update1.Attributes.Add("vci_number", number1.ToString());
                    service.Update(update1);

                    //update entity
                    var updateentity = new Entity(context.PrimaryEntityName, context.PrimaryEntityId);
                    updateentity.Attributes.Add(fieldname, prefix+number1.ToString());
                    service.Update(updateentity);

                }



            }

  • McDauly Profile Picture
    219 on at
    RE: auto numbering cwa pulls same number twice. With code

    Hello Linn Zaw Win, thx for the response. I will build this in my code next week. Cause of holidays lately.

  • Verified answer
    Linn Zaw Win Profile Picture
    3,407 on at
    RE: auto numbering cwa pulls same number twice. With code

    To avoid that, you will need to lock the autonumbering entity in the transaction. Basically, what you have to do is make the dummy update to the record to lock it and prevent other processes from accessing.

    1. Create a new dummy text field in vci_autonumbering
    2. After RetrieveMultiple of vci_autonumbering entity,
    3. update the record by populating random value into the new dummy field (this time, the record will only be accessible by current cwa thread)
    4. retrieve the same vci_autonumbering entity to get the latest value (just in case if any other thread updates the data between step 2 and 3)
    5. proceed the process as usual and use the auto number to populate in the entity
    6. increase the auto number (only after everything, the lock for the record is released and other threads can access it)

    Read more details in the link below.

    https://www.akaes.com/blog/how-to-implement-robust-auto-numbering-using-transactions-in-microsoft-dynamics-crm/

  • Verified answer
    LeoAlt Profile Picture
    16,331 Moderator on at
    RE: auto numbering cwa pulls same number twice. With code

    Hi partner,

    There is no problem in your code, please check the following points.

    1.Add auditing to this entity x and check if the number field updated after run the cwa.

    2.Debug your cwa step by step to find out the reason.

    Regards,

    Leo

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Siv Sagar Profile Picture

Siv Sagar 149 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 57

#3
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 53 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans