Search code examples
c#quickbooksqbfc

How to list all Sales Receipts using the QuickBooks API


Context

I'm importing sales data into QuickBooks using a C# desktop application and the QuickBooks QBFC API.

I need to build a list of the Memo values for all Sales Receipts in the company file. This is because the Memo contains a sales reference which must be checked for duplicates before a new Sales Receipt can be imported.

Issue

The code I'm using at present is:

IMsgSetRequest requestMsgSet = qbSessionManager.CreateMsgSetRequest("UK", 13, 0);

ISalesReceiptQuery salesReceiptQuery = requestMsgSet.AppendSalesReceiptQueryRq();

salesReceiptQuery.metaData.SetValue(ENmetaData.mdNoMetaData);
salesReceiptQuery.IncludeRetElementList.Add("Memo");

IMsgSetResponse responseMsgSet = qbSessionManager.DoRequests(requestMsgSet);

IResponseList responseList = responseMsgSet.ResponseList;

for (int i = 0; i < responseList.Count; i++)
{
    IResponse response = responseList.GetAt(i);

    if (response.StatusCode == 0)
    {
        ExistingOrderIds.Add(response.Detail.ToString());
    }
    else
    {
        Log.Error("While building OrderId list. Error returned when listing memos from QuickBooks Sales Receipts");
        Log.Error(response.StatusCode + " " + response.StatusMessage + " " + response.Detail);
    }
}

This returns just a single IResponseList item and response.detail.toString() yields "System.__ComObject". There are actually several hundred Sales Receipts in this company file.

Question

What am I doing wrong here?


Solution

  • Ok, finally cracked this. My misconception is that you iterate on IResponseList. Actually the single IResponseList contains all my Memos in a list of ISalesReceiptRet objects. Hence iteration needs to occur on the ISalesReceiptRetList. The QuickBooks OSR examples are not remotely clear on this! So the working code is as follows:

    IMsgSetRequest requestMsgSet = qbSessionManager.CreateMsgSetRequest("UK", 13, 0);
    
    ISalesReceiptQuery salesReceiptQuery = requestMsgSet.AppendSalesReceiptQueryRq();
    
    salesReceiptQuery.metaData.SetValue(ENmetaData.mdNoMetaData);
    salesReceiptQuery.IncludeRetElementList.Add("Memo");
    
    IMsgSetResponse responseMsgSet = qbSessionManager.DoRequests(requestMsgSet);
    
    IResponse response = responseMsgSet.ResponseList.GetAt(0);
    
    ISalesReceiptRetList salesReceiptRetList = (ISalesReceiptRetList)response.Detail;
    
    for (int i = 0; i < salesReceiptRetList.Count; i++)
    {
        if (salesReceiptRetList.GetAt(i).Memo != null)
        {
            string memo = salesReceiptRetList.GetAt(i).Memo.GetValue();
    
            if (memo != string.Empty)
            {
               ExistingOrderIds.Add(memo);
            } 
        }
    }