0

I'm experimenting with the XeroNet SDK and am getting this error: "You cannot have multiple line items with the same LineItemID."

This is the code I'm using:

var lineItems = new List<LineItem>();

var rnd = new Random();
var count = rnd.Next(1, 20);
var date = DateTime.UtcNow.AddDays(-rnd.Next(1, 600)).Date;

Console.WriteLine($"Adding {count} line items");
for (var i = 0; i < count; i++)
{
  lineItems.Add(new LineItem
  {
    Quantity = rnd.Next(1, 10),
    AccountCode = "200",
    Description = $"BLAH{i}",
    UnitAmount = (decimal)(rnd.NextDouble() * 100) + 1
  });
}
var invoice = new Invoice
{
  Contact = new Contact { Name = "Foo" },
  Type = Heads(rnd) ? InvoiceType.AccountsPayable : InvoiceType.AccountsReceivable,
  Date = date,
  DueDate = date.AddDays(90),
  LineAmountTypes = Heads(rnd) ? LineAmountType.Inclusive : LineAmountType.Exclusive,
  LineItems = lineItems
};

var response = private_app_api.Create(invoice);

I'm sure it's something obvious.

jeznag
  • 4,183
  • 7
  • 35
  • 53

1 Answers1

0

I believe when you construct your LineItems, the LineItem model is being instantiated with default GUID value public Guid LineItemId { get; set; } which is why it is duplicated

You should be able to fix this with when you instantiate the LineItem

lineItems.Add(new LineItem
            {
                LineItemId = Guid.NewGuid(),
                Quantity = rnd.Next(1, 10),
                AccountCode = "200",             
                Description = $"BLAH{i}",
                UnitAmount = (decimal)(rnd.NextDouble() * 100) + 1
            });