Integrate QuickBooks Desktop with Ruby on Rails

Making Requests to QuickBooks

Now that you have the Web Connector set up, you can start making requests to QuickBooks.

Making a QBXML Request

Let's start by asking QuickBooks for a list of customers.

To queue requests for QuickBooks, we'll give QUBE Sync which Connection to use and specify the query.

We'll want to store these requests in our Rails app to reference them later, so first create a QuickbooksRequest model.

rails g model QuickbooksRequest qube_connection:references request_json:jsonb response_json:jsonb qube_id:string:uniq
# Get the connection
connection = current_user.qube_connections.find(params[:id])

# We're going to send equivalent of the following QBXML request
# <?qbxml version="16.0"?>
# <QBXML>
# <QBXMLMsgsRq onError="stopOnError">
#     <CustomerQueryRq requestID="1" iterator="Start">
#         <MaxReturned>25</MaxReturned>
#         <IncludeRetElement>ListID</IncludeRetElement>
#         <IncludeRetElement>FullName</IncludeRetElement>
#     </CustomerQueryRq>
# </QBXMLMsgsRq>
# </QBXML>
request_json = QubeSync::RequestBuilder.new(version: "16.0") do |b|
  b.QBXML {
    b.QBXMLMsgsRq('onError' => 'stopOnError') {
      b.CustomerQueryRq('requestID' => request.id, 'iterator' => 'Start') {
        b.MaxReturned 1
        b.IncludeRetElement 'ListID'
        b.IncludeRetElement 'FullName'
      }
    }
  }
end

# Queue the request
queued_request = QubeSync.queue_request(connection.qube_id, {
  request_json: request_json
})

# Store the request in your database
QuickbooksRequest.create!(
  qube_connection: connection, 
  request_json: request_json, 
  qube_request_id: queued_request["id"])

Notice we're using the CustomerQueryRq to ask QuickBooks for a list of customers. Because QuickBooks can't handle large queries, we've included the iterator="Start" attribute to paginate our results in batches of 25 (specified in the MaxReturned attribute). QUBE Sync will automatically expand this to use QuickBooks' QBXML iteration protocol behind the scenes. You can find more information about the available requests in the QuickBooks Documentation.

Handling the Response

If you head over to the Queued Requests section of QUBE Sync, you'll see the request you just made. If your Web Connector hasn't already run, you can click "Update Selected" to trigger the sync. You'll notice that QUBE Sync automatically detected the request to iterate and converted the request we sent into a series of paginated requests to QuickBooks. Clicking on the request will show you the response from QuickBooks.

Next Steps

Now that you know how to make requests to QuickBooks, you can start handling those responses in your application via Webhooks. Check out the Webhooks guide to learn more.