Get Next Document Number with eConnect

1 Comment

eConnect provides a way to automatically get and set the next document number in Dynamics GP. This works for some document types. Here we will go through an example of getting the next document number.

In the GP user interface, we see there are options when creating a SOP transaction. We would need to select the type of document (quote, order, fulfillment order, invoice, return, backorder) and then select the type of document once selected based on our setup (e.g. in TWO company, STDQTE, STDORD, STDINV etc):

First, create a new Visual Studio console application:

Next, right click on references and select Add Reference:

Browse out to the eConnect assembly, located in the eConnect API folder:

Add a using statement:

  • using Microsoft.Dynamics.GP;

Add a variable for GetNextDocNumbers. Notice the different methods available:

The types of documents we can get the next number of include:

  • GL Journal
  • Inventory transaction
  • PO
  • PO Receipt
  • RM
  • SOP
  • PM Voucher

If we select GetNextSOPNumber, notice the parameters we need to supply:

  • Increment or Decrement
  • DOCIDKey. E.g. STDINV
  • DocType. This is the type of document, e.g. Quote, Order, Invoice.
  • BackOfficeConnectionString. This is a connection string to the SQL Server database

Notice there is also a separate method for GetSopNumber. The other document types do not have a separate method as well:

We will run both of these. The code should look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Dynamics.GP;

namespace Carl.GP.GetNextNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            string connString = string.Empty;
            string sqlserver = "localhost";
            string database = "TWO";

            connString = "data source=" + sqlserver + ";" + "initial catalog=" + database + ";" + "Integrated Security=SSPI;";

            // Get Next SOP Number - Method 1
            Microsoft.Dynamics.GP.eConnect.GetNextDocNumbers getNextDocNumbers = new Microsoft.Dynamics.GP.eConnect.GetNextDocNumbers();
            Console.WriteLine(getNextDocNumbers.GetNextSOPNumber(Microsoft.Dynamics.GP.eConnect.IncrementDecrement.Increment, "STDINV", Microsoft.Dynamics.GP.eConnect.SopType.SOPInvoice, connString));

            // Get Next SOP Number - Method 2
            Microsoft.Dynamics.GP.eConnect.GetSopNumber getSOPNumber = new Microsoft.Dynamics.GP.eConnect.GetSopNumber();
            Console.WriteLine(getSOPNumber.GetNextSopNumber(3, "STDINV", connString));

            Console.ReadLine();
        }
    }
}

Run the code:

If we try the same thing with an Inventory Transaction, note in the UI there are 2 document types – Adjustment and Variance:

Add the code:

Microsoft.Dynamics.GP.eConnect.GetNextDocNumbers getNextDocNumbers = new Microsoft.Dynamics.GP.eConnect.GetNextDocNumbers();
Console.WriteLine(getNextDocNumbers.GetNextIVNumber(Microsoft.Dynamics.GP.eConnect.IncrementDecrement.Increment, Microsoft.Dynamics.GP.eConnect.IVDocType.IVAdjustment, connString));

Run and note the outcome:

This functionality is useful when integrating Dynamics GP with an external system. The new number generated will be reserved by GP.

Note this functionality also exists through SQL. Notice in the company database there are stored procedures with Get Next functionality:

Scripting the procedure, we can call it and receive the output.

Code:

USE [TWO]
GO

DECLARE @RC int
DECLARE @I_tIVDOCTYP tinyint
DECLARE @I_tInc_Dec tinyint
DECLARE @O_vIvNumber varchar(21)
DECLARE @O_iErrorState int

SET @I_tIVDOCTYP = 1
SET @I_tInc_Dec = 1

EXECUTE @RC = [dbo].[taGetIvNumber] 
   @I_tIVDOCTYP
  ,@I_tInc_Dec
  ,@O_vIvNumber OUTPUT
  ,@O_iErrorState OUTPUT

SELECT @O_vIvNumber, @O_iErrorState

GO

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

One Response to Get Next Document Number with eConnect

  1. I am creating a Cash Receipt in Great Plains using “CreateCashReceiptAsync” endpoint in GP webservices. This is correctly creating the cash receipt.

    Now how do I apply this cash receipt to an receivable invoice using GP webservices.

Leave a Reply

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