Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Common record for locking in pessimistic concurrency scenario in Workflows

Posted on by

Hi all

I have an async workflow that executes on creation of entity type A. One of the things it must do is count the number of existing records in entity type A and then update the current entity type A record based on that count. The problem is, a number of them can be created at the same time, and so they're all updating with the same value. Essentially, I need them to run serially.

I'm aware of the dummy lock solution, and it's what I want to do, but how do I find a common record to lock? None of the created records have any common linked records except the type of entity. I considered trying to update a field on the WF owning user, but I don't have that as an option on the Update step?

Anyone have any clever ways of doing this? I'm not finding much other than the dummy lock in the forums/blogs/etc.

Thanks
James

  • James Profile Picture
    James on at
    RE: Common record for locking in pessimistic concurrency scenario in Workflows

    Thanks again, Bipin, but this would simply be delaying the concurrency issue till later in the process, would it not?

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Common record for locking in pessimistic concurrency scenario in Workflows

    Hi,

    You can assign records created from Incoming Email to a generic Team. Then Write plugin to assign the records to intended users based on your business requirement. This way you don't have to take care of concurrency scenario.

    Let me know if this is helpful!

  • James Profile Picture
    James on at
    RE: Common record for locking in pessimistic concurrency scenario in Workflows

    Thanks Rajesh, but I'm very specifically looking to prevent concurrency.

    The update to entity type A mentioned above is to assign the record. The problem I'm having at the moment is that incoming emails are creating records simultaneously, and so all the records are being assigned to the same person, based on them having the lowest calculated load.

  • Rajesh Chungath Profile Picture
    Rajesh Chungath 465 on at
    RE: Common record for locking in pessimistic concurrency scenario in Workflows

    If you are looking for a identity column (auto number) or any logic based on total record count

    you can leverage OOB auto number feature

    docs.microsoft.com/.../create-auto-number-attributes

    Sample code

    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);

    to update existing field

    //Modify the retrieved auto-number attribute

    AttributeMetadata retrievedAttributeMetadata = attributeResponse.AttributeMetadata;

    retrievedAttributeMetadata.AutoNumberFormat = "CAR-{RANDSTRING:5}{SEQNUM:6}"; //Modify the existing attribute by writing the format as per your requirement

    // Update the auto-number attribute            

    UpdateAttributeRequest updateRequest = new UpdateAttributeRequest

                           {

                               Attribute = retrievedAttributeMetadata,

                               EntityName = "newWidget",

                           };

    // Execute the request

    _serviceProxy.Execute(updateRequest);

    After updating the auto number format set seed value to next number. For initial setup find total count of records then add one to set seed value.

    //Define the seed

    SetAutoNumberSeedRequest req = new SetAutoNumberSeedRequest();

    req.EntityName = "newWidget";

    req.AttributeName = "newSerialnumber";

    req.Value = 10000;

    _serviceProxy.Execute(req);

    If you are performing any custom logic based on total count you can perform that based on this auto number field.

  • James Profile Picture
    James on at
    RE: Common record for locking in pessimistic concurrency scenario in Workflows

    Hi Bipin

    All parallel executions will still count the same number of records, then, so I'm still in the same position. Each instance must get a different answer.

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Common record for locking in pessimistic concurrency scenario in Workflows

    HI,

    Why don't you write plugin on Pre-operation Create, this way you will be able to get exact record count.

  • James Profile Picture
    James on at
    RE: Common record for locking in pessimistic concurrency scenario in Workflows

    Hi Mansoor - I don't mind trying this but can you please explain to me how it will help with concurrency? It's quite possible for two entities to be created at the same time, or within milliseconds of each other, so a realtime wofklow could still execute each instance in parallel, could it not?

    Thanks

    James

  • Mansoor Sulaiman Profile Picture
    Mansoor Sulaiman 4,330 on at
    RE: Common record for locking in pessimistic concurrency scenario in Workflows

    Hi,

    Can you try it with realtime workflows instead of async?

    Mansoor

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans