This stored procedure can be executed to generate the next sequential sales document number; this script was created to get the next sales invoice number for a transaction to be inserted into Microsoft Dynamics GP through eConnect. I write stored procedures as a wrapper around the eConnect stored procedure as we are often working with the clients IT department or a third party and this abstracts the call way from the other application so any changes by Microsoft can be managed within the wrapper stored procedure rather than the application.
This particular example is for generating a sales order number, but can be used to generate a document number for any of the sales document transactions. To do this, change the first highlighted parameter to one of the following numbers:
- Quote
- Order
- Invoice
- Return
- Back Order
- Fulfillment Order
The second parameter should be set to the Doc ID which will vary depending on how you have configured Sales Order Processing.
The stored procedure to get the next sales document number is:
-- drop stored proc if it exists[/sqlgrey]
IF OBJECT_ID (N'usp_AZRCRV_GetNextSOPDocumentNumber', N'P') IS NOT NULL
DROP PROCEDURE usp_AZRCRV_GetNextSOPDocumentNumber
GO
-- create stored proc
CREATE PROCEDURE usp_AZRCRV_GetNextSOPDocumentNumber AS
/*
Created by Ian Grieve of azurecurve | Ramblings of an IT Professional (http://www.azurecurve.co.uk)
This code is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0 Int).
*/
BEGIN
DECLARE @return_value INT
DECLARE @I_tSOPType TINYINT = 2
DECLARE @I_cDOCID CHAR(15) = 'STDORD'
DECLARE @I_tInc_Dec TINYINT = 1
DECLARE @O_vSopNumber AS VARCHAR(21)
DECLARE @O_iErrorState INT
EXEC @return_value = taGetSopNumber @I_tSOPType = @I_tSOPType, @I_cDOCID = @I_cDOCID, @I_tInc_Dec = @I_tInc_Dec, @O_vSopNumber = @O_vSopNumber OUTPUT, @O_iErrorState = @O_iErrorState OUTPUT
SELECT @O_vSopNumber
END
GO
-- grant execute permission on stored proc to DYNGRP
GRANT EXECUTE ON usp_AZRCRV_GetNextSOPDocumentNumber TO DYNGRP
GO
You can execute the stored procedure using the below:
-- execute stored proc
EXEC usp_AZRCRV_GetNextSOPDocumentNumber
GO
I’ve written similar stored procedures in the past in other next numbers for other parts of Dynamics GP:
I have also posted a custom solution which can be used to generate a next number when there isn’t a method available in eConnect.
What should we write about next?
If there is a topic which fits the typical ones of this site, which you would like to see me write about, please use the form, below, to submit your idea.
That query will fail as the @I_cDOCID parameter is actually a char(15), not TINYINT.
Thanks for the heads up, Mariano. I did have a working version of this, but it looks like something happened while I ws writing the post.
I do usually test before publication, so not sure how I missed this.