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:
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;
}