Here you go Sandip. I appreciate the effort you're putting into this. I'm really stuck with this issue and it's a dependency for our new project.
private void CreateSalesInvoice()
{
CompanyKey _companyKey;
Context _context;
// Create an instance of the web service want to do this
//asynchronously.
using (var wsDynamicsGP = new DynamicsGPClient())
{
_context = new Context();
// Specify which company to use (sample company)
_companyKey = new CompanyKey { Id = 2 }; //Hautelook
// Set up the context object
_context.OrganizationKey = _companyKey;
_context.CultureName = "en-US";
// Create a sales invoice object
SalesInvoice salesInvoice = new SalesInvoice();
// Create a sales document type key for the sales invoice
SalesDocumentTypeKey salesInvoiceType = new SalesDocumentTypeKey { Type = SalesDocumentType.Invoice };
// Populate the document type key for the sales invoice
salesInvoice.DocumentTypeKey = salesInvoiceType;
// Create a customer key
CustomerKey customerKey = new CustomerKey { Id = "MEMBER" };
// Set the customer key property of the sales invoice
salesInvoice.CustomerKey = customerKey;
// Create a batch key
BatchKey batchKey = new BatchKey { Id = "TEST_020915" };
// Set the batch key property of the sales invoice object
salesInvoice.BatchKey = batchKey;
salesInvoice.CustomerPONumber = "PO_02092015";
salesInvoice.CreatedDate = DateTime.Now;
//salesInvoice.WarehouseKey = new WarehouseKey { Id = "TESTWH1" };
// Create a sales invoice line to specify the invoiced item
SalesInvoiceLine salesInvoiceLine = new SalesInvoiceLine();
// Create an item key
ItemKey invoiceItem = new ItemKey { Id = "3655949" };
// Set the item key property of the sales invoice line object
salesInvoiceLine.ItemKey = invoiceItem;
//Set Warehouse
salesInvoiceLine.WarehouseKey = new WarehouseKey { Id = "VERNON" };
// Create a sales invoice quatity object
Quantity invoiceCount = new Quantity { Value = 2, DecimalDigits = 0 };
// Set the quantity of the sales invoice line object
salesInvoiceLine.Quantity = invoiceCount;
//Set the Sequence Number ////16384
var salesLineKey = new SalesLineKey
{
LineSequenceNumber = 16384
,
SalesDocumentKey = new SalesDocumentKey { Id = "IV1" }
,
CompanyKey = _companyKey
};
var salesLineSerialKey = new SalesLineSerialKey { SalesLineKey = salesLineKey, SequenceNumber = 16384, CompanyKey = _companyKey, QuantityType = QuantityType.OnHand };
var salesLineSerials = new[]
{new SalesLineSerial{
SerialNumber = "16384"
,DateReceived=DateTime.Now
,Key = salesLineSerialKey
,DateSequenceNumber=1
}
};
salesInvoiceLine.Serials = salesLineSerials;
// 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;
try
{
// Get the create policy for the sales invoice object
Policy salesInvoiceCreatePolicy = wsDynamicsGP.GetPolicyByOperation("CreateSalesInvoice", _context);
// Create the sales invoice
wsDynamicsGP.CreateSalesInvoice(salesInvoice, _context, salesInvoiceCreatePolicy);
}
catch (Exception ex)
{
}
}
}