Quickbooks Online Accounting - How to add multiple line items in an invoice?

放肆的年华 提交于 2019-12-06 04:48:50

Currently every time the loop executes this line

invoice.Line = new Line[] { invoiceLine };

it is overwriting the previous value by assigning a new array. There will always be one item in the array once there are order items based on your current code.

Either store the lines in a list and then convert to an array after processing all the order items.

var orderItems = order.OrderItems;
var lines = new List<Line>();
foreach (var orderItem in orderItems) {
    //Line
    Line invoiceLine = new Line();

    //...code removed for brevity

    lines.Add(invoiceLine);
}    
//Assign Line Items to Invoice
invoice.Line = lines.ToArray();

Or the other option would be to create the array before and populate it as the line items are created.

var orderItems = order.OrderItems;
var lineCount = orderItems.Count();
//Creating Line array before
invoice.Line = new Line[lineCount];   
for (int index = 0; index < lineCount; index++) {
    //Order item
    var orderItem = orderItems[index];
    //Line
    Line invoiceLine = new Line();

    //...code removed for brevity

    //Assign Line Item to Invoice
    invoice.Line[index] = invoiceLine;
}   

UPDATE:

Get the item reference and set the necessary values

var itemRef = itemRepository.Get(i => i.ItemID == orderItem.ItemID).FirstOrDefault();
if (itemRef != null) {
    //Line Sales Item Line Detail - ItemRef
    lineSalesItemLineDetail.ItemRef = new ReferenceType() {
        Name = itemRef.FullDescription,
        Value = itemRef.ItemID 
    };
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!