Search code examples
c#quickbooksnsoftware

Can't update SalesReceipt in Quickbooks desktop


I'm using QuickBooks Integrator from /nSoftware to integrate with QuickBooks Desktop

I'm trying to update an invoice and I don't get any errors but when I check in QuickBooks I see that nothing changed and it didn't actually get updated.

First I try to lookup the invoice based on the RefNumber and if it found an Invoice then I try to replace the Line Items and then i call the update method like this existingInvoice.Update();

Here's my code sample:

    public static List<Invoice> FindInvoice(string refNumber)
    {
        var invoicesSearch = new Objsearch
        {
            QueryType = ObjsearchQueryTypes.qtInvoiceSearch,
            RuntimeLicense = "MYLICENSEKEY",
            QBConnectionString = "MYCONNECTIONSTRINGTOREMOTECONNECTOR",
            SearchCriteria = new SearchCriteria
            {
                RefNumberContains = refNumber
            },
        };
        invoicesSearch.Search();
        var qbInvoiceList = invoicesSearch.Results.ToList();

        var invoiceObjList = new List<Invoice>();
        foreach (var inv in qbInvoiceList)
        {
            var newInv = new Invoice();
            newInv.QBResponseAggregate = inv.Aggregate;
            invoiceObjList.Add(newInv);
        }
        return invoiceObjList.FirstOrDefault();
    }



    public static void PutInvoice(Invoice invoice)
    {
        var existingInvoice = FindInvoice(invoice.RefNumber);
        if (existingInvoice != null)
        {
            existingInvoice.LineItems.Clear();
            existingInvoice.LineItems.AddRange(invoice.LineItems);

            existingInvoice.QBConnectionString = "MYCONNECTIONSTRINGTOREMOTECONNECTOR";
            existingInvoice.RuntimeLicense = RuntimeLicense;
            existingInvoice.QBXMLVersion = "12.0";

            existingInvoice.Update(); //this line 
        }
    }

Solution

  • Okay, so the issue was that I was setting the QBXMLVersion the last thing before updating.

    In order for the Update() to process successfully the QBXMLVersion needs to be set the first thing.

    Here's an updated working example:

    public static void PutInvoice(Invoice invoice)
    {
        var existingInvoice = FindInvoice(invoice.RefNumber);
        if (existingInvoice != null)
        {
            existingInvoice.QBXMLVersion = "12.0";
            existingInvoice.RuntimeLicense = "MyRuntimeLicenseKey";
            existingInvoice.QBConnectionString = "MYCONNECTIONSTRINGTOREMOTECONNECTOR";
    
            existingInvoice.LineItems.Clear();
            existingInvoice.LineItems.AddRange(invoice.LineItems);
    
            existingInvoice.Update(); 
        }
    }