# Expression Help?

**URL:** <https://community.tulip.co/t/expression-help/8160>\
**Category:** General\
**Created:** [April 24, 2023, 4:03pm UTC](https://community.tulip.co/t/expression-help/8160 "2023-04-24T16:03:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonh](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@jasonh](https://community.tulip.co/u/jasonh)\
**Post date:** [April 24, 2023, 4:03pm UTC](https://community.tulip.co/t/expression-help/8160/1 "2023-04-24T16:03:23Z")

</div>

So, I’m not an expert when it comes to expressions. I have an expression that I want to write, but unsure how to do it.  
I have a Tulip app that needs to read the most recent or highest serial number, then add to it a variable an operator inputs. It will then store those newly created serial numbers in the Tulip table.

So to visualize things, here’s the Tulip table:

 ![Expression 1](https://us1.discourse-cdn.com/flex020/uploads/agilemfg/original/2X/9/9b2ce8ff871381bc7cb86c4d4fbc04cfa32bc3cd.png)

And here’s the test app:

 ![Expression 2](https://us1.discourse-cdn.com/flex020/uploads/agilemfg/original/2X/a/a9c02c3636db4901564f7a47ec6c3ec862bb8d3e.png)

When the button is pushed, I would like the expression to find the highest serial number in the “serial number” column, then add 1 for each number in the “Input# of things to build” and create a list of new serial numbers to be added in the “serial number” column in the Tulip Table.

EX. if an operator is building 10 new things, and the last serial number is “SP12345” then once the button is clicked, 10 new serial numbers will be added to the “Serial Number” column:  
SP12346  
SP12347  
SP12348  
etc.

Is that even possible?

---

<div class="post-metadata">

**Author:** ![Grant](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tulip.co/grant/32/182_2.png) [@Grant](https://community.tulip.co/u/Grant)\
**Post date:** [April 24, 2023, 5:34pm UTC](https://community.tulip.co/t/expression-help/8160/2 "2023-04-24T17:34:03Z")

</div>

Hi @jasonh,

This definitely should be possible but I might approach it a little differently.

The first challenge you’re going to face is that the Serial Number column is of data type text, not an integer. This means that the MAX() operator doesn’t mean very much in this context. However, you could work around this a couple ways. First, if your Serial Number is always incrementing, you can infer that your last created Serial Number is your max value.

This should be easy to find - just write a table query to return records sorting by date created descending and limit the output to 1. You can then use an aggregation for Mode on the Serial Number column to get a tangible value within your application.

From here, the input widget (I’ll call it `Build Quantity`) you have really indicates how many times your app will need to loop to create records. So if you set a variable called `Index` to a default value of 1, when you press the button to generate you can go to a looping step. On step enter, check to see if `Index` \> `Build Quantity`. If it’s not, create a new table record where the ID is an expression that takes the table aggregation, strips the SP from the ID, converts to an integer, adds one, then adds the SP back to the ID field. You can then increment your `Index` and transition back to the same step.

Once the condition fails, you’ve created 10 records and can move to the next step.

There’s a couple different design decisions you may need to make depending on how many people could be running this at once, if the parts need to be sequential, etc. but these would be the general steps.

Let me know if that makes sense!

---

<div class="post-metadata">

**Author:** ![jasonh](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@jasonh](https://community.tulip.co/u/jasonh)\
**Post date:** [April 25, 2023, 1:42pm UTC](https://community.tulip.co/t/expression-help/8160/3 "2023-04-25T13:42:54Z")

</div>

Hi @Grant  
Thanks for the reply!

Our serial numbers are a combination of letters and numbers, so must remain as text. I’ve created a table query to sort by serial number (z to a) so that _should_ sort the latest serial number to the top.

 ![Expression 3](https://us1.discourse-cdn.com/flex020/uploads/agilemfg/original/2X/6/65008987c758ae4ec30b40f12a6b0bcc81a83951.png)

Our build process happens in groups of 10 (sometimes less, sometimes more). Looping makes sense, but we need to have a set of X numbers generated and saved in the table.

To explain why, we generate serial number labels based on the group of parts the operator will be making. Right now this is a paper process where the operator writes down the serial numbers, then physically walks to a printer to input the numbers and print the labels.  
In the future we plan on integrating the printing function into Tulip, but for now it will remain a manual process.  
The operators do builds in groups of 10, so we need to stick with that until we can gradually change.

The aggregation that removes the text, converts to an integer, increments then concats the text back in is great! The generating X# of serial numbers is the toughest part right now…

---

<div class="post-metadata">

**Author:** ![Grant](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tulip.co/grant/32/182_2.png) [@Grant](https://community.tulip.co/u/Grant)\
**Post date:** [April 25, 2023, 1:58pm UTC](https://community.tulip.co/t/expression-help/8160/4 "2023-04-25T13:58:07Z")

</div>

Hi @jasonh,

You can definitely create a variable number of new table records together but it will need to happen via a loop as we only support actions creating one table record at a time (unless you used X number of placeholders but that wouldn’t truly by dynamic).

The issue I wanted to highlight was if you had multiple people creating records at one time, you have to make a decision on how you want to organize them. You could theoretically have:

- SN1 (Operator A)
- SN2 (Operator B)
- SN3 (Operator A)
- SN4 (Operator B)
- …

or you could build in protection where operators reserve the next block of serial numbers:

- SN1 (Operator A)
- SN2 (Operator A)
- SN3 (Operator A)
- …
- SN11 (Operator B)
- …

To accomplish the first, you’d just want to build a table record based on the returned value from the aggregation. To accomplish the second, you’d probably want to use a second table that tracks the current serial number and just increment by the reserved part count immediately so that the next user takes the next block.

Does the looping step make sense? You would just keep using step open triggers to fire actions to create a new table record based on some dynamic value (either the aggregation or some variable counter depending on which approach you take).

---

<div class="post-metadata">

**Author:** ![tony.wang](https://avatars.discourse-cdn.com/v4/letter/t/e9a140/32.png) [@tony.wang](https://community.tulip.co/u/tony.wang)\
**Post date:** [April 25, 2023, 3:09pm UTC](https://community.tulip.co/t/expression-help/8160/5 "2023-04-25T15:09:02Z")

</div>

@jasonh As your description for SN,your expression should like below:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/agilemfg/original/2X/f/f54119c6f008d8d6be7906b3b32121595dfd2e8c.png)

‘SP’ + totext(texttointeger(right(@Variable.CodeDesc , 5)) + 1)

The result of each step as below:

SP12345 right(@Variable.CodeDesc , 5) =\> “12345” string  
texttointeger(right(@Variable.CodeDesc , 5)) =\> 12345 int  
texttointeger(right(@Variable.CodeDesc , 5)) + 1=\>12346 int  
totext(texttointeger(right(@Variable.CodeDesc , 5)) + 1) =\>12346 string  
‘SP’ + totext(texttointeger(right(@Variable.CodeDesc , 5)) + 1)=\> “SP12346”

if you need generate 10 or more SN one time,you need combine with customer widget for loop 10 times

wish it will be helpful!

---

<div class="post-metadata">

**Author:** ![thorsten.langner](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@thorsten.langner](https://community.tulip.co/u/thorsten.langner)\
**Post date:** [April 26, 2023, 8:13am UTC](https://community.tulip.co/t/expression-help/8160/6 "2023-04-26T08:13:22Z")

</div>

Hi @jasonh,

to Auto-Increment a Table Value (also mixed with text) is an often requested funktionality.

Please have a look if the solution in this thread helps:

> [@How to prevent user from entering same number again (How to increment more complex table ID's)](https://community.tulip.co/t/how-to-prevent-user-from-entering-same-number-again-how-to-increment-more-complex-table-ids/7748/6):
>
> Create a query with limit 1 and sorting new to old: Create an aggregation with Mode on ID: Extract the string after the “-” (e.g. “01”) make it an Integer, increment it by one Check if you want to add a “0” when the number is lower than 10 and make it a string. Extract the string before “-” (or hardcode “UN-”) and add the new number-string create a record with this one… Result: [Increment\_table\_ID] The result will be like: UN-01 UN-02 … UN-09 UN…

---

<div class="post-metadata">

**Author:** ![jasonh](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@jasonh](https://community.tulip.co/u/jasonh)\
**Post date:** [May 2, 2023, 3:47pm UTC](https://community.tulip.co/t/expression-help/8160/7 "2023-05-02T15:47:10Z")

</div>

Hi everyone,

I wanted to take a quick minute to thank you all for helping me with this problem. As someone who has very little programming experience, I’m struggling with understanding and completing expressions and triggers. I’m going to re-visit some of the Tulip University sessions on the Table Queries, Aggregations and the Expression editor.

My hope is in the near future all of this will make more sense and I’ll have a better handle on things.
