Forum Discussion

em_amdahl's avatar
em_amdahl
Copper Contributor
Sep 24, 2023

How to add a custom counter to a title name in sharepoint list.

Hello I'm looking to add a custom ID counter to my titles in the sharepoint list. 

 

The title (Machine Number) will look like this.... 

 

M123456

M123425

 

I'd like to add a counter to the title in the instance that we have multiple entries for the same Machine Number. 

 

M123456-1

M123425-1

M123456-2

and so on....

 

How can I do this? 

  • Rob_Elliott's avatar
    Rob_Elliott
    Bronze Contributor

    em_amdahl you can't do this with just a SharePoint list as it can't iterate over all the items in the list. But you can do this with a flow in Power Automate as shown below. I've used a single line of text column called Reference but if you do it with the Title column the principal is the same.

     

    This is my list as it is currently with 3 items for Martinique and we'll add a 4th item for Martinique and when we do we'll just add the basic reference for the location, M555125.

     

    The flow

     

    1. The trigger is the SharePoint when an item is created and the first action is to get the item:

     

     

    2. Next, add a compose action, click in the inputs field and select Reference from the get item section of the dynamic content box.

     

    Next, initialize a float variable and set it's initial value to 0.

     

    Next, add another compose action and select ID from the get item section of the dynamic content box. We'll need this to exclude the new item in the next action.

     

     

    3. Next, add a SharePoint get items action and open the advanced options link and in the filter query field and ID ne to 'the outputs of the ComposeID action'. Make sure you after you've selected this (or before) you wrap the outputs in single quotes. ne in the filter query stands for not equal to.

     

     

    4. Next, add a na apply to each and in the output field select value from the dynamic content box. Next, add a compose action and select Reference from the get items section of the dynamic content box.

     

    Next, add another compose action and click in the inputs field. In the dynamic content box click the expression tab and use the following expression: substring(outputs('ComposeReference'),0,7)and what this does is to get the first 7 characters of the Reference column in the list item.

     

     

    5. Next add a condition and in the left field select the outputs of the ComposeShortRef action then is equal to then in the right field select the outputs of the ComposeNewItemReference.

     

    Leave the red if no channel empty. But in the green if yes channel add an increment variable action, click in the name field and select the variable you created earlier and set the value to 1. So for each item it finds with the first 7 characters of the reference being M555125 it will add 1 to the variable to give a count.

     

     

    6. Next, outside the apply to each add another compose action and add 1 to the varCounter variable which will give us the next reference number using the expression add(variables('varCounter'),1)

     

     

    7. Finally, an an update item action and select the outputs of the ComposeNewItemReference then a dash - then the outputs of the Compose+1 action.

     

     

    So we add a new Martinique item:

     

     

    The flow runs, calculates how many items are already there for Martinique, adds 1 to the total and updates the item with the new reference number:

     

     

    Rob
    Los Gallardos
    Microsoft Power Automate Community Super User.
    Principal Consultant, SharePoint and Power Platform WSP UK (and classic 1967 Morris Traveller driver)

    • em_amdahl's avatar
      em_amdahl
      Copper Contributor
      It doesn't like step #4 with the substring.

      The value of first parameter is of type 'Null' but the function 'substring' expects a string.https://aka.ms/logicexpressions


      Unable to process template language expressions in action 'Compose_4' inputs at line '0' and column '0': 'The template language function 'substring' expects its first parameter 'string' to be a string. The provided value is of type 'Null'. Please see https://aka.ms/logicexpressions#substring for usage details.'.

Resources