# Atomically setting NextValue counter?

**URL:** <https://www.epiusers.help/t/atomically-setting-nextvalue-counter/62781>\
**Category:** Epicor ERP 10\
**Tags:** e10\
**Created:** [December 16, 2019, 6:16pm UTC](https://www.epiusers.help/t/atomically-setting-nextvalue-counter/62781 "2019-12-16T18:16:36Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![krum](https://www.epiusers.help/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@krum](https://www.epiusers.help/u/krum)\
**Post date:** [December 16, 2019, 6:16pm UTC](https://www.epiusers.help/t/atomically-setting-nextvalue-counter/62781/1 "2019-12-16T18:16:36Z")

</div>

Is there a way to atomically set a `NextValue` counter if the current value is lower than some specified value? This could also take the form of `GetNextSequence(seqName, minValue)`. There doesn’t seem to be a way to do this atomically, so any attempt to do this within a BPM creates a race condition.

---

<div class="post-metadata">

**Author:** ![TobyLai](https://www.epiusers.help/user_avatar/www.epiusers.help/tobylai/32/8246_2.png) [@TobyLai](https://www.epiusers.help/u/TobyLai)\
**Post date:** [December 18, 2019, 2:53am UTC](https://www.epiusers.help/t/atomically-setting-nextvalue-counter/62781/2 "2019-12-18T02:53:20Z")

</div>

I guess you mean Automatically?

I think your solution is related to this post.

> [@Auto Numbering - Customer ID](https://www.epiusers.help/t/auto-numbering-customer-id/43884/10):
>
> It has been fun seeing the document that i published YEARS ago roll around the internet on how to auto-assign customer/supplier/project ID. I made it to be public for that reason… but it has gone under many iterations. Method 1: I “invented” 25 years ago was to create a phoney customer ID called “Next”… and in the customer record, I bumped the number… but that method is bad because you always had to “hide” this phony customer. Method 2 (I borrowed from someone) was to put a UD Field in the Com…

---

<div class="post-metadata">

**Author:** ![krum](https://www.epiusers.help/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@krum](https://www.epiusers.help/u/krum)\
**Post date:** [December 18, 2019, 7:14am UTC](https://www.epiusers.help/t/atomically-setting-nextvalue-counter/62781/3 "2019-12-18T07:14:49Z")

</div>

I mean atomically. I’d like to write something like this, but it contains a potential race condition:

```
var nv = new NextValue(Db);
var foo = nv.GetNextSequence("FOO");
if(foo < MIN)
{
    foo = MIN;
    NextValue.SetSequenceCurrentValue(Db, "FOO", foo);
}

```

I was hoping there was some undocumented method that would do this atomically, like

```
var nv = new NextValue(Db);
var foo = nv.GetNextSequence("FOO", MIN); // method doesn't exist
```

---

<div class="post-metadata">

**Author:** ![markdamen](https://www.epiusers.help/user_avatar/www.epiusers.help/markdamen/32/54917_2.png) [@markdamen](https://www.epiusers.help/u/markdamen)\
**Post date:** [December 18, 2019, 8:34am UTC](https://www.epiusers.help/t/atomically-setting-nextvalue-counter/62781/4 "2019-12-18T08:34:06Z")

</div>

I think @josecgomez may have originally shared this below code:

Erp.Internal.Lib.CompanySequence.GetNextCompanySeq(CompanyID, SequenceName)

Erp.Internal.Lib.SetCurrentCompanySequence(CompanyID, SequenceName, currentValue)

---

<div class="post-metadata">

**Author:** ![krum](https://www.epiusers.help/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@krum](https://www.epiusers.help/u/krum)\
**Post date:** [December 18, 2019, 4:31pm UTC](https://www.epiusers.help/t/atomically-setting-nextvalue-counter/62781/5 "2019-12-18T16:31:32Z")

</div>

Sorry, I don’t see how that addresses my question. I’m looking for a way to atomically set the current value if the current value is lower than some specified value. The goal is to avoid the need to take the system offline to bump a counter.

---

<div class="post-metadata">

**Author:** ![markdamen](https://www.epiusers.help/user_avatar/www.epiusers.help/markdamen/32/54917_2.png) [@markdamen](https://www.epiusers.help/u/markdamen)\
**Post date:** [December 18, 2019, 4:47pm UTC](https://www.epiusers.help/t/atomically-setting-nextvalue-counter/62781/6 "2019-12-18T16:47:44Z")

</div>

You don’t have to take anything offline - you can call either of those 2 methods at any time.

---

<div class="post-metadata">

**Author:** ![krum](https://www.epiusers.help/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@krum](https://www.epiusers.help/u/krum)\
**Post date:** [December 18, 2019, 4:50pm UTC](https://www.epiusers.help/t/atomically-setting-nextvalue-counter/62781/7 "2019-12-18T16:50:49Z")

</div>

But you cannot call one and then conditionally call the other atomically. (Unless there’s something I don’t know about the framework, like all BPMs run in a single thread.)

---

<div class="post-metadata">

**Author:** ![TobyLai](https://www.epiusers.help/user_avatar/www.epiusers.help/tobylai/32/8246_2.png) [@TobyLai](https://www.epiusers.help/u/TobyLai)\
**Post date:** [December 18, 2019, 11:06pm UTC](https://www.epiusers.help/t/atomically-setting-nextvalue-counter/62781/8 "2019-12-18T23:06:51Z")

</div>

Check out transactionscope.

[https://blog.goyello.com/2016/11/30/let-net-framework-care-about-transactions-handling-for-you-by-implementing-ienlistmentnotification/](https://blog.goyello.com/2016/11/30/let-net-framework-care-about-transactions-handling-for-you-by-implementing-ienlistmentnotification/)

> **[More Epicor BPM C# code development tips | Luke McAvoy](https://lukemcavoy.co.uk/epicor-bpms-update-records/)**
>
> More Epicor BPM coding tips for adding, updating, deleting records in the Epicor ERP application

---

<div class="post-metadata">

**Author:** ![Epic\_Santiago](https://www.epiusers.help/letter_avatar_proxy/v4/letter/e/3d9bf3/32.png) [@Epic\_Santiago](https://www.epiusers.help/u/Epic_Santiago)\
**Post date:** [December 19, 2019, 8:36pm UTC](https://www.epiusers.help/t/atomically-setting-nextvalue-counter/62781/9 "2019-12-19T20:36:27Z")

</div>

How about this?

```
var nv = new NextValue(Db);
int foo;
while ((foo = nv.GetNextSequence("FOO")) < MIN)
{
    Console.WriteLine(foo);
}

```

`GetNextSequence()` will get the next value and rev the counter so no one else can get the same value. I think that satisfies your atomic requirements.

As far as the comments regarding transactions, that’s good advice but does not apply in this case. The values are rev’d outside the transaction (otherwise it would create a lot of database contention).

---

<div class="post-metadata">

**Author:** ![krum](https://www.epiusers.help/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@krum](https://www.epiusers.help/u/krum)\
**Post date:** [December 20, 2019, 7:08pm UTC](https://www.epiusers.help/t/atomically-setting-nextvalue-counter/62781/10 "2019-12-20T19:08:36Z")

</div>

That makes sense. I wonder how long it will take to increment a million times…

---

<div class="post-metadata">

**Author:** ![Epic\_Santiago](https://www.epiusers.help/letter_avatar_proxy/v4/letter/e/3d9bf3/32.png) [@Epic\_Santiago](https://www.epiusers.help/u/Epic_Santiago)\
**Post date:** [December 20, 2019, 7:24pm UTC](https://www.epiusers.help/t/atomically-setting-nextvalue-counter/62781/11 "2019-12-20T19:24:16Z")

</div>

Doing just about anything a million times will be relatively slow, especially if it includes database access. If it’s a one time hit it’s likely acceptable. If your use case includes the sequence getting out of sync by a million regularly you will want to look at `GetSequenceBlock("foo", 1000000)` which can increment the sequence by an arbitrary block size. But then you’ll have to deal with potentially two threads incrementing by that much at once and then you could end up with a big gap in your sequence. You can experiment with doing a block size of half the gap minus 1 and then incrementing by one the rest of the way to avoid that. I’ll leave this as an exercise to the reader 😉

---

<div class="post-metadata">

**Author:** ![krum](https://www.epiusers.help/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@krum](https://www.epiusers.help/u/krum)\
**Post date:** [December 20, 2019, 7:33pm UTC](https://www.epiusers.help/t/atomically-setting-nextvalue-counter/62781/12 "2019-12-20T19:33:59Z")

</div>

My use case is that we have counters that we might want to bump up to the next million once a year or something like that. My original idea was to put the minimum values in the same BPM that _uses_ the counter values, because that’s where I expect someone to go looking for them. But since bumping the counters will probably be a rare occurrence, it’s probably easier to put a comment in the BPM with the instruction to take Epicor offline and manually update the counter in Ice.SysSequence. Either that or count up to the new minimum and include a comment saying to expect a long delay.

---

<div class="post-metadata">

**Author:** ![Mark\_Wonsil](https://www.epiusers.help/user_avatar/www.epiusers.help/mark_wonsil/32/51594_2.png) [@Mark\_Wonsil](https://www.epiusers.help/u/Mark_Wonsil)\
**Post date:** [December 23, 2019, 6:44pm UTC](https://www.epiusers.help/t/atomically-setting-nextvalue-counter/62781/13 "2019-12-23T18:44:44Z")

</div>

Could you use two counters: your million counter and the detail counter and just concatenate the results? Bumping the million counter would be immediate.
