Search code examples
c#quickbooks

QuickBooks Retrieve of Customers Fails


I have this code:

protected readonly IMsgSetRequest RequestMsgSet = null;
protected IMsgSetResponse ResponseMsgSet = null;

public string GetAllCustomer(bool IsActiveOnly = true)
{

    RequestMsgSet.ClearRequests();
    ICustomerQuery CustomerQueryRq = RequestMsgSet.AppendCustomerQueryRq();

    if (IsActiveOnly)
    {
        if (CustomerQueryRq != null)
            CustomerQueryRq.ORCustomerListQuery.CustomerListFilter.ActiveStatus.SetValue(
                ENActiveStatus.asActiveOnly);
    }
    else {
        CustomerQueryRq.ORCustomerListQuery.CustomerListFilter.ActiveStatus.SetValue(ENActiveStatus.asAll);
    }
    ResponseMsgSet = SessionManager.DoRequests(RequestMsgSet);
    return(ResponseMsgSet.ToXMLString());
}

Console.WriteLine(GetAllCustomer());

It returns this:

<?xml version="1.0" ?>
<QBXML>
<QBXMLMsgsRs>
<CustomerQueryRs requestID="0" statusCode="1000" statusSeverity="Error" statusMessage="There has been an internal error when processing the request." />
</QBXMLMsgsRs>
</QBXML>

I suspect that the DoRequests runs out of memory as I have 110,000 customers.

How can I confirm that DoRequests is running out of memory?

How can I rewrite this code to use less memory?


Solution

  • Have a look at ICustomerQuery

    You can get data with filtering's such as: name range (a-i, j-r than s-z) filter, date range filter, name starts with filter and other some filters as well.

    Also this can be used to get only the things you want like: FullName and AccountNumber.

    In this way you may consume less memory usage. Have a look at below sample code:

    public IList<CustomerModelQB> GetAllCustomer(string fromName = "a", string toName = "z", bool IsActiveOnly = true)
        {
            RequestMsgSet.ClearRequests();
            ICustomerQuery CustomerQueryRq = RequestMsgSet.AppendCustomerQueryRq();
    
            if (IsActiveOnly)
            {
                if (CustomerQueryRq != null)
                    CustomerQueryRq.ORCustomerListQuery.CustomerListFilter.ActiveStatus.SetValue(
                        ENActiveStatus.asActiveOnly);
            }
            else
                CustomerQueryRq.ORCustomerListQuery.CustomerListFilter.ActiveStatus.SetValue(ENActiveStatus.asAll);
    
            //CustomerQueryRq.ORCustomerListQuery.CustomerListFilter.MaxReturned.SetValue(3);
    
            //Set field value for FromName
            CustomerQueryRq.ORCustomerListQuery.CustomerListFilter.ORNameFilter.NameRangeFilter.FromName.SetValue(fromName);
            //Set field value for ToName
            CustomerQueryRq.ORCustomerListQuery.CustomerListFilter.ORNameFilter.NameRangeFilter.ToName.SetValue(toName);
    
            CustomerQueryRq.IncludeRetElementList.Add("FullName");
            CustomerQueryRq.IncludeRetElementList.Add("AccountNumber");
            ResponseMsgSet = SessionManager.DoRequests(RequestMsgSet);
            return WalkCustomerQuery(ResponseMsgSet);
        }
    

    Further you may refer to this question for alphabetical breakout of things.