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

Question

Question

Forms executes stored procedures twice if passing in a parameter

asked on October 14, 2015

On one of our forms, we have a stored procedure that generates unique, sequential purchase order numbers when the form loads. In addition to returning the generated number from a SQL Sequence Object (and prepending it with 'CN'), the stored procedure also puts it into a table, along with the date, so that we can reference it later if need be:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[GetPONumberSequence] 
AS
BEGIN
    SET NOCOUNT ON;

	DECLARE @sequence nvarchar(50)
	SET @sequence = 'CN' + Cast((NEXT VALUE FOR dbo.PONumberSequence) as nvarchar(50))
   
    INSERT INTO [dbo].[PONumbersSequence]
           (LastUsedPONumber)
    VALUES
           (@sequence)
    SELECT TOP 1 [LastUsedPONumber] FROM dbo.PONumbersSequence 
		ORDER BY "DateInserted" DESC 
END

GO

This has been working great.

Recently, we saw the need to keep track of which Forms user is generating the purchase order number. Forms has the ability to pass parameters back to a stored procedure (it uses the same code, according to Robert Gordon), so we created a new stored procedure that takes in a @User parameter:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[GetPONumberSequenceWithUser]
    @User nvarchar(50)
AS
BEGIN
    SET NOCOUNT ON;

	DECLARE @sequence nvarchar(50)
	SET @sequence = 'CN' + Cast((NEXT VALUE FOR dbo.PONumberSequenceWithUser) as nvarchar(50))
   
    INSERT INTO [dbo].[PONumbersSequenceWithUser]
           (LastUsedPONumberWithUser, Submitter)
    VALUES
           (@sequence, @User)
    SELECT TOP 1 [LastUsedPONumberWithUser] FROM dbo.PONumbersSequenceWithUser 
		ORDER BY "DateInserted" DESC 
END

GO

We then updated the Lookup Rule in Forms to pass the User field's value as the User variable.

The issue is that now the stored procedure executes twice. We know this because the sequence object is incremented twice, and the record table has two entries:

The rows with "Travis Dyer" as the submitter are inserted when the stored procedure is executed by Forms. You'll note that the DateInserted column shows pairs of records within a second or less of each other, indicating that it ran twice.

There is also a "test1" row, which was created by running the stored procedure from SQL Management Studio. No duplicate rows were generated that way, so we have a good suspicion that this is a Forms bug.

Our project is dead in the water due this problem and we'd like some input from Laserfiche if possible.

0 0

Replies

replied on October 15, 2015

We were able to recreate your issue.  But I was wondering what would you expect to happen if the user refreshed the page?  I assume even if I fixed the code you would then get two items in the table.  

Perhaps your stored procedure can be a little smarter.  There are number of unique ids on the page when you load that you can use to identify the submission for example uqid or routing ResumeId if this is taking place inside a task.  That way you can be sure that only one item is added to the table.  

Another approach which is even better is to use Workflow with Forms.   You can use the old approach to get a unique id and then run a workflow service to populate the table with the name and the id.

We don't want to call the forms server with a lookup twice and should resolve this issue in a future release.

 

2 0
replied on October 15, 2015

I like the unique ID approach. I put a Form UQID field (will be hidden) that gets populated with the unique id when the form loads:

$('#q44 input').val($('[name="uqid"]').val());

I also added a UQID column to the table and improved the logic in the Stored Procedure to run only if the UQID is not empty (because the SP runs once at the very beginning before the unique ID field is populated, sending a blank value) AND if it doesn't already exist in the record table:

ALTER PROCEDURE [dbo].[GetPONumberSequenceWithUser]
    @User nvarchar(50),
    @UQID nvarchar(100)
AS
IF (DATALENGTH(@UQID) > 0 AND NOT EXISTS (SELECT LastUsedPONumberWithUser FROM [dbo].[PONumbersSequenceWithUser] WHERE UQID = @UQID))
BEGIN
    SET NOCOUNT ON;

    DECLARE @sequence nvarchar(50)
    SET @sequence = 'CN' + Cast((NEXT VALUE FOR dbo.PONumberSequenceWithUser) as nvarchar(50))
   
    INSERT INTO [dbo].[PONumbersSequenceWithUser]
           (LastUsedPONumberWithUser, Submitter, UQID)
    VALUES
           (@sequence, @User, @UQID)
    SELECT TOP 1 [LastUsedPONumberWithUser] FROM dbo.PONumbersSequenceWithUser 
		ORDER BY "DateInserted" DESC 
END

After refreshing the data source and updating the lookup rule in Forms, I noticed the "autofill" button appear next to the Form UQID field. So I added a simulated click right after populating the field with the uqid of the form:

$('#q44 input').val($('[name="uqid"]').val());
$('#q44').find('button').click();

It seems to work fine... the PO number gets generated when the form loads without any input from the user:

And more importantly, only one number gets generated per form:

I have to admit however that this feels more like a "dirty hack" than a clean workaround. I'm not sure how well it will hold in a production setting. We'll hide the Form UQID field on the form, do a slow roll-out to the users and see how things go. This form is critical to the customer's business, so I'm hoping it works fine in production and we don't end up needing a service pack before version 10!

0 0
replied on November 26, 2015

Hi Ege,

 

I tried your SQL store proc, and failed to make it work, I imagine you did not run the store proc with the seach rules.

 

did you run it by using the java?

0 0
replied on October 15, 2015

We monitor the field linked to the input field for any change events associated with it so if you are changing the user field with javascript then you will see the stored procedure run again.  If the field is not modified in your custom javascript then please open a support case so we can look into it further.

1 0
replied on October 15, 2015

This issue occurs when there are multiple lookup rules in the form and one of those rules is just a fill rule that populates a drop down field with some values. We are looking into the matter and will update this thread when there is a resolution.

1 0
replied on June 13, 2017

How has this done in production? I basically put together the same solution and then encountered the duplicate execution of the stored proc in 10.2.1 and am trying your solution to see if I can get it to work.

1 0
replied on October 14, 2015

Until you hear back from Laserfiche about this you could easily get around this by first checking to see if an entry already exists before insertion by comparing submitter name and datetime (down to the second and not millisecond).

0 0
replied on October 15, 2015

The field doesn't have any custom JS. It has a default value of current user's display name. Maybe that is causing the stored procedure to trigger twice? But that doesn't make sense since the user's name is captured during both executions... Should I open a case?

0 0
replied on October 15, 2015

This is not something our development and support teams could recreate when using a generic form/business process and stored procedure.

I have a form where it takes a valuation as an input and then passes that to a stored procedure to get a fee value based off of that valuation. When I set a default value into the valuation and then start the business process, the stored procedure is only executed once as seen in SQL Profiler.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetFee]
	@valuation decimal(10,2)
AS
BEGIN
	SET NOCOUNT ON;
	select fee from valuationfees
	where @valuation between ValuationFrom and ValuationTo
END

However, if further assistance is needed in investigating the matter you're running into, then please do open a support case.

0 1
replied on October 15, 2015

I'm not seeing where this example actually inserts anything?
 

0 1
replied on October 15, 2015

The insert is moot because the actual issue reported is the SP getting executed twice.

0 1
replied on October 15, 2015

"On one of our forms, we have a stored procedure that generates unique, sequential purchase order numbers when the form loads. In addition to returning the generated number from a SQL Sequence Object (and prepending it with 'CN'), the stored procedure also puts it into a table, along with the date, so that we can reference it later if need be:"

They are pulling from a stored procedure when the Form Loads. Your example doesn't do that. Secondly, they referenced that when inserting with one variable it worked and now with two it doesn't. Yours shows neither.

The SP is getting executed twice because it's possible that the pulling of a value on first load and then inserting afterwards is causing the PAGE_LOAD method to fire twice possibly because of multiple occurances of MYBASE.LOAD

0 0
replied on October 15, 2015 Show version history

The cause of the issue has already been determined as Ege had opened a support case. See the note regarding when there are multiple lookup rules on the form where one of those rules is just a fill rule. It was confirmed that if all other lookup rules are removed, leaving only the execution of the stored procedure, then there is no issue. We also determined that if there were multiple lookup rules, then if they followed the format of "when field1 matches this, fill field2 with value from that" there is also no issue. The issue only occurs as soon as you add a lookup rule that just does a fill into some field.

2 0
replied on November 26, 2015

Hi Ege,

 

I tried your SQL store proc, and failed to make it work, I imagine you did not run the store proc with the seach rules.

 

did you run it by using the java?

0 0
replied on June 27, 2017

Are there any updates on an official fix to this?  I seem to be running into the same problem.

 

- Alon

0 0
replied on August 30, 2017 Show version history

Only lookup rule with stored procedure to fill auto number is also not working. Without any parameter SP is getting executed and fills the auto number. As soon it has two parameter, one with username as default value and the second with form ID, filled using jquery it is not working because it takes form ID as blank.

 

How can I execute the stored procedure again once it fills the form ID? I used jquery ajax call but it gets executed even I save any change. Also once user submits the form then it generates new ID so it does not match with existing. I am searching for blogs but still no success.

0 0
replied on August 31, 2017

I got it working using the stored procedure and lookup rule. Actually if you make the field "Form UQID" read-only then it will not work. Instead of that you can make the field read-only using jQuery code. Even I don't require to trigger any click or change event.

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

Sign in to reply to this post.