web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

Why are we getting multiple distribution lines when creating a sales invoice?

(0) ShareShare
ReportReport
Posted on by 45

I’m using Microsoft Dynamics GP.  First of all, I am a programmer by job title.  I have no training and only limited usage on using the Dynamics GP product.  I wrote a program that uses the GP Web Services to create a sales invoice.  The sales invoice includes 2 distribution lines.  However, when the accountants look at GP, they see 4 distribution lines; each of the lines that my program adds has been duplicated.  What am I doing wrong?  Any other ideas on why this is happening?

 Here’s the code for adding the distribution lines (GL codes have been hidden for privacy issues):

 

                //Adds the Distributions - where it hits the GL Accounts

                salesInvoice.Distributions = new SalesDistribution[2];

 

                SalesDistribution apSalesDistribution = new SalesDistribution();

                apSalesDistribution.GLAccountKey = new GLAccountNumberKey() { Id = "xx-xx-xxxx" };

                apSalesDistribution.CreditAmount = new MoneyAmount() { Currency = "USD", Value = itemsReceived.CostEach };

                apSalesDistribution.DistributionTypeKey = new DistributionTypeKey() { Id = 1 };

                salesInvoice.Distributions[0] = apSalesDistribution;

 

                SalesDistribution salesDistribution = new SalesDistribution();

                salesDistribution.GLAccountKey = new GLAccountNumberKey() { Id = "yy-yy-yyyy" };

                salesDistribution.DebitAmount = new MoneyAmount() { Currency = "USD", Value = itemsReceived.CostEach };

                salesDistribution.DistributionTypeKey = new DistributionTypeKey() { Id = 2};

                salesInvoice.Distributions[1] = salesDistribution;

 

On this page, there should be 2 lines based on the code above, but there are 4 lines:

pastedimage1659529555930v1.png 

Here’s the full code for creating the sales invoice (again, identifying information has been removed):

 

                    CompanyKey companyKey;

            companyKey = new CompanyKey();

 

            Context context;

            SalesInvoice salesInvoice;

            SalesDocumentTypeKey salesInvoiceType;

 

            CustomerKey customerKey;

            customerKey = new CustomerKey();

 

            BatchKey batchKey;

            SalesInvoiceLine salesInvoiceLine;

            ItemKey invoiceItem;

            Quantity invoiceCount;

 

            Policy salesInvoiceCreatePolicy;

            salesInvoiceCreatePolicy = new Policy();

 

            string salesInvoiceNumber = "";

 

            try

            {

                logger.Info("Creating sales invoice.");

 

                // Create an instance of the service

                DynamicsGPClient wsDynamicsGP = new DynamicsGPClient();

 

                // Create a context with which to call the service

                context = new Context();

 

                // Specify which company to use (sample company)

                companyKey.Id = ((parms.liveMode) ? x : y);

 

                // Set up the context object

                context.OrganizationKey = (OrganizationKey)companyKey;

 

                // Create a sales invoice object

                salesInvoice = new SalesInvoice();

 

                // Create a sales document type key for the sales invoice

                salesInvoiceType = new SalesDocumentTypeKey();

                salesInvoiceType.Type = SalesDocumentType.Invoice;

 

                // Populate the document type key for the sales invoice

                salesInvoice.DocumentTypeKey = salesInvoiceType;

 

                // Create a customer key

                customerKey.Id = "xxxx-xxx";

 

                // Set the customer key property of the sales invoice

                salesInvoice.CustomerKey = customerKey;

 

                salesInvoice.PostedDate = parms.endDate;

 

                // Create a batch key

                batchKey = new BatchKey();

 

                batchKey.Id = "CSA_" + salesOrderDate.ToString("yyMMdd");

 

 

                // Set the batch key property of the sales invoice object

                salesInvoice.BatchKey = batchKey;

 

                // Create a sales invoice line to specify the invoiced item

                salesInvoiceLine = new SalesInvoiceLine();

 

                // Create an item key

                invoiceItem = new ItemKey();

                invoiceItem.Id = itemsReceived.GPPartNumber;

                logger.Info("SalesInvoiceCreator.cs createGPSalesOrder() GP part number used for sales invoice: " + invoiceItem.Id);

 

                // Set the item key property of the sales invoice line object

                salesInvoiceLine.ItemKey = invoiceItem;

 

                // Create a sales invoice quatity object

                invoiceCount = new Quantity();

                invoiceCount.Value = itemsReceived.Quantity;

                logger.Info("SalesInvoiceCreator.cs createGPSalesOrder() Quantity used for sales invoice: " + invoiceCount.Value);

 

 

                // Set the quantity of the sales invoice line object

                salesInvoiceLine.Quantity = invoiceCount;

 

                MoneyAmount unitCost = new MoneyAmount()

                {

                    Value = itemsReceived.CostEach

                };

                logger.Info("SalesInvoiceCreator.cs createGPSalesOrder() Cost used for sales invoice: " + unitCost.Value);

 

 

                salesInvoiceLine.UnitPrice = unitCost;

 

                // Create an array of sales invoice lines

                // Initialize the array with the sales invoice line object

                SalesInvoiceLine[] invoiceLines = { salesInvoiceLine };

 

                // Add the sales invoice line array to the sales line object

                salesInvoice.Lines = invoiceLines;

 

                //Adds the Distributions - where it hits the GL Accounts

                salesInvoice.Distributions = new SalesDistribution[2];

 

                SalesDistribution apSalesDistribution = new SalesDistribution();

                apSalesDistribution.GLAccountKey = new GLAccountNumberKey() { Id = "xx-xx-xxxx" };

                apSalesDistribution.CreditAmount = new MoneyAmount() { Currency = "USD", Value = itemsReceived.CostEach };

                apSalesDistribution.DistributionTypeKey = new DistributionTypeKey() { Id = 1 };

                salesInvoice.Distributions[0] = apSalesDistribution;

 

                SalesDistribution salesDistribution = new SalesDistribution();

                salesDistribution.GLAccountKey = new GLAccountNumberKey() { Id = "yy-yy-yyyy" };

                salesDistribution.DebitAmount = new MoneyAmount() { Currency = "USD", Value = itemsReceived.CostEach };

                salesDistribution.DistributionTypeKey = new DistributionTypeKey() { Id = 2};

 

                try

                {

                    // Get the create policy for the sales invoice object

                    salesInvoiceCreatePolicy = wsDynamicsGP.GetPolicyByOperation("CreateSalesInvoice", context);

                    wsDynamicsGP.UpdatePolicy(salesInvoiceCreatePolicy, new RoleKey { Id = "00000000-0000-0000-0000-000000000000" }, context);

                }

                catch (Exception ex)

                {

                    logger.Error("Error creating sales invoice policy." + Environment.NewLine + "Error: " + ex.Message + Environment.NewLine + "Stack trace: " + ex.StackTrace);

                    throw ex;

                }

 

                try

                {

                    // Create the sales invoice

                    wsDynamicsGP.CreateSalesInvoice(salesInvoice, context, salesInvoiceCreatePolicy);

                }

                catch (Exception ex)

                {

                    logger.Error("Error creating sales invoice." + Environment.NewLine + "Error: " + ex.Message + Environment.NewLine + "Stack trace: " + ex.StackTrace);

                    throw ex;

                }

Categories:
I have the same question (0)
  • Joseph Markovich Profile Picture
    4,007 on at

    If you take out the code for adding the distribution lines and create another sales invoice, are there just two lines?

    Do you need to override what the default distributions would be?

    Joe

  • DiscountFilters Profile Picture
    45 on at

    Thank you so much for your response!

    Yes, I tried taking out the distribution lines from my code and there are only 2 distribution lines.  I'm guessing that means somewhere in GP it's set to create distribution lines.  Could you tell me where that is so I can tell the accountants and they can change the GL codes there?  If I did need to override the distributions in my code, how would I do that?

  • Joseph Markovich Profile Picture
    4,007 on at

    Typically, the default GL accounts for Financial, Sales, Purchasing are in Administration > Setup > Posting > Posting Accounts. Then just pick the series (Sales, Purchasing, etc).

    These are the default accounts that get filled in when transactions are created.

    The customer or the inventory item could have their own GL accounts too:

    Customers: Sales > Cards > Customer > click Accounts button.

    Items: Inventory > Cards > Item > click Accounts button.

    I am not a programmer (just know enough to be dangerous) but did a quick search about overriding the distributions with web services -- take a look at this:

    community.dynamics.com/.../gp-web-services-how-do-i-add-distribution-lines-to-my-payablesinvoice

    Have no idea if it will work, but trying to help. :)

    Joe

  • DiscountFilters Profile Picture
    45 on at

    This is wonderful.  Thank you so much for your help!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 699

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 582 Super User 2026 Season 1

#3
Subra Profile Picture

Subra 493

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans