You are viewing limited content. For full access, please sign in.

Question

Question

Increment Counter for each document then reset each month

asked on February 1, 2019 Show version history

Hi all.

 

Using Workflow, I'm trying to generate a number. I have some conditions.

This number should be unique, start with MMYYYY, have 10 caracters and should reset each month.

Exemple : first number : 0220190001

next one : 0220190002

next month : 0320190001

 

How can I proced?

 

Thanks in advance.

Regards

 

Edit : I forgot : The number is generated for each process. I can't use the loops.

0 0

Answer

SELECTED ANSWER
replied on February 1, 2019

Hi Olivier,

 

I have a workflow that does something very similar to this:

Query Data

I have a SQL table that holds a single value:

Create Tokens

Now that I have the most recently used ID, I create an empty token that will later hold the updated ID.

 

Pattern Match

Since the value is very controlled and can be easily parsed, I use a pattern match activity to separate the ID into its individual components. For you, this would be Month, Year, and ID Number. You could use the patterns below:

Month:  ^(\d{2})

Year: ^\d{2}(\d{4})

ID:  (\d{4})$

 

Conditional Decision

Next, I used a conditional decision to identify whether the ID needs to be incremented (same year) or reset (new year) (screenshot below of the current year branch conditions, the new year branch does not have conditions). For you, this could be something like incremented (same month and year) or reset (new month).

 

Current Year Branch

Within the Current Year branch, I first increment the ID number using a token calculator. Then I apply the result of that to the ID token that was created above (Create Request ID Token).

The formatting "D5" ensures that there will be 5 digits for example 00001 or 00100. To get your expected format you would use "D4".

 

New Year Branch

In the New Year Branch, I simply apply a value to the ID token as I know that the ID will be 00001 (for you 0001).

 

Update Data

Finally, I update the SQL table with the now current ID value. 

 

Now that you have the current ID in the ID token, you can do whatever else you would like with it.

 

Hope this helps!

1 0
replied on February 1, 2019

This method is fine as long as you aren't worried about the possibility of multiple instances of the workflow running at the same time. Otherwise, you could end up with a workflow pulling a number, and while it's processing, another workflow pulls the same number before the first workflow increments the number.

Sequences in SQL Server, as indicated in the linked post, are guaranteed to not have this problem.

2 0
replied on February 2, 2019

I was thinking the same thing. Aaron's workflow doesn't look reliable, in a situation where multiple document's are processed in rapid succession. 

0 0
replied on February 4, 2019

Hi Devin and Ben,

 

Thanks for the feedback. Your worries can be solved by wrapping the described workflow in a "Simple Synchronization Sequence" workflow action. This will prevent multiple instances of workflow from accessing a lookup table simultaneously.

 

By using workflow to perform this process, you are creating a simple and easy to understand process that is also highly scalable if needed. I worry, and this will be different for each company, that using a complex SQL statement may be very difficult for users to troubleshoot or rework if needed.

0 0
replied on February 4, 2019

Hi Aaron,

I might have missed something obvious here but my understanding of the SSS is to manage access of multiple workflows, or multiple branches of the same workflow, to a sing Entry ID.

It doesn't seem to me that it will be of any use in this scenario. Can you post up a screenshot of how you would use SSS to prevent multiple Entry IDs from attempting to access the same SQL, when the workflow is executed multiple times, simultaneously?

-Ben

0 0
replied on February 4, 2019

Hi Ben,

 

After doing a little digging, it appears you are correct. 

 

I agree if your organization has the possibility of running the workflow above workflow more than once within 5 seconds or so, then I wouldn't use the workflow, for example, I would not recommend this workflow on importing multiple documents at a single time.

 

This workflow would be very helpful for a forms process that requires a unique identifier in a custom format, as one example.

 

Probably the easiest way to generate a unique identifier that has the date on it is to use the already unique ID that is given to each document on import into LF (Entry ID). then just combine it with the date token in some format. This doesn't give you the reset to zero that Olivier was looking for, but it does guarantee that it's a unique ID, has a very quick runtime, provides scalability, and is extremely easy to troubleshoot and/or reuse.  

0 0

Replies

replied on February 4, 2019 Show version history

Here is how I would tackle this.

First create an SQL Sequence

CREATE SEQUENCE [dbo].[RecordSeq]
    START WITH 1
    INCREMENT BY 1
    MINVALUE 1
;

Then Create a Table in SQL

CREATE TABLE RecordID (
    ID int NOT NULL IDENTITY,
    RecordNumber bigint NOT NULL,
    YearMonth date DEFAULT GETDATE() 
);

Finally, in a Workflow Custom Query activity, use the following

-- Variables
DECLARE @NextVal BIGINT
DECLARE @CurDate DATE
DECLARE @RetVal INT
-- Get New Record ID
SELECT @NextVal = NEXT VALUE FOR [dbo].[RecordSeq]
-- Get Date
SELECT @CurDate = GETDATE()
-- Check if table already had entries for the month
SELECT @RetVal = COUNT(*) FROM [dbo].[RecordID] WHERE LEFT(CONVERT(VARCHAR(10),YearMonth,112),6) = LEFT(CONVERT(VARCHAR(10),@CurDate,112),6)
IF (@RetVal > 0) -- Already has entries in the month
  BEGIN
    -- Create new record entry
    INSERT INTO [dbo].[RecordID] (RecordNumber) VALUES (@NextVal)
  END
ELSE
  BEGIN -- Does not have record entry for the month
    -- Reset the Sequence
    ALTER SEQUENCE [dbo].[RecordSeq]
    RESTART
    -- Get New Record ID
    SELECT @NextVal = NEXT VALUE FOR [dbo].[RecordSeq]
    -- Create new record entry
    INSERT INTO [dbo].[RecordID] (RecordNumber) VALUES (@NextVal)
  END
-- Get the record entry ID in needed format
SELECT RIGHT(LEFT(CONVERT(VARCHAR(6), YearMonth, 112),6),2) + LEFT(CONVERT(VARCHAR(4),YearMonth,112),4) + RIGHT(REPLICATE('0',3) + CONVERT(VARCHAR(4),RecordNumber,4),4) AS RecID
FROM [dbo].[RecordID]
WHERE [YearMonth] = @CurDate AND [RecordNumber] = @NextVal;

 

2 0
replied on February 1, 2019

It's easy to know the month inside a Workflow. The trickier part is maintaining the sequence.

Have a look at this thread.

https://answers.laserfiche.com/questions/153359/Generate-Sequential-Numbers-in-Workflow

1 0
replied on February 1, 2019

Hi Thank you all.

@████████ I did what you said and it looks pretty good.

Thank you.

Regards

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.