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

Question

Question

Incremental Number with Stored procedure

asked on May 17, 2016 Show version history

Hi,

I am using this example:

https://www.laserfiche.com/support/webhelp/Laserfiche/10/en-US/administration/#../Subsystems/Forms/Content/Lookup-Rules.htm%3FTocPath%3DForms%7CCreating%2520a%2520Form%7C_____5

 

I have 2 questions:

1. Currently it stores the number as 1, 2,3 & so on, what if I want a custom number, like: 201601, 201602, 201603..... or if it's possible to make it Alphanumeric too, like: INV201601, INV201602....

2. Even if I open the form in Preview mode, it increments the number, I want the number to be incremented only on Submit..

 

I am sure it's possible, just not, HOW 

 

Thanks in advance for your help.

S

0 0

Answer

SELECTED ANSWER
replied on May 17, 2016 Show version history

Hi Sahil,

Try using this as a stored procedure:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE LabTestingSubCount 
AS
BEGIN
  SET NOCOUNT ON;
  DECLARE @count int;
  -- set the padding for the submission count
  DECLARE @padding int = 2;
  -- set the prefix for the submission count
  DECLARE @prefix nvarchar(50) = 'INV2016';
  -- set the name of your Forms process
  DECLARE @process nvarchar(max) = 'ProcessNameHere';
  SET @count = (
    SELECT COUNT(submission_id)
    -- make sure to change FormsDB to your actual Forms database
    FROM [FormsDB].[dbo].[cf_form_submissions]
    WHERE process_id = (
      SELECT bp_id
      -- make sure to change FormsDB to your actual Forms database
      FROM [FormsDB].[dbo].[cf_business_processes]
      WHERE name = @process
      -- account for deleted processes with the same name
      AND is_deleted = 0
    )
    -- step_id is 1 for the initial form submission; edit as appropriate
    AND step_id = '1'
  );
  SELECT @prefix + RIGHT('00000'+CONVERT(VARCHAR,@count + 1),@padding) AS 'count';
END
GO

What this stored procedure does is query the number of submissions at the appropriate step of the appropriate Forms process, and add 1. You can then format the string. When using this in a Forms lookup rule, the return value is treated as the column "count".

Be sure to make the appropriate edits, in particular:

  • The Forms database name; that is not something which can be passed as a variable in SQL (easily)
  • The variable @padding controls the length of the number string; in the example it is 2, so the return would be '01', '02', '03', etc.
  • The variable @prefix controls what is prepended to the number string; in the example it is 'INV2016', so the final return will be 'INV201201', 'INV201602', 'INV201603', etc.
  • The variable @process is the name of the process in Forms; in the example it is ProcessNameHere. Be sure to leave the name inside single quotes.
  • At present, this stored procedure will only count submissions of the initial form in the process; change the value of step_id in line 27 to the appropriate step number.

Hope this helps!

ADDENDUM: When using this stored procedure in a lookup rule, make sure that the user configured for the data source has access to both the database with this stored procedure and to the Forms database.

UPDATE - 19 May: The original code listing as-is would not account for Forms processes with the same name that had been deleted, so in such cases the lookup would not return because of a SQL error. The current code listing reflects change to account for that. (Edit credit to Xiaoxiao Yan)

1 0
replied on July 29, 2016

James,

 

Would you happen to know what to modify in the SQL query, to have the "INV2016" value to dynamically generate based on the current year?

ex: INV(CURRENTYEAR)

Thanks.

0 0

Replies

replied on May 19, 2016

James, 

I opened a support case as after running 

exec LabTestingSubCount

I got the below error....

Msg 512, Level 16, State 1, Procedure LabTestingSubCount, Line 12 
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

 

Lynn from Support was very kind & helped me to figure out & on her suggestion I changed the line

WHERE name = @process 

to 

WHERE name = @process and is_deleted = 0

 

& all is working!!!

 

Thanks to you & of course Lynn!!

 

S

1 0
replied on May 19, 2016

Glad to hear it, Sahil! And glad that Lynn was able to sort this out. It sounds like there was another process named "Diana" which had been deleted, but the record is still stored in the database, so multiple results were returned.

I'll update the code in the marked post to reflect this change.

0 0
replied on May 19, 2016

Glad to hear it, Sahil! And glad that Lynn was able to sort this out. It sounds like there was another process named "Diana" which had been deleted, but the record is still stored in the database, so multiple results were returned.

I'll update the code in the marked post to reflect this change.

replied on May 18, 2016 Show version history

Hi James,

Thanks a lot for your kind help, however as you might have expected I ran into some issues...

Please see the attached Screenshot, which shows how the database looks, I have the Stored procedure made, however I don't know how to make it propagate into Table?

 

My Database name is Diana & so is the process name, I have put it appropriately in the script, please confirm it's correct.

 

Then in the Lookup I set up column result to : count

Unfortunately, after all these it's not showing up the number.

Can you please help, what am I missing?

 

Thanks in advance,

S

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE LabTestingSubCount 
AS
BEGIN
  SET NOCOUNT ON;
  DECLARE @count int;
  -- set the padding for the submission count
  DECLARE @padding int = 2;
  -- set the prefix for the submission count
  DECLARE @prefix nvarchar(50) = 'INV2016';
  -- set the name of your Forms process
  DECLARE @process nvarchar(max) = ‘Diana’;
  SET @count = (
    SELECT COUNT(submission_id)
    -- make sure to change FormsDB to your actual Forms database
    FROM [Diana].[dbo].[cf_form_submissions]
    WHERE process_id = (
      SELECT bp_id
      -- make sure to change FormsDB to your actual Forms database
      FROM [Diana].[dbo].[cf_business_processes]
      WHERE name = @process
    )
    -- step_id is 1 for the initial form submission; edit as appropriate
    AND step_id = '1'
  );
  SELECT @prefix + RIGHT('00000'+CONVERT(VARCHAR,@count + 1),@padding) AS 'count';
END
GO

The user has full rights to the DB & also to the forms process.

Screen Shot 2016-05-18 at 10.37.35.png
0 0
replied on May 18, 2016

Hi Sahil,

Don't use the Diana database; use whatever database is configured in the Forms configuration page under the database tab. The idea is to query Forms data directly to determine how many submissions have already been made, then increment that value and display the (formatted) result. It does not actually query the Diana database with your other lookup data. However, I do highly recommend to not create the stored procedure directly in the Forms database itself.

So the setup is: in Diana, create the stored procedure. In the stored procedure, reference the other Forms database, not Diana. It should work then. The only other caveat I can think of would be if you are not on Forms 10.1, the database schema is slightly different but I do not think any differences will affect this procedure.

Hope this helps!

0 0
replied on May 18, 2016

I'm using 10.1, so it should be fine.

I will try tomorrow & keep you posted

 

Thanks

0 0
replied on May 18, 2016

Sounds good.

Also note that this solution is meant to address the concern of the number incrementing both during live submissions and during previews; however, it will not address situations where multiple instances of the form are open at once --- it is possible that if both User A and User B start the form before either submits it, they will be assigned the same number.

If the above scenario is a possibility, you may want to rethink the process, in particular when the invoice number is assigned. It may be better to generate this in the step after submission, say in a second form for a user task or by using a Workflow step before sending an email confirmation. Trying to display a unique number before the form is submitted, with no gaps or duplicates, isn't really possible because there is no way to account for forms which have not yet been submitted.

Cheers

0 0
replied on May 18, 2016

I don't want it to increment during preview, is that possible?

Only on submission

0 0
replied on May 18, 2016

Yes, it is possible and this stored procedure will handle that case.

0 0
replied on May 18, 2016

Awesome!!!

0 0
replied on May 18, 2016 Show version history

Hi James,

I could not resist & tried, but again it doesn't populate the number :(

Please see attached screenshot for how I have configured the Lookup & the Stored procedure, the forms DB's name is Forms

 

Laserfiche Forms Version 10.1.0.559

 

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE LabTestingSubCount 
AS
BEGIN
  SET NOCOUNT ON;
  DECLARE @count int;
  -- set the padding for the submission count
  DECLARE @padding int = 2;
  -- set the prefix for the submission count
  DECLARE @prefix nvarchar(50) = 'INV2016';
  -- set the name of your Forms process
  DECLARE @process nvarchar(max) = 'Diana';
  SET @count = (
    SELECT COUNT(submission_id)
    -- make sure to change FormsDB to your actual Forms database
    FROM [Forms].[dbo].[cf_form_submissions]
    WHERE process_id = (
      SELECT bp_id
      -- make sure to change FormsDB to your actual Forms database
      FROM [Forms].[dbo].[cf_business_processes]
      WHERE name = @process
    )
    -- step_id is 1 for the initial form submission; edit as appropriate
    AND step_id = '1'
  );
  SELECT @prefix + RIGHT('00000'+CONVERT(VARCHAR,@count + 1),@padding) AS 'count';
END
GO

 

 

Any suggestions what am I missing?

 

Thanks

S

Screen Shot 2016-05-18 at 9.57.23 PM.png
0 0
replied on June 18, 2021 Show version history

Hi Everyone,

I know this post is old, but I need to know how to format the output of a stored procedure to look like this "EML-2021-0615-001".

Hoping for your response.

Thanks

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

Sign in to reply to this post.