# Cannot create a loop with BPM widgets

**URL:** <https://www.epiusers.help/t/cannot-create-a-loop-with-bpm-widgets/60854>\
**Category:** Epicor ERP 10\
**Tags:** bpm\
**Created:** [October 16, 2019, 2:21pm UTC](https://www.epiusers.help/t/cannot-create-a-loop-with-bpm-widgets/60854 "2019-10-16T14:21:59Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![JasonMcD](https://www.epiusers.help/user_avatar/www.epiusers.help/jasonmcd/32/22326_2.png) [@JasonMcD](https://www.epiusers.help/u/JasonMcD)\
**Post date:** [October 16, 2019, 2:21pm UTC](https://www.epiusers.help/t/cannot-create-a-loop-with-bpm-widgets/60854/1 "2019-10-16T14:21:59Z")

</div>

So, I can’t do this, eh? That’s how I read the error.

I’m sure many of you are laughing at the idea of this code. I just want the next available PO number that is above number X but below the number in company configuration, so that this site can have its own PO number set.

Anyhow, I guess the only way to melt down the system is in C#? Yay.

 ![image](https://epiusers.help/uploads/default/original/3X/8/4/84b2940327bf029ceb36f942c4a24d76403db84a.png)

---

<div class="post-metadata">

**Author:** ![Aaron\_Moreng](https://www.epiusers.help/user_avatar/www.epiusers.help/aaron_moreng/32/745_2.png) [@Aaron\_Moreng](https://www.epiusers.help/u/Aaron_Moreng)\
**Post date:** [October 16, 2019, 2:45pm UTC](https://www.epiusers.help/t/cannot-create-a-loop-with-bpm-widgets/60854/2 "2019-10-16T14:45:11Z")

</div>

Since this is on PO.Update, why not perform a check and if it fails the check, throw an exception?  
That will prevent the PO.Update method from completing and will force the user to enter a “valid” number.  
The next time they try to save, it will evaluate the condition again.

---

<div class="post-metadata">

**Author:** ![josecgomez](https://www.epiusers.help/user_avatar/www.epiusers.help/josecgomez/32/55731_2.png) [@josecgomez](https://www.epiusers.help/u/josecgomez)\
**Post date:** [October 16, 2019, 3:10pm UTC](https://www.epiusers.help/t/cannot-create-a-loop-with-bpm-widgets/60854/3 "2019-10-16T15:10:40Z")

</div>

You can create loops but usually you need to have more than 1 widget in between (unless they changed that recently)

---

<div class="post-metadata">

**Author:** ![JasonMcD](https://www.epiusers.help/user_avatar/www.epiusers.help/jasonmcd/32/22326_2.png) [@JasonMcD](https://www.epiusers.help/u/JasonMcD)\
**Post date:** [October 16, 2019, 3:35pm UTC](https://www.epiusers.help/t/cannot-create-a-loop-with-bpm-widgets/60854/4 "2019-10-16T15:35:07Z")

</div>

@Aaron_Moreng I might be misunderstanding what you mean, but I’m trying to generate the next number, not validate it.

Well, OK, I’m validating it, too, but it’s not from user input.

But AFAIK, each _site_ cannot have its own sequence of PO numbers natively. You can manually enter a number lower than the starting number in company config, or use a BPM to do the same.

---

<div class="post-metadata">

**Author:** ![JasonMcD](https://www.epiusers.help/user_avatar/www.epiusers.help/jasonmcd/32/22326_2.png) [@JasonMcD](https://www.epiusers.help/u/JasonMcD)\
**Post date:** [October 16, 2019, 3:45pm UTC](https://www.epiusers.help/t/cannot-create-a-loop-with-bpm-widgets/60854/5 "2019-10-16T15:45:03Z")

</div>

For what it’s worth, I made no changes to my BPM and it worked exactly as I wanted it to. The error in the designer is not a binding law, I guess.

---

<div class="post-metadata">

**Author:** ![Banderson](https://www.epiusers.help/user_avatar/www.epiusers.help/banderson/32/50875_2.png) [@Banderson](https://www.epiusers.help/u/Banderson)\
**Post date:** [October 16, 2019, 3:49pm UTC](https://www.epiusers.help/t/cannot-create-a-loop-with-bpm-widgets/60854/6 "2019-10-16T15:49:06Z")

</div>

That validation is kind of crap… It doesn’t catch things that it should and, like you’ve found, raises problems that aren’t problems.

---

<div class="post-metadata">

**Author:** ![JasonMcD](https://www.epiusers.help/user_avatar/www.epiusers.help/jasonmcd/32/22326_2.png) [@JasonMcD](https://www.epiusers.help/u/JasonMcD)\
**Post date:** [October 16, 2019, 3:52pm UTC](https://www.epiusers.help/t/cannot-create-a-loop-with-bpm-widgets/60854/7 "2019-10-16T15:52:57Z")

</div>

@Banderson Yeah I’m sorry for taking some of your time. I know that wasn’t meant as a guilt trip, but I feel silly now. Which is kind of your point.

---

<div class="post-metadata">

**Author:** ![Banderson](https://www.epiusers.help/user_avatar/www.epiusers.help/banderson/32/50875_2.png) [@Banderson](https://www.epiusers.help/u/Banderson)\
**Post date:** [October 16, 2019, 3:55pm UTC](https://www.epiusers.help/t/cannot-create-a-loop-with-bpm-widgets/60854/8 "2019-10-16T15:55:05Z")

</div>

No worries, I was just agreeing with you, that I never truly trusted it.

---

<div class="post-metadata">

**Author:** ![ckrusen](https://www.epiusers.help/user_avatar/www.epiusers.help/ckrusen/32/50584_2.png) [@ckrusen](https://www.epiusers.help/u/ckrusen)\
**Post date:** [October 16, 2019, 4:04pm UTC](https://www.epiusers.help/t/cannot-create-a-loop-with-bpm-widgets/60854/9 "2019-10-16T16:04:56Z")

</div>

Here’s my very first attempt at LINQ code from scratch (so be gentle…)

I believe the following code in a Set Field Widget should work.

```auto
(from x in Db.POHeader where (x.PONum >= 10000 && x.PONum <= 30000) select x.PONum).Max()+1

```

Obviously only call it if you pass the condition for testing if its the target Plant

# EDIT:

It does no error checking to see if a prior PO existed in that range of 10,000 to 20,000. If none exists it will error out.

Also, our Next PO Num in Company Maint is 35,000

---

<div class="post-metadata">

**Author:** ![JasonMcD](https://www.epiusers.help/user_avatar/www.epiusers.help/jasonmcd/32/22326_2.png) [@JasonMcD](https://www.epiusers.help/u/JasonMcD)\
**Post date:** [October 16, 2019, 5:44pm UTC](https://www.epiusers.help/t/cannot-create-a-loop-with-bpm-widgets/60854/10 "2019-10-16T17:44:31Z")

</div>

The max idea is a more useful approach than mine, which would require no deleted PO numbers. And undoubtedly faster.

---

<div class="post-metadata">

**Author:** ![ckrusen](https://www.epiusers.help/user_avatar/www.epiusers.help/ckrusen/32/50584_2.png) [@ckrusen](https://www.epiusers.help/u/ckrusen)\
**Post date:** [October 16, 2019, 6:23pm UTC](https://www.epiusers.help/t/cannot-create-a-loop-with-bpm-widgets/60854/11 "2019-10-16T18:23:28Z")

</div>

Like I said, that was my first attempt at LINQ code, so it may not be 100%. But it did work (as long as there was already one PO between 10,000 and 30,000)

When there isn’t, I think it the first part `(from ... .MAX()` returns a null, and it errors when trying to evaluate `null + 1`

I thought I could get even more clever, and do the plant check in that LINQ code, but the plant is not stored with the POHeader ☹

---

<div class="post-metadata">

**Author:** ![A.Baeisa](https://www.epiusers.help/user_avatar/www.epiusers.help/a.baeisa/32/8988_2.png) [@A.Baeisa](https://www.epiusers.help/u/A.Baeisa)\
**Post date:** [October 17, 2019, 7:37am UTC](https://www.epiusers.help/t/cannot-create-a-loop-with-bpm-widgets/60854/12 "2019-10-17T07:37:18Z")

</div>

> [@ckrusen](#):
>
> (from x in Db.POHeader where (x.PONum \>= 10000 && x.PONum \<= 30000) select x.PONum).Max()+1

may i ask why you want to add plant check, if each company has its own sequence range then,

```auto
var ttPOHeader_xRow = (from ttPOHeader_Row in ttPOHeader
			 where ttPOHeader_Row.Added()
select ttPOHeader_Row).FirstOrDefault();
if(ttPOHeader_xRow != null)
{
var NextPONum = (from x in Db.POHeader where x.Company == ttPOHeader_xRow.Company select x.PONum).Max()+1
//now you can assign NextPONum to ttPOHeader.PONum
// OR if you have that value stored in Company table then
var NextPONum = (from x in Db.Company where x.Company == ttPOHeader_xRow.Company select x.YourCompanyLastPONumUDField)+1
}

```

**Edit:**  
you can call this value from the plant table - if you decide to have different PONum range per plant not company- using the same principle, i.e. comparing to the session PlantID to get your record in Db.Plant table.

```auto
var NextPONum = (from x in Db.Plant where x.Company == ttPOHeader_xRow.Company && x.Plant == Session.PlantID select x.YourPlantLastPONumUDField)+1

```

 ![image](https://www.epiusers.help/uploads/default/original/3X/3/6/36ad5ab46c91f9152b96f3ed21e51340a642ab8f.png)

---

<div class="post-metadata">

**Author:** ![ckrusen](https://www.epiusers.help/user_avatar/www.epiusers.help/ckrusen/32/50584_2.png) [@ckrusen](https://www.epiusers.help/u/ckrusen)\
**Post date:** [October 17, 2019, 4:14pm UTC](https://www.epiusers.help/t/cannot-create-a-loop-with-bpm-widgets/60854/13 "2019-10-17T16:14:18Z")

</div>

> [@A.Baeisa](#):
>
> may i ask why you want to add plant check, if each company has its own sequence range

because the OP said:

> [@JasonMcD](#):
>
> so that this site can have its own PO number set.

I assumed that to be, that they wanted different PONum ranges for different plants/sites, within the same company

---

<div class="post-metadata">

**Author:** ![A.Baeisa](https://www.epiusers.help/user_avatar/www.epiusers.help/a.baeisa/32/8988_2.png) [@A.Baeisa](https://www.epiusers.help/u/A.Baeisa)\
**Post date:** [October 18, 2019, 7:03am UTC](https://www.epiusers.help/t/cannot-create-a-loop-with-bpm-widgets/60854/14 "2019-10-18T07:03:53Z")

</div>

thanks @ckrusen, i have posted the needed code for both cases

> [@Cannot create a loop with BPM widgets](https://www.epiusers.help/t/cannot-create-a-loop-with-bpm-widgets/60854/12):
>
> may i ask why you want to add plant check, if each company has its own sequence range then, var ttPOHeader\_xRow = (from ttPOHeader\_Row in ttPOHeader where ttPOHeader\_Row.Added() select ttPOHeader\_Row).FirstOrDefault(); if(ttPOHeader\_xRow != null) { var NextPONum = (from x in Db.POHeader where x.Company == ttPOHeader\_xRow.Company select x.PONum).Max()+1 //now you can assign NextPONum to ttPOHeader.PONum // OR if you have that value stored in Company table then var NextPONum = (from x in Db…

---

<div class="post-metadata">

**Author:** ![JasonMcD](https://www.epiusers.help/user_avatar/www.epiusers.help/jasonmcd/32/22326_2.png) [@JasonMcD](https://www.epiusers.help/u/JasonMcD)\
**Post date:** [August 10, 2023, 6:17pm UTC](https://www.epiusers.help/t/cannot-create-a-loop-with-bpm-widgets/60854/15 "2023-08-10T18:17:56Z")

</div>

FOUR YEARS LATER…

Now that I’m not nearly as scared of BPMs, I finally gave this code a whirl, and it works great! Calvin, this was brilliant.

It literally was this simple. No usings or any coder stuff. Wish I had tried this earlier…

> [@ckrusen](#):
>
> Obviously only call it if you pass the condition for testing if its the target Plant

OK, right, I did add a few conditions (in widgets) ahead of the code, but the point is that the Set Field widget is truly this simple.

And it works in milliseconds. Yes!
