Search code examples
intuit-partner-platformquickbooks-online

With c# devkit, how do i access the line ItemID on an invoice


With c# devkit, how do i access the line ItemID on an invoice?

My response xml has a section that looks like this:

<Line>
    <Desc>TestItem</Desc>
    <Amount>66.00</Amount>
    <Taxable>false</Taxable>
    <ItemId idDomain="QBO">3</ItemId>
    <UnitPrice>33</UnitPrice>
    <Qty>2</Qty>
    <ServiceDate>2013-01-07-08:00</ServiceDate>
</Line>

And can pull data from this with the devkit like this:

List<Intuit.Ipp.Data.Qbo.Invoice> customerInvoicesWithinDateRange = invoice.ExecuteQuery<Intuit.Ipp.Data.Qbo.Invoice>(context).ToList<Intuit.Ipp.Data.Qbo.Invoice>();
foreach (var invoiceFound in customerInvoicesWithinDateRange)
{
    foreach (var line in invoiceFound.Line)
    {
       things += line.Desc + " <br> ";
       things += line.Taxable.ToString() + " <br> ";
       things += line.ServiceDate.ToShortDateString() + " <br> ";
       things += line.Amount.ToString() + "<br>";
    }
}

But there is no line.ItemId,line.Qty nor line.UnitPrice in the object.

I know that when i set the line.Items object it is as an ItemsChoiceType2 object, but when i try and access the line.Items object i get "NullReferenceException: Object reference not set to an instance of an object."

What steps am i missing to get that elusive "3"?

Thanks

Edit: @Peter when i do:

List<Intuit.Ipp.Data.Qbo.Invoice> customerInvoicesWithinDateRange = invoice.ExecuteQuery<Intuit.Ipp.Data.Qbo.Invoice>(context).ToList<Intuit.Ipp.Data.Qbo.Invoice>();
var quantity = customerInvoicesWithinDateRange[0].Line[0].Items[Array.IndexOf(customerInvoicesWithinDateRange[0].Line[0].ItemsElementName, Intuit.Ipp.Data.Qbo.ItemsChoiceType2.Qty)];

I get "System.ArgumentNullException: Value cannot be null." on the 2nd line. I think customerInvoicesWithinDateRange[0].Line[0].ItemsElementName is null. It was also null in my foreach loop.

Edit Edit:

nm, i actually had an invoice in the query that dind't have any lines i guess. wrapping it in the following solved the final hurdle:

if (customerInvoicesWithinDateRange[0].Line[0].Items != null)

Thanks again for your help.


Solution

  • The objects in Items are defined in the ItemsElementName array of ItemsChoiceType2.

    So, without any error checking, quantity can be obtained from the following line:

    var quantity = invoices[0].Line[0].Items[Array.IndexOf(invoices[0].Line[0].ItemsElementName, Intuit.Ipp.Data.Qbo.ItemsChoiceType2.Qty)];