SQL Stored Procedure to Get the Next Microsoft Dynamics GP Sales Document Number

Microsoft Dynamics GPThis 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:

  1. Quote
  2. Order
  3. Invoice
  4. Return
  5. Back Order
  6. 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.

Your Name

Your Email

Suggested Topic

Suggestion Details

Looking for support or consultancy with Microsoft Dynamics GP?

I no longer work with Microsoft Dynamics GP, but the last company I worked for was ISC Software in the UK; if you’re looking for support or consultancy services with Microsoft Dynamics GP you can contact them here.

2 thoughts on “SQL Stored Procedure to Get the Next Microsoft Dynamics GP Sales Document Number

  1. That query will fail as the @I_cDOCID parameter is actually a char(15), not TINYINT.

    1. Ian Grieve says:

      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.

Leave a Reply

Your email address will not be published. Required fields are marked *