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.