Search code examples
c#.netquickbooksintuit-partner-platform

Intuit Partner Platform - Can't add an invoice because Item ID is unknown


I am trying to insert an invoice via IPP into a blank Quickbooks store. The insert will always fail because the Item's ID for the Invoice line item does not exist. I have been trying multiple paths to code around this with no success. I thought I could query for the Items and if it cannot find the one I am looking for It will create it. I cannot seem to get this to work. Can I just build an Invoice with a new Item I have created?

I currently build the invoice like this: (the problem is the line that starts with "object[] invoiceItemValues") Intuit.Ipp.Data.Qbd.InvoiceHeader invoiceHeader = new Intuit.Ipp.Data.Qbd.InvoiceHeader(); invoiceHeader.ARAccountName = "Accounts Receivable"; invoiceHeader.BillAddr = physicalAddress; invoiceHeader.CustomerName = customer.Name; invoiceHeader.DocNumber = (invoiceMaxId).ToString().PadLeft(4, '0'); invoiceHeader.TaxAmt = salesTax; invoiceHeader.TotalAmt = amount;

        List<Intuit.Ipp.Data.Qbd.InvoiceLine> listLine = new List<Intuit.Ipp.Data.Qbd.InvoiceLine>();


        Intuit.Ipp.Data.Qbd.ItemsChoiceType2[] invoiceItemAttributes = { Intuit.Ipp.Data.Qbd.ItemsChoiceType2.ItemId, Intuit.Ipp.Data.Qbd.ItemsChoiceType2.UnitPrice, Intuit.Ipp.Data.Qbd.ItemsChoiceType2.Qty };
        object[] invoiceItemValues = { new Intuit.Ipp.Data.Qbd.IdType() { idDomain = Intuit.Ipp.Data.Qbd.idDomainEnum.QB, Value = "2" }, amount, new decimal(1) };



        var invoiceLine = new Intuit.Ipp.Data.Qbd.InvoiceLine();
        invoiceLine.Amount = amount;
        invoiceLine.AmountSpecified = true;
        invoiceLine.Desc = "test " + date.ToShortDateString(); // TODO: This will need to hold the additional fields
        invoiceLine.ItemsElementName = invoiceItemAttributes;
        invoiceLine.Items = invoiceItemValues;
        invoiceLine.ServiceDate = date;
        invoiceLine.ServiceDateSpecified = true;
        listLine.Add(invoiceLine);

        Intuit.Ipp.Data.Qbd.Invoice invoice = new Intuit.Ipp.Data.Qbd.Invoice();
        invoice.Header = invoiceHeader;
        invoice.Line = listLine.ToArray();

This is the error I get: "Validation for txn_line_rec.item_id INVALID key = 30 domain=QB enum = Item"

Here is what tries to query Items, but also requires IDs.

ItemQuery qbdItemQuery = new ItemQuery();
qbdItemQuery.Items = new object[] { new IdSet() { Id = new IdType[] { new IdType() { idDomain = idDomainEnum.NG, Value = "79841" } } } };
qbdItemQuery.ItemsElementName = new ItemsChoiceType4[] { ItemsChoiceType4.ListIdSet };
List<Item> ItemQueryResult = qbdItemQuery.ExecuteQuery<Item>(context).ToList<Item>();

Solution

  • Please see the sample in QBO, similarly you can code for QBD- Intuit.Ipp.Data.Qbo.Invoice invoice = new Intuit.Ipp.Data.Qbo.Invoice();

    Intuit.Ipp.Data.Qbo.InvoiceHeader invoiceHeader = new     Intuit.Ipp.Data.Qbo.InvoiceHeader();
    invoiceHeader.DocNumber = "AUTO_GENERATE";
    invoiceHeader.TxnDate = new DateTime(2013, 09, 04);
    invoiceHeader.TxnDateSpecified = true;
    invoiceHeader.CustomerId = new Intuit.Ipp.Data.Qbo.IdType() { idDomain =     Intuit.Ipp.Data.Qbo.idDomainEnum.QBO, Value = "12" };
    invoiceHeader.SubTotalAmt = 100;
    invoiceHeader.SubTotalAmtSpecified = true;
    invoiceHeader.TotalAmt = 100;
    invoiceHeader.TotalAmtSpecified = true;
    
    invoice.Header = invoiceHeader;
    
    Intuit.Ipp.Data.Qbo.InvoiceLine invoiceLine = new Intuit.Ipp.Data.Qbo.InvoiceLine();
    invoiceLine.Desc = "Description";
    invoiceLine.Amount = 100;
    invoiceLine.AmountSpecified = true;
    invoiceLine.Taxable = false;
    invoiceLine.TaxableSpecified = true;
    invoiceLine.ItemsElementName = new Intuit.Ipp.Data.Qbo.ItemsChoiceType2[] {     Intuit.Ipp.Data.Qbo.ItemsChoiceType2.ItemId,     Intuit.Ipp.Data.Qbo.ItemsChoiceType2.UnitPrice, Intuit.Ipp.Data.Qbo.ItemsChoiceType2.Qty };
    invoiceLine.Items = new object[] { new Intuit.Ipp.Data.Qbo.IdType() { idDomain = Intuit.Ipp.Data.Qbo.idDomainEnum.QBO, Value = "30" }, 100m, 1m };
    
    invoice.Line = new Intuit.Ipp.Data.Qbo.InvoiceLine[] { invoiceLine };
    
    Intuit.Ipp.Data.Qbo.Invoice addedInvoice = dataService.Add<Intuit.Ipp.Data.Qbo.Invoice>(invoice);
    

    Edit: To create item-

    Intuit.Ipp.Data.Qbo.Item qboItem = new Intuit.Ipp.Data.Qbo.Item();                          
                            qboItem.Name = "nemoitem11";
                            qboItem.Desc = "Itemdesc";
                            qboItem.UnitPrice = new Money() ;
                            qboItem.UnitPrice.Amount = 20;
                            qboItem.IncomeAccountRef = new AccountRef();
                            qboItem.IncomeAccountRef.AccountId = new IdType() { idDomain = idDomainEnum.QBO, Value = "1" };
                            Item resultItem = commonService.Add(qboItem) as Item;