Search code examples
c#sdkquickbooksobject-referenceqbfc

How to query customer list detailed with sales rep and price level in Quickbook SDK


My goal is very basic: try to get all customers details, including sales rep and price level.

I create a new customer class like this.

public class Customer
{
    public string Name { get; set; }
    public string FullName { get; set; }
    public bool IsActive { get; set; }
    public string SalesRep { get; set; }
    public string PriceLevel { get; set; }
}

And this is the main code

https://gist.github.com/anonymous/3968904d2d0fc492ed176c40465313b6#file-gistfile1-txt

private void button1_Click(object sender, EventArgs e)
    {

        QBSessionManager sessionManager = null;
        try
        {
            sessionManager = new QBSessionManager();
            IMsgSetRequest requestMsgSet = sessionManager.CreateMsgSetRequest("US", 13, 0);
            requestMsgSet.Attributes.OnError = ENRqOnError.roeContinue;

            sessionManager.OpenConnection("", "Quickbooks SDK Demo Test");
            sessionManager.BeginSession("", ENOpenMode.omDontCare);

            ICustomerQuery customerQueryRq = requestMsgSet.AppendCustomerQueryRq();

            customerQueryRq.ORCustomerListQuery.CustomerListFilter.ActiveStatus.SetValue(ENActiveStatus.asAll);

            IMsgSetResponse responseMsgSet = sessionManager.DoRequests(requestMsgSet);

            sessionManager.EndSession();
            sessionManager.CloseConnection();

            IResponse response = responseMsgSet.ResponseList.GetAt(0);
            ICustomerRetList customerRetList = (ICustomerRetList)response.Detail;


            List<Customer> customers = new List<Customer>();


            if (customerRetList != null)
            {
                for (int i = 0; i < customerRetList.Count; i++)
                {
                    ICustomerRet customerRet = customerRetList.GetAt(i);

                    Customer customer = new Customer();
                    {
                        customer.Name = customerRet.Name.GetValue();
                        customer.FullName = customerRet.FullName.GetValue();
                        customer.IsActive = customerRet.IsActive.GetValue();
                        customer.PriceLevel = customerRet.PriceLevelRef.FullName.GetValue();
                        customer.SalesRep = customerRet.SalesRepRef.FullName.GetValue();
                    }
                    customers.Add(customer);
                }
            }

            dataGridView1.DataSource = customers;

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        finally
        {
            sessionManager.EndSession();
            sessionManager.CloseConnection();
        }
    }

When I run the program, I got an error: "Object Reference not set to an instance of an object" I know something wrong with the SalesRep and PriceLevel as these are object references but I have no idea how to fix it.

Please help.

Thanks


Solution

  • These fields:

    customer.PriceLevel = customerRet.PriceLevelRef.FullName.GetValue();
    customer.SalesRep = customerRet.SalesRepRef.FullName.GetValue();
    

    Won't always exist. You can't assume that SalesRepRef or PriceLevelRef exists for every customer. Some customers won't have a sales rep, or won't have a custom price level.

    So, test to make sure these aren't NULL or otherwise unset in some way prior to trying to get their value.