Supporters of End User
Web

SharePoint: How to Create an Auto-Incrementing Number Field For Use in a Custom ID - Part 3

Item is currently unrated. Press SHIFT+ENTER to rate this item.1 star selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+ESCAPE to leave rating submit mode.2 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.3 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.4 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.5 stars selected. Press SHIFT+ENTER to submit. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.
Categories: 

In the previous article, we created our "Initialize Workflows" workflow that will be used on item creation to allow us the chance to enter in all fields necessary in the document details, and set the starting variable for our main workflow, which we're going to create now.

The Nuts and Bolts

Before we begin, it's important to understand the foundation upon which the workflow logic will run.  In order to have the auto-incrementing number functionality, we need a way in which we can get the next number, increment it, then apply it back to our master list.  We'll accomplish this by using the built-in system-created ID field that is used on each and every list and library within SharePoint to compare the value of the current item on the list (in this case, the document we just uploaded to our "Projects Library") with that of the matching ID on the "Auto ID Master" list.

The above comparison is the absolute foundation for how the auto-incrementing logic works.  The “ID” of the most recent item on the master list will always be 1-higher than that of the most recent item in the project document library.  When we add in a new item to that library, its ID will now match the most recent ID in the master list…so when we run the workflow, it will match up.  The importance of this will become more and more apparent as we build out the rest of the workflow, but the key element to remember at this point is that the comparison of the ID’s is what makes this work.

And again, DO NOT CREATE ANY ITEMS ON THE LIST, OR ADD ANY DOCUMENTS TO THE LIBRARY YET!!!!!  wait until the end of the series where we test things.

Getting Started

To start, open SharePoint Designer, connect to our site and create a new workflow.

Name the workflow "Auto ID", attach it to the "Projects Library" and set it start options to "Automatically start this workflow whenever an item is changed".

Since we added in an update action in the "Initialize Workflows" workflow that will set the "Auto Number" field to "xxxx", this new workflow will only start once that has occurred.

Click "Next" to move to the next screen for specifying the workflow steps details.

In order to make the following steps easier to follow, we're going to be building the workflow in two separate pieces: the first will be for new projects added to the library, and the second will handle new versions added to existing projects.

For the first piece, we need to determine if the item added is a new version.  We do this by adding a condition that must be satisfied before the workflow can proceed.  Additionally, in order to make sure that this will only run for "new" items added, we'll verify that the "Initialize Workflows" workflow has indeed performed its update.

Under the "Conditions" menu, select "Compare Projects Library Field".

Click the "field" link and choose "Version Code" in the dropdown then click the "value" link and choose "A" from the dropdown.

Under the "Conditions" menu, select "Compare Projects Library Field" a second time.

Click the "field" link and choose "Run Main Workflow" from the dropdown then click the "value" link and choose "Yes" from the dropdown.

With these conditions set, the workflow will now only run if the document added has a version code of "A" (signifying that it is a "new" project), and if the "Run Main Workflow" value is set to "Yes" (which at this point, it would be since our "Initialize Workflows" workflow sets this value).  Both of these conditions must be "TRUE" in order to proceed.

Next, we'll define the actions that are to occur if the workflow starts.

We'll be using a couple variables to hold the value of the current number and that of its incremented value, so we'll create these first before we define the actions that the workflow will take.

At the bottom of the main workflow designer window, click "Variables".

In the window that opens, click "Add" to create a new variable called "CurrentNumber" and set its type as "Number" then click "OK" to add the variable.

Click "Add" a second time and create another new variable called "NextNumber" as a "Number" type and add it to the list of variables.

Once both variables have been created, click "OK" to return to the main workflow designer window.

Under the "Actions" menu, click "Do Calculation".

Click the first "Value" link then the "fx" button to open a "Define Workflow Lookup" window.

In the "Source" dropdown, select "Auto ID Master" - this will expand the window to allow us to further define the value we're looking for in our calculation.

In the upper "Field" dropdown ("Lookup Details" section), select "Next Number Available".

In the lower "Field" dropdown ("Find the list item" section), select "Auto ID Master:ID".

Click the "fx" button next to the "Value" field to open a new "Define Workflow Lookup" window and select "Current Item" in the "Source" dropdown and "ID" in the "Field" dropdown then click "OK" to return to the first lookup window.

Remember how I said earlier about how we'd be performing comparisons on the "ID" values of the current item and that of the matching item on the master list?...well, this is it in action.

Click "OK" to close the window and return to the main workflow designer window.

Click the remaining "value" link and type in a zero "0" then click the "Variable: calc" link and select "Variable: CurrentNumber" from the dropdown.

Perform the exact same steps a second time, but for the second "value" link, type in a one "1" and assign it to the "NextNumber" variable.

Under the "Actions" menu, click "Do Calculation".

Click the first "Value" link then the "fx" button to open a "Define Workflow Lookup" window.

In the "Source" dropdown, select "Auto ID Master" - this will expand the window to allow us to further define the value we're looking for in our calculation.

In the upper "Field" dropdown ("Lookup Details" section), select "Next Number Available".

In the lower "Field" dropdown ("Find the list item" section), select "Auto ID Master:ID".

Click the "fx" button next to the "Value" field to open a new "Define Workflow Lookup" window and select "Current Item" in the "Source" dropdown and "ID" in the "Field" dropdown then click "OK" to return to the first lookup window.

Click "OK" to close the window and return to the main workflow designer window.

Click the remaining "value" link and type in a zero "1" then click the "Variable: calc" link and select "Variable: NextNumber" from the dropdown.

Now that we have our two variables with values assigned, we'll move through the remaining actions to create a new item on the master list, delete the old one, update the current item in the project library to set its auto number value and workflow "Run" value, then end the workflow.

Under the "Actions" menu, select "Create List Item".

Click the "this list" link to open a new "Create New List Item" window and choose "Auto ID Master" in the "List" dropdown (this will add "Title(*)" in the field section).

Click to select "Title(*)" then click the "Modify" button to open a "Value Assignment" window.

In the "To this value" field, type in "Next Available Number" then click "OK" to return to the "Create" screen.

Click the "Add" button to open a "Value Assignment" window and choose "Next Number Available" in the "Set this field" dropdown then click the "fx" button.

In the "Define Workflow Lookup" window that opens, select "Workflow Data" in the "Source" dropdown and "Variable: NextNumber" in the "Field" dropdown then click "OK" to return to the "Value Assignment" window.

Click "OK" to return to the "Create New List Item" screen then "OK" to return to the main workflow designer window.

Now that we've created a new item on the master list that has the incremented value, we can go ahead and delete the previous item on the same list...which we'll do by using a comparison of ID's on the item we just created in our "Projects Library" and that of the item on the "Auto ID Master" list that matches.

Under the "Actions" menu, select "Delete Item".

Click the "this list" link and in the "Choose List Item" window that opens, select "Auto Id Master" in the "List" dropdown - this will expand the window to allow us to further define the item we want to delete.

Using the same technique we've already done a couple times, we match up the ID comparison by selecting "Auto ID Master:ID" in the "Field" dropdown then click the "fx" button to open a new "Define Workflow Lookup" window and choose "Current Item" for its "Source" and "ID" for its "field" values.

Then click "OK" to close the "Define Workflow Lookup" window and "OK" again to close the "Choose List Item" window and return to the main workflow designer window.

Now that we've created our new item on the master list and deleted the old one (this prevents more than one number from ever being displayed in the "New Project" lookup field and makes sure that for new projects you only use whatever the next available number is), we need to update the current item in the "Projects Library" to set its "Auto Number" field to the number of the item we just deleted from the "Auto ID Master" list.  We have to do this now because since we just deleted the item, the lookup value present in the current item in the library will now be blank (it doesn't exist anymore, so the value disappears from the field)...but since we captured that value at the beginning of this workflow and assigned it to a variable, we still have access to it.

Under the "Actions" menu, select "Update List Item".

Click the "this list" link to open the "Update List Item" window.

Leave the "List" dropdown as is (should have "Current Item" already selected) and click the "Add" button to define which fields we want to update.

In the "Value Assignment" window that opens, select "Auto Number" in the "Set this field" dropdown then click the "fx" button to open a new "Define Workflow Lookup" window and choose "Workflow Data" in the "Source" dropdown and "Variable: CurrentNumber" in the "Field" dropdown then click "OK" to return to the "Value Assignment" window.

Click "OK" to return to the "Update" window.

Since we're performing an update on the item, AND the workflow it set to start whenever an item is updated, we need to also update the "Run Main Workflow" field in order to make it so when the workflow starts, its initial condition to check for will fail.

Click the "Add" button again to open a "Value Assignment" window and select "Run Main Workflow" in the "Set this field" dropdown and "No" in the "To this value" dropdown then click "OK" to close the window.

Click "Ok" to close the "Update..." window and return to the main workflow designer window.

The last step is to add an action that will stop the current workflow.  This step isn't actually necessary for the workflow to operate properly, but since this workflow runs whenever an item is updated and we're performing an update within the workflow itself, I always like to add in an action to manually end the current workflow so there's only ever one instance running at a time.  Realistically though, since in our update we're setting the "Run Main Workflow" field to a value that will make the next "run" of the workflow stop immediately, adding in a manual stop will act more like a "Safety Net" in case something doesn't work correctly.

Under the "Actions" menu, select "Stop Workflow" then click the "this message" link and type in "Workflow completed with new project added".

Summary

In our workflow we've defined a series of actions that will provide the functionality for the auto-incrementing number and ability to create our final "Project ID".

We created 2 variables for capturing the current "Next Available Number" from the "Auto ID Master" list that allows us to preserve the number even after the item has been deleted so we can use it in our current item in the "Project Library", and to allow us to increment that number and use it to create a new item in the "Auto ID Master" list for the next item added to the "Projects Library.

Since the workflow itself runs when an update is performed, we also added some logic in the actions to define only specific time when we want it to run, and how to stop it after it has.

With this, we have the first piece of the workflow completed.  All that's left now is to build out the second half of the workflow that will handle new "Versions" of documents added to existing projects...which we'll do next time.

For now though, you can go ahead and click "Finish" on the main workflow designer page to build and deploy  the workflow to our document library.  Next time, we'll re-open the existing workflow and continue on by adding in a new conditional branch to handle versions.

And again, DO NOT CREATE ANY ITEMS ON THE LIST, OR ADD ANY DOCUMENTS TO THE LIBRARY YET!!!!!  wait until the end of the series where we test things (this way we don't have to go back and cleanup the list to get it back to a synched state where we'll be testing things).

Till then...
- Dessie

Comments

SharePoint: How to Create an Auto-Incrementing Number Field For Use in a Custom ID - Part 4 | EndUserSharePoint.com

SharePoint: How to Create an Auto-Incrementing Number Field For Use in a Custom ID – Part 3

[...] the previous article we built out the first part of our "Auto ID" workflow to handle new items added to the [...]

Posted 14-Jun-2010 by SharePoint: How to Create an Auto-Incrementing Number Field For Use in a Custom ID - Part 4 | EndUserSharePoint.com
Ron Kats

Error AutoID workflow

When deleting the error "Error deleting a list item" occurs. The system account is granted full access. Any suggestions are welcome.

Posted 04-Oct-2011 by Ron Kats

Notify me of comments to this article

E-mail:
   

Add Comment

Title:

 
Comment:
Email:

   


Name:

 
Url: