SOLVED

Auto-Increment Number Field in a SharePoint List

Copper Contributor

I need to create an auto-increment field with a standard amount of digits. The format needs to be like this T000001, T000002, T000003, ......T000010, etc (7 Digits). When it gets to the 10th record it should not do this T0000010 (8 digits).

I tried using the popular, often referenced solution (link below). This method does not work as I hoped. If you put it in a format like this T1000, when it gets to the 10th item it does this T100010 rather than T10010.

Here is the popular solution I already tried: https://www.epmpartners.com.au/blog/how-to-create-an-auto-incrementing-number-field-in-a-sharepoint-...

26 Replies

@Ninjak have you tried using a Calculated field?

In your list or library, the ID column (a system column) will increment each time an item is added. You can use a calculated column to create a prefix and append the ID  e.g.  T000+ID 

https://support.office.com/en-us/article/examples-of-common-formulas-in-sharepoint-lists-d81f5f21-2b...

 

best response confirmed by Steve Knutson (MVP)
Solution

Hi Keenan (@Ninjak), this complex auto-increment can be achieved relatively easily using 2 lists in SharePoint and a flow in Power Automate. The result will look like this in the main list which I've called Crop Harvest:

 

0-SP-List-Result.png

 

You will need a second list (which I've called Increment) to hold the next value. In the result above I manually changed the value to show the difference based on the number, and I've manually changed it back to 9 for now but  you will see that it will auto-increment in the flow. This second list will only ever have 1 item which is the next value to be used:

 

0-SP-IncrementList.png

 

In the flow the trigger I've used is When an item is created in the SharePoint main list.

 

Next we get the 1 item from the Increment list, and the next action is to initialize a string variable which I've named varIncrementNumber:

 

1-Trigger-getItems-initVariable.png

 

The next action to add is a Compose control which I've named ComposeNext and you select Next value from the dynamic content box on the right. What you will see when you do this is that it wraps itself in an apply to each control. It does this because although there is only 1 item in the Increment list there could potentially be lots so it adds the apply to each. That's fine, it's expected and you don't try to delete it but just leave it there.

 

The next action is a condition. For this first condition we are only going to test if the output of the previous compose action is less than 10 (we'll do the others shortly):

 

2-apply-composeNext-Condition1-9.png

 

If it IS less than 10 then in the If yes green channel we add an Append to string variable and in the value type T00000 immediately followed by the output of the ComposeNext selected from the dynamic content box on the right:

 

3-1to9-Append.png

 

If the ComposeNext value isn't less than 10 then in the red If no channel you add another condition. In the left hand box select the Output from the ComposeNext from the dynamic content, select is greater than or equal to, and then in the right hand box type 10. Then add another row and do the same but make it less than 100.

 

Again, you'll get a green if yes channel and a red if no channel. In the green channel, as before, add an append to string variable action and this time in the value add T0000 and then the output of the ComposeNext:

 

5-10to99Append.png

 

In the red If no channel add another condition as before but make it greater than or equal to 100 and less than 1000.

 

You keep doing that - changing the number of leading zeros each time of course - until you get to the last condition which is if the output of the ComposeNext is greater than or equal to 10000 and less than 100000 then you will add an Append to string variable in both channels. In the green If yes channel it will be T0 plus the Output and in the red if no channel it will be just T plus the Output.

 

6-condition10000-99999.png

 

So we've got the right value with the T plus correct leading zeros in our variable now. The next action is still inside the apply to each but not inside any of the conditions. The action is a SharePoint Update item action and in the text column where you are storing the increment text value you select the string variable varIncrementNumber. You'll see I've also stored the actual numeric value number from the ComposeNext:

 

7-updateMainList.png

 

We're almost at the end now and the next action is to add a Compose and add 1 to the ComposeNext value. So for this we use an expression of add (outputs('ComposeNext'),1). Finally we use the output of that to update the Increment list:

 

8-Compose-UpdateIncrement.png

 

Whatever value you have in the Increment list the flow will check that and apply the T and the correct number of zeros and then the increment value. It will take just 2 or 3 seconds to do this and to update the main list and the increment list.

 

That took far longer to write out and do the screenshots than it took to prepare the SharePoint lists and do the flow!! :smile:  Do come back with questions about any of this.

 

Rob
Los Gallardos

Microsoft Power Automate Community Super User

@Ninjak 

Hi,

Can you please tell me the column type of the NextValue field in Increment List?

Also I would like to know the Value to be given in Initialize Variable control for the varIncrementNumber.

 

 

@PriyankaJagarapu  I think your question might have been to me rather than the original poster. NextValue is a number column with decimal places set to 0. There's no initial value for the varIncrementValue so just leave it empty. All you're doing with that is to create the variable and it gets populated later in the flow.

 

Rob
Los Gallardos
Microsoft Power Automate Community User

@RobElliott 

Hi

Thanks for this, it looks almost exactly like what I need to do with one exception.  I'm doing this for document numbers for projects.  So my increment list is my list of projects.  I have a next number column in the list.

 

So, before I create the string, I need to get the next value for the specific project.  What action would I need to add (and where) in order to achieve this?

 

I;m most grateful for your help.

 

Best

 

Greg

Good morning,

I recently built this flow for creating lot numbers at our organization and ran into an issue where if 2 items are added very quickly then the same number is issued for both records.@RobElliott 

@mcaleer1 yes, we've also run into the same problem. I'm still trying to work on a way to avoid this issue.

 

Rob
Los Gallardos
Microsoft Power Automate Community Super Use

Actually I just figured it out! If you select settings for 'When an item is Created' you can turn on Concurrency Control and adjust the slider to only allow one item to process at a time. @RobElliott 

I also wanted to say thank you for the detailed instructions on how to build this flow. @RobElliott 

@mcaleer1 I could be wrong but I thought I'd tried it with concurrency control set to 1, but perhaps I didn't so thanks for flagging it up. I'll take another look at my flows.

 

Rob
Los Gallardos
Microsoft Power Automate Community Super User

@RobElliott I have tried to set this up for a while, tried 3 times and done everything exactly, but it does not work.

I dont get an error when I try to test the workflow, it just does not work....

 

One thing is however different. Every time I set up the composenext, I get an additional step, without being able to remowe it. I cant see if you have that on your end as well, since you cut of the image :(

 

JesperTC_0-1594988650216.png

 

Is this available as a template?

@Asad_Yousaf no you'd need to build it from blank.

 

Rob
Los Gallardos
Microsoft Power Automate Community Super User

@RobElliott  - I wanted to thank you for this!  This is excellent!  I spent a few hours trying to get a similar situation to this to work and I couldn't get the variables to increment by 1.  Just wanted to give you a virtual high five  :)

how can I do that ,

i want a formula to add+1 each time I adda task@Steve Knutson 

@Ninjakfor SharePoint server, you can't used Microsoft automate without using gateway.

Instead, you should use JSOM or SharePoint Designer!

@Ninjak I realise this post is over a year old but thought it is worth highlighting that there is now a Format Number action that should make this process a lot more simple:

 

BenLeach_EXP_0-1614856098876.png

 

Brilliant. That works like a champ, thanks!

@RobElliott 

 

I realize that this thread is over a year old. But I have had this bookmarked and attempted to make this work multiples times over the past 6 months. I finally was able to figure out where I went wrong and got it to work. 

So after it finally working on a fresh list, I decided that I would bring in my already created log file and put all those logs in there and update my Increment list to be the next value. I then set my flow to be active. And it worked....but it keeps looping through my already created list values. 

Where should I begin looking to stop the flow from auto running even though I have not clicked the create new in my sharepoint list? Please help as I have had over 2000 flows run in the last hour.

1 best response

Accepted Solutions
best response confirmed by Steve Knutson (MVP)
Solution

Hi Keenan (@Ninjak), this complex auto-increment can be achieved relatively easily using 2 lists in SharePoint and a flow in Power Automate. The result will look like this in the main list which I've called Crop Harvest:

 

0-SP-List-Result.png

 

You will need a second list (which I've called Increment) to hold the next value. In the result above I manually changed the value to show the difference based on the number, and I've manually changed it back to 9 for now but  you will see that it will auto-increment in the flow. This second list will only ever have 1 item which is the next value to be used:

 

0-SP-IncrementList.png

 

In the flow the trigger I've used is When an item is created in the SharePoint main list.

 

Next we get the 1 item from the Increment list, and the next action is to initialize a string variable which I've named varIncrementNumber:

 

1-Trigger-getItems-initVariable.png

 

The next action to add is a Compose control which I've named ComposeNext and you select Next value from the dynamic content box on the right. What you will see when you do this is that it wraps itself in an apply to each control. It does this because although there is only 1 item in the Increment list there could potentially be lots so it adds the apply to each. That's fine, it's expected and you don't try to delete it but just leave it there.

 

The next action is a condition. For this first condition we are only going to test if the output of the previous compose action is less than 10 (we'll do the others shortly):

 

2-apply-composeNext-Condition1-9.png

 

If it IS less than 10 then in the If yes green channel we add an Append to string variable and in the value type T00000 immediately followed by the output of the ComposeNext selected from the dynamic content box on the right:

 

3-1to9-Append.png

 

If the ComposeNext value isn't less than 10 then in the red If no channel you add another condition. In the left hand box select the Output from the ComposeNext from the dynamic content, select is greater than or equal to, and then in the right hand box type 10. Then add another row and do the same but make it less than 100.

 

Again, you'll get a green if yes channel and a red if no channel. In the green channel, as before, add an append to string variable action and this time in the value add T0000 and then the output of the ComposeNext:

 

5-10to99Append.png

 

In the red If no channel add another condition as before but make it greater than or equal to 100 and less than 1000.

 

You keep doing that - changing the number of leading zeros each time of course - until you get to the last condition which is if the output of the ComposeNext is greater than or equal to 10000 and less than 100000 then you will add an Append to string variable in both channels. In the green If yes channel it will be T0 plus the Output and in the red if no channel it will be just T plus the Output.

 

6-condition10000-99999.png

 

So we've got the right value with the T plus correct leading zeros in our variable now. The next action is still inside the apply to each but not inside any of the conditions. The action is a SharePoint Update item action and in the text column where you are storing the increment text value you select the string variable varIncrementNumber. You'll see I've also stored the actual numeric value number from the ComposeNext:

 

7-updateMainList.png

 

We're almost at the end now and the next action is to add a Compose and add 1 to the ComposeNext value. So for this we use an expression of add (outputs('ComposeNext'),1). Finally we use the output of that to update the Increment list:

 

8-Compose-UpdateIncrement.png

 

Whatever value you have in the Increment list the flow will check that and apply the T and the correct number of zeros and then the increment value. It will take just 2 or 3 seconds to do this and to update the main list and the increment list.

 

That took far longer to write out and do the screenshots than it took to prepare the SharePoint lists and do the flow!! :smile:  Do come back with questions about any of this.

 

Rob
Los Gallardos

Microsoft Power Automate Community Super User

View solution in original post