Jake Farley
Jake Farley

Reputation: 72

How can I get all invoices for one month and save them all as PDFs using QBFC13 and C#?

I am making an application that gets all invoices for one month, and then saves each of those invoices as a PDF, I have never developed with QuickBooks so I have very little knowledge about it. Would someone be able to give me any source code that does this or something similar for me to build off of? I have found a couple links that I think are pointing me in the right direction but I'm not 100% sure:

How can I get the invoices that has been paid on an specific date from QuickBooks to .NET app?

https://intuitdeveloper.lc.intuit.com/questions/1382278-sample-net-code-for-quickbooks-api-oauth-call-to-create-an-invoice-and-pdf-or-any-api-call-if-no-invoice

https://intuitdeveloper.lc.intuit.com/questions/1060982-quickbooks-online-api-invoice-pdf-download

I am using C# .NET and QBFC13Lib.

Edit:

I have taken this code for getting all invoices from the linked question.

bool sessionBegun = false;
        bool connectionOpen = false;
        QBSessionManager sessionManager = null;

        try
        {
            //Create the session Manager object
            sessionManager = new QBSessionManager();

            //Create the message set request object to hold our request
            IMsgSetRequest requestMsgSet = sessionManager.CreateMsgSetRequest("US", 13, 0);
            requestMsgSet.Attributes.OnError = ENRqOnError.roeContinue;

            //Connect to QuickBooks and begin a session
            sessionManager.OpenConnection("", "GenerateInvoicePDFs");
            connectionOpen = true;
            sessionManager.BeginSession("", ENOpenMode.omDontCare);
            sessionBegun = true;

            IInvoiceQuery invoiceQueryRq = requestMsgSet.AppendInvoiceQueryRq();

            invoiceQueryRq.IncludeLineItems.SetValue(true);

            //Send the request and get the response from QuickBooks
            IMsgSetResponse responseMsgSet = sessionManager.DoRequests(requestMsgSet);
            IResponse response = responseMsgSet.ResponseList.GetAt(0);
            IInvoiceRetList invoiceRetList = (IInvoiceRetList)response.Detail;

            var invoices = new List<Invoice>();

            if (invoiceRetList != null)
            {
                for (int i = 0; i < invoiceRetList.Count; i++)
                {
                    IInvoiceRet invoiceRet = invoiceRetList.GetAt(i);

                    var invoice = new Invoice
                    {
                        QuickBooksID = invoiceRet.TxnID.GetValue(),
                        EditSequence = invoiceRet.EditSequence.GetValue()
                    };
                }
            }
        }
        catch
        {

        }

I am given an error that says that Invoice is not a type.

Upvotes: 2

Views: 1328

Answers (1)

Nacharya
Nacharya

Reputation: 229

The SDK provides filters on the query object to enable you to query a subset of data. You can filter the invoices based on transaction date:

// get all invoices for the month of august 2016
invoiceQueryRq.ORInvoiceQuery.InvoiceFilter.ORDateRangeFilter.TxnDateRangeFilter.ORTxnDateRangeFilter.TxnDateFilter.FromTxnDate.SetValue(new DateTime(2016, 8, 1));
invoiceQueryRq.ORInvoiceQuery.InvoiceFilter.ORDateRangeFilter.TxnDateRangeFilter.ORTxnDateRangeFilter.TxnDateFilter.ToTxnDate.SetValue(new DateTime(2016, 8, 31));

Or, you can query invoices that have been modified within a given date range:

// all invoices modified in the month of August 2016
invoiceQueryRq.ORInvoiceQuery.InvoiceFilter.ORDateRangeFilter.ModifiedDateRangeFilter.FromModifiedDate.SetValue(new DateTime(2016, 8, 1));
invoiceQueryRq.ORInvoiceQuery.InvoiceFilter.ORDateRangeFilter.ModifiedDateRangeFilter.ToModifiedDate.SetValue(new DateTime(2016, 8, 31));

In the code that you are using, when you call BeginSession, the first argument is Blank. Although this works by taking the currently open company file, it is advised to explicitly provide the file path of company file you want to query for.

Be sure to cleanup after you are done querying:

if (requestMsgSet != null)
{
    Marshal.FinalReleaseComObject(requestMsgSet);
}
if (invoiceQueryRq != null)
{
    Marshal.FinalReleaseComObject(invoiceQueryRq);
}
sessionMgr.EndSession();
sessionMgr.CloseConnection();

In any case, I would advise you to go through the SDK developers guide to understand what your code is actually doing.

Update:

The second part of your question is answered here.

Upvotes: 2

Related Questions