Developing requests and processing responses with the QBFC library

This page introduces the objects in the QBFC library, focusing on the objects and methods you will most often use when building request messages and processing response messages. It also shows you how to use the API reference pages to obtain the details you need to work with QBFC objects. Functionally speaking, there are two groups of objects in the library:

If you are comfortable using XML simply writing out qbXML requests may be the way for you to implement your application. However, for many programmers, QBFC provides a faster and easier way to write an application.

Objects, objects everywhere: where do I start?

If you try to understand the QBFC object model by studying the individual objects, you’re in for a long day’s work. The QBFC library is a thin wrapper, which means there are a great many objects in the library, most of which model QuickBooks objects. The best way to understand the QBFC objects is to consider the way in which they are used, beginning with the central QBFC object: QBSessionManager. Applications use the session manager to

This page concentrates on QBSessionManager’s role in creating and sending requests and receiving responses, because these are the operations in which your applications will be working with the large number QBFC objects that model QuickBooks objects and use them to retrieve or operate on QuickBooks data. Despite the large number of these data-modeling objects, they are similar in nature, and the procedures described here apply to all of them.

For information about the session manager’s role in opening connections and beginning sessions, see Connections, sessions and authorizations.

Which QBFC objects do I use to create and build a request message?

The following figure outlines the steps for creating and building a request message. They begin with the session manager.

  1. Call the QBSessionManager’s CreateMsgSetRequest method to create an IMsgSetRequest object. This object will contain one or more request messages.

  2. Call one of the IMsgSetRequest’s append methods to create an empty request object. The important point here for understanding the QBFC library is that IMsgSetRequest has a large number of append methods, one for each of the SDK’s request objects. And each of the request objects corresponds to a combination of a QuickBooks object and an operation, so there are request objects for AccountAdd, AccountModify, AccountQuery, and so on. The figure represents this by showing append methods for company query, customer add, and vendortype query. Each request object has it’s own data fields, depending on the QuickBooks object it models, but your application handles every request object in the same way.

    ../../../_images/objects_for_building_requests.gif
  3. The append methods return an empty request object. The application needs to finish building the object before sending the message set to the request processor. If, for example, the application is adding a new customer to QuickBooks, it calls AppendCustomerAddRq, which returns an empty ICustomerAdd object. The application can define the new customer by calling the ICustomerAdd’s SetValue methods to set its properties to values that describe the new customer.

Notice that the various append methods are not listed in the reference pages. If your programming environment supports Intellisense, you can see all of the Append methods in your code editor when you instantiate an IMsgSetRequest object. If you don’t have this, and don’t have an object browser that can look into the QBFC library, you can easily construct the append method name simply by applying the naming convention–at the request names listed in the reference pages: just put “Append” in front of each request name and “Rq” at the end of the request name.

Only invoke an Append method once for each desired request! Each time you invoke an Append method, you are adding a new and separate request object to the request set.

For each request in the API Reference pages, the reference provides code samples that show the construction in VB or VB.Net code.

Using the reference pages to build out a request

The reference pages have the information you need to build out an empty request object. The page for an object will show the data properties that are available for the object, which of them are required, and what are the details for each property (data type, max length, and enumerated values). The figure below shows the reference page for the ICustomerAdd object.

../../../_images/OSR_listing_customer_add.jpg

Notice the following about information on the page:

Each leaf node has a SetValue method, which the application uses to set the value of the property, subject to the datatype, max length, and list of enumerated values displayed on the reference page. The following snippet shows how this is done for a customer add object’s FirstName property:

Dim MyICustomerAdd as ICustomerAdd
Set MyICustomerAdd = MyRequestMsgSet.AppendCustomerAddRq
MyICustomerAdd.FirstName.SetValue("Fred")

The next illustration shows the enumerated values for the DeliveryMethod property, which appear when the data type name, IQBENDeliveryMethodType, is clicked. Note that the appearance of “EN” in the data type name indicates that it has enumerated values.

../../../_images/osr_display_of_enumerated_values.png
Other useful IMsgSetRequest methods

The IMsgSetRequest Append methods are already covered in this document and in the API Reference pages. However, the request message set object has other methods you need to know about.

IMsgSetRequest Method/Property Parameters Description

HRESULT Attributes ([out, retval]

IAttributesRqSet**pVal);

-pVal

Pointer to the returned IAttributeRqSet object

This property returns the IAttributeRqSet object, which you would need if you wanted to determine the current attribute settings in the request set.

IAttributeRqSet contains the attributes that are currently in effect for all requests in the message set.

QuickBooks supports several attributes, which are documented in the API Reference patges.

HRESULT

ClearRequests();

NA Removes all requests currently appended to the request message set.

HRESULT RequestList ([out, retval]

IRequestList* *pVal);

pVal

Pointer to the returned IRequestList object

You probably will seldom use this property. You would use this property if you wanted to get one or more requests from the request message set. The IRequestList object returned has a count and a GetAt method for returning individual IRequest objects from the list.

Once you have the IRequest object, you can use its RequestID, Type, or Detail methods as desired. The Detail is processed exactly like its IResponse counterpart, which is thoroughly covered in Chapter 7, “Handling Responses Using QBFC or qbXML.”

HRESULT

ToXMLString([out, retval] BSTR* qbXMLRequest);

-qbXMLRequest

Pointer to the returned string containing the request message set in qbXML format.

This method is very handy during diagnostics where you need to examine the complete XML representation of the requests that were built in QBFC. Useful for making sure you are getting the requests you expect.

HRESULT Verify([out] BSTR* errorMsg, [out, retval]

VARIANT_BOOL* isOK)

errorMsg

Contains an error message for every request that failed validation. If there is no failure, this string is empty.

isOk

Returns True in VB and Variant_True in C++ if all the requests are valid.

Returns False in VB and Variant_False in C++ otherwise.

The DoRequests method causes validation to be run automatically. However, if you need to validate the requests for proper construction before you invoke DoRequests, you can use this method.
Which QBFC objects do I use to process a response?

The following figure outlines the steps for processing a response message. They begin with the session manager.

  1. Create IMsgSetResponse object.

  2. Populate the IMsgSetResponse by calling the QBSessionManager’s DoRequests method, and capturing the return in the IMsgSetResponse.

  3. The important point here for understanding how to work with the QBFC library is that the actual data objects in the responses are nested within the structure of the IMsgSetResponse object. Remember that the IMsgSetRequest could have contained multiple requests, and that, therefore, the IMsgSetResponse will often contain multiple responses. Your application will want to process each response separately. The response or responses are contained in the IMsgSetResponse’s ResponseList property.

    ../../../_images/objects_for_processing_responses.gif
  4. To access the data in the response, you need to work your way through the layers of nesting. First, get a handle on the response list:

1
2
Dim responseList as IResponseList
responseList = responseMsgSet.ResponseList
  1. The individual responses are nested in the response list as IResponse objects. To access the individual responses in the response list, call the response list’s GetAt() method. If you are certain that the IMsgSetRequest contained only a single request message you can get the single response in a single call, like this:

1
2
3
Dim response as IResponse
response = responseList.GetAt(0)
'    ... process response

Otherwise, you’ll want to loop over the response list and process each IResponse separately:

1
2
3
4
5
for i=0 to responseList.Count-1
   Dim response as IResponse
   response = responseList.GetAt(i)
' ... process response
Next i
  1. A look at IResponse objects, as shown in the figure, reveals the following significant characteristics:
    • An IResponse object has some properties that report on its status–StatusCode, StatusSeverity, and StatusMessage
    • If the source request was valid, the IResponse object will have a non-empty Detail attribute, which contains the actual QuickBooks data.
    • The Detail attribute will have a data type that is appropriate for the data it holds and corresponds to the source request type. The response to an IAccountAddRq, for example, will be an IAccountRet object for example, and the response to an IAccountQueryRq will be IAccountRetList, indicating a list of Account objects.
    • Note the naming convention for the detail objects: IOBjectTypeRet or, in the case of multiple objects, IObjectTypeListRet, which is simply a list of IOBjectTypeRet.
    • As with the request objects, there are many *Ret objects, one for each QuickBooks object that can be accessed via the SDK.
  2. To access the QuickBooks data in the *Ret objects, call the GetValue methods for each property of the object, according to the following pattern: data = IOBjectTypeRet.PropertyName.GetValue(). For query responses, the application will need to loop through the objects in tje *RetList and process them in sequence.
  3. For code examples of processing responses see Receiving and processing responses.
Using the API Reference pages to process a response

The reference pages have the information you need to process a response object. For each QuickBooks object, the reference page shows both the request and response message used to work with that object via the SDK. For the response side, the *Ret object, the page will show the data properties that are returned with the object and what are the details for each property (its data type, max length, and enumerated values). The figure below shows the reference page for the IResponse object of type ICustomerRet.

../../../_images/osr_ret_object.jpg

Notice the following about information on the page:

Each leaf node has a GetValue() method, which the application uses to get the value returned for the property. It should conform to the datatype, max length, and list of enumerated values displayed on the reference page. The following snippet shows how this is done for an ICustomerRet customer response object’s FirstName property:

Dim ReturnedFirstName As String
ReturnedFirstName = MyICustomerAdd.FirstName.getValue()
Objects and methods used in response message processing

The following tables list the objects described in the previous paragraphs, along with the their methods and properties.

IMsgSetResponse

The following table shows the properties and methods for the message set response object:

IMsgSetResponse Method/Property Parameters Description
HRESULT ResponseList ([out, retval] IResponseList* *pVal)

-pVal

Pointer to the returned IResponseList object.

You need to invoke this on every IMsgSetResponse object to get the response list.
HRESULT ToXMLString ([out, retval] BSTR* qbXMLResponse);

-qbXMLResponse

Pointer to the returned string containing the response message set in qbXML format.

This method is very handy during diagnostics where you need to examine the complete XML representation of the responses that were returned from QuickBooks. Useful for making sure you are getting the results you expect.
IResponseList

The following table shows the properties and methods for the IResponseList object.

IResponseList Method/ Property Parameters Description

HRESULT Count

([out, retval] long *pVal)

-pVal

Pointer to the returned number of IResponse objects contained in the list.

Useful for setting up a loop, with Count used as the loop limit.

RESULT GetAt

(long index, [out,retval] IResponse** retVal);

-index

The supplied index specifying which IResponse object in the list is to be returned.

-retVal

Pointer pointer to the returned IResponse object.

The index is zero based, so the first IResponse on the list has the index of 0.
IResponse

The following table shows the properties and methods for the IResponse object.

IResponse Method/Property Parameters Description

HRESULT Detail

([out, retval] IQBBase** pVal);

-pVal

Pointer pointer to the response contents

This returned value must be upcast in most languages. For example, in VB.NET, for example, you would get the Response detail data into an ItemInventoryRet object like this:

itemInventoryRet = response.Detail as itemInventoryRet

HRESULT iteratorID

([out, retval] BSTR *pVal);

-pVal The iteratorID is returned only for queries that use iterators to manage the amount of data returned. You would need to get this ID from the first reponse, then use it in the succeeding iterations of the query.

HRESULT

iteratorRemainingCount ([out, retval] long *pVal);

-pVal This property indicates the number of objects remaining to be iterated through. This is helpful in optimizing the MaxReturned value, among other things.

HRESULT retCount

([out, retval] long *pVal);

-pVal This value is available if the response is a query response. It indicates the count of Ret objects in the list.
HRESULT StatusCode ([out, retval] long *pVal); -pVal Indicates success or the nature of any failure. The value 0 indicates success.
HRESULT StatusMessage ([out, retval] BSTR *pVal); -pVal A text message that provides more information than just the status code regarding the nature of the failure
HRESULT StatusSeverity ([out, retval] BSTR *pVal); -pVal The severity level of the error.

HRESULT Type

([out, retval] IResponseType** pVal);

-pVal The type of RetList or Ret object contained in the IResponse. You need this to specify the object to receive the IResponse.Detail data.