Question

I have been given the painful task of writing a C# application to sync up employee time entries in a separate database with Quickbooks. Since I'm brand new to QB programming, I'm trying to peform basic tasks, such as getting a list of customers, then jobs for each customer, then employees. I've been reading the SDK documentation, but I'm still a little fuzzy on the details because the examples I'm finding are a little too advanced for me at the moment :-P

To keep things simple, I would like to ask for a code snippet that gives me the list of customers for starters. Here's the code I've got:

        QBSessionManager SessionManager = new QBSessionManager();
        IMsgSetRequest customerSet = SessionManager.CreateMsgSetRequest("US", 8, 0);

        //          
        // Code to get list of customers here.
        //

        SessionManager.OpenConnection2("", "New App", ENConnectionType.ctLocalQBD);
        SessionManager.BeginSession(string.Empty, ENOpenMode.omDontCare);
        IMsgSetResponse Resp = SessionManager.DoRequests(customerSet);
        MessageBox.Show(Resp.ToXMLString());
        SessionManager.EndSession();
        SessionManager.CloseConnection();

Can anyone fill in the "code to get list of customers here" for me? Thank you very much in advance!

Victor

Was it helpful?

Solution 2

Ok, seems like I found the missing piece:

ICustomerQuery customers = customerSet.AppendCustomerQueryRq();

This produces all the data related to each customer, which is a step forward. Parsing the XML for customers should be pretty straightforward, but parsing the individual tasks/jobs for each customer will be laborious, because there are no subnodes for each task - basically you get repeating chunks of XML with all the basic customer info (address, billing address, shipping address, etc.), then this one property called "FullName" which appends a colon to the customer name, followed by the task title (which itself can be followed by another colon with a subtask title, etc.). I'm wondering if there's something clever I can do with the request query to get a better xml response (for instance, specify what properties I want returned, and maybe enforce the creation of subnodes for each task for a given customer)...comments are appreciated.

OTHER TIPS

customers.IncludeRetElementList.Add("IsActive");
customers.IncludeRetElementList.Add("ListID");
customers.IncludeRetElementList.Add("EditSequence");
customers.IncludeRetElementList.Add("Name");
customers.IncludeRetElementList.Add("ParentRef");

Only the fields specified in the above list will be returned from QuickBooks - it is very important to use the correct strings in the correct case - no error messages will result if something is wrong. You cannot specify sub-fields (eg, City within an Address block; you must get the entire Address block). For custom fields, you also must specify the OwnerID (use 0 for custom fields that are not private to an application)

customers.IncludeRetElementList.Add("DataExtRet"); //will return non-private and/or private data extension fields depending on the OwnerIDList, below
customers.OwnerIDList.Add("0"); // required for non-private data extn fields
customers.OwnerIDList.Add("Your Appln GUID"); // Use this to get private data extns for the Appln identified by the GUID

Adding to Victors, Chili and Hassan's answer. Glad to have stumbled on this question as I myself was struggling with the QBFC examples. Here is a full set of code that might just help someone down the road. If in the meantime someone could just point me in the direction of some useful documentation...that would be great.

Getting the Employee data to an XML string

    public static string EmployeeListXML()
    {
        QBSessionManager SessionManager = new QBSessionManager();
        IMsgSetRequest msgSetReq = SessionManager.CreateMsgSetRequest("US", 8, 0);
        IEmployeeQuery employee = msgSetReq.AppendEmployeeQueryRq();
        employee.IncludeRetElementList.Add("IsActive");
        employee.IncludeRetElementList.Add("ListID");
        employee.IncludeRetElementList.Add("EditSequence");
        employee.IncludeRetElementList.Add("FirstName");
        employee.IncludeRetElementList.Add("LastName");
        employee.IncludeRetElementList.Add("SSN");
        //employee.IncludeRetElementList.Add("ParentRef");
        //employee.IncludeRetElementList.Add("DataExtRet"); //will return non-private and/or private data extension fields depending on the OwnerIDList, below
        employee.OwnerIDList.Add("0"); // required for non-private data extn fields
        //customers.OwnerIDList.Add("Your Appln GUID"); // Use this to get private data extns for the Appln identified by the GUID
        SessionManager.OpenConnection2("", Application.ProductName, ENConnectionType.ctLocalQBD);
        //SessionManager.BeginSession(string.Empty, ENOpenMode.omDontCare);
        SessionManager.BeginSession(frmMain.QBFileName, ENOpenMode.omDontCare); // I have the filename on frmMain
        IMsgSetResponse Resp = SessionManager.DoRequests(msgSetReq);
        SessionManager.EndSession();
        SessionManager.CloseConnection();
        //MessageBox.Show(Resp.ToXMLString());
        return Resp.ToXMLString();
    }

Putting the XML string into a List of Emplpoyee Objects

    public static List<Employee> EmployeeXMLtoList()
    {
        string sXML = EmployeeListXML();

        List<Employee> lstEmp = new List<Employee>();
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(sXML);
        XmlNodeList parentNode = xmlDoc.GetElementsByTagName("EmployeeRet");
        foreach (XmlNode childNode in parentNode)
        {
            Employee oEmp = new Employee();
            oEmp.ListID = childNode.SelectSingleNode("ListID").InnerText;
            oEmp.EditSequence = childNode.SelectSingleNode("EditSequence").InnerText;
            oEmp.Active = childNode.SelectSingleNode("IsActive").InnerText;
            oEmp.FirstName = childNode.SelectSingleNode("FirstName").InnerText;
            oEmp.LastName = childNode.SelectSingleNode("LastName").InnerText;
            oEmp.SSN = childNode.SelectSingleNode("SSN").InnerText;
            lstEmp.Add(oEmp);
        }
        return lstEmp;
    }

Function that return an Employee Object using Linq

    public static Employee GetEmployeeObject(string sSSN)
    {
        Employee oReturn = null;
        List<Employee> lstEmployee = EmployeeXMLtoList();

        IEnumerable<Employee> lstEmps = from oEmp in lstEmployee
                                        where oEmp.SSN == sSSN
                                        select oEmp;

        foreach (var oEmp in lstEmps)
        {
            oReturn = oEmp;
        }
        return oReturn;
    }

Example of Code

 Employee oEmployee = QB.GetEmployeeObject("112-35-8560");

Employee Class

public class Employee
{
    private string sEmployeeID;
    private string sSSN;
    private string sLastName;
    private string sFirstName;
    private string sAddress1;
    private string sAddress2;
    private string sCity;
    private string sState;
    private string sZipCode;
    private string sGender;
    private string sEthnicity;
    private DateTime dDOB;
    private string sMaritalStatus;
    private int iDependants;
    private string sUScitizen;
    private decimal iPayRate;
    private string sPhone;
    private DateTime dHireDate;
    private string sEmail;

    public Employee() { }

    public string EmployeeID
    {
        get { return sEmployeeID; }
        set { sEmployeeID = value; }
    }

    public string ListID
    {
        get; set;
    }

    public string EditSequence
    {
        get;  set;
    }

    public string Active
    {
        get; set;
    }


    public string SSN
    {
        get { return sSSN; }
        set { sSSN = value; }
    }

    public string LastName
    {
        get { return sLastName; }
        set { sLastName = value; }
    }

    public string FirstName
    {
        get { return sFirstName; }
        set { sFirstName = value; }
    }

    public string FullName
    {
        get { return FirstName + " " + LastName; }
        set { }
    }

    public string Address1
    {
        get { return sAddress1; }
        set { sAddress1 = value; }
    }

    public string Address2
    {
        get { return sAddress2; }
        set { sAddress2 = value; }
    }

    public string State
    {
        get { return sState; }
        set { sState = value; }
    }
    public string City
    {
        get { return sCity; }
        set { sCity = value; }
    }
    public string ZipCode
    {
        get { return sZipCode; }
        set { sZipCode = value; }
    }
    public string Gender
    {
        get { return sGender; }
        set { sGender = value; }
    }

    public string Ethnicity
    {
        get { return sEthnicity; }
        set { sEthnicity = value; }
    }

    public DateTime DOB
    {
        get { return dDOB; }
        set { dDOB = value; }
    }
    public string MaritalStatus
    {
        get { return sMaritalStatus; }
        set { sMaritalStatus = value; }
    }
    public int Dependants
    {
        get { return iDependants; }
        set { iDependants = value; }
    }
    public string UScitizen
    {
        get { return sUScitizen; }
        set { sUScitizen = value; }
    }

    public decimal PayRate
    {
        get { return iPayRate; }
        set { iPayRate = value; }
    }
    public DateTime HireDate
    {
        get { return dHireDate; }
        set { dHireDate = value; }
    }
    public string Phone
    {
        get { return sPhone; }
        set { sPhone = value; }
    }
    public string Email
    {
        get { return sEmail; }
        set { sEmail = value; }
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top