AnsweredAssumed Answered

[Guide]How to use Ruby on Rails to make RESTish API ExecuteProcess calls

Question asked by brandon_fitch on Jun 22, 2017

I was struggling working with XML and the httparty gem.  I had tried moving back to Net::HTTP, different formatting tools for XML, but I finally landed on the following code.  To try to help contribute to the community, here is a hopefully helpful starting point to base your RESTish API calls directly from a Rails app to trigger on demand process executions:

 

First you'll need httparty and a controller.  I have since moved some of this code into the model, but using it all in the controller is a good place to start to see how everything pieces together.

 

Next you'll need to create your form_for in your view.erb, I won't go over that as it's pretty basic, depending on what you want to do.  

 

In this example, we are allowing the user to type in an account ID they got from software C (a legacy as400 system) that is slowly being phased out over a long period of time, have Boomi (software B) handle the logic to grab the specific account from C and transfer that account data to our Salesforce (software A).  

 

The reasoning behind this, is we have a scheduled process that updates account data at the end of the day for non-critical account transfers/updates, but no good way to trigger a new account with the touch of a button or for accounts that needed to be added or updated immediately.

 

Now into the controller:

(lines 5-6) First build up your placeholder Object, add a hidden ID field to an exposed attribute (here it's account_to_push.account_id). 

 

Then build up your xml request.  

 

(lines 8-13) Header data was a little funky, and I had to use a combination of old hash syntax and new hash syntax to make it work.

 

(lines 15-17) Credentials for basic_auth by setting environment variables in your rails app.

 

And finally the hardest part, formatting the body.

 

(lines 19-27) I had to end up using a less used %{} to build up my xml.  Using httparty to build the xml with a symbol caused the innerXML to not be accessible.  ¯\_(ツ)_/¯  On Line 24, I am using string interpolation in this example, but you will want to sanitize those inputs after testing.

 

(lines 29-33) Finally piece it together using an HTTParty.post again using env variables for your orgs account ID, and build your request.

 

require 'httparty'

class AccountsController < ApplicationController
  def account_force
    account_to_push = Account.new()
    account_to_push.account_id = account_params[:account_id]

    xml_header = {
      "Content-Type" => "application/xml",
      Accept: "application/xml",
      access_control_allow_credentials: 'true',
      access_control_allow_origin: 'http://localhost:3000' # temporary testing
    }

    xml_credentials = {
      username: ENV["BOOMI_USERNAME"], password: ENV["BOOMI_PW"]
    }

    xml_data = %{<?xml version="1.0" encoding="UTF-8"?>
      <ProcessExecutionRequest processId="process ID" atomId="atom ID" xmlns="http://api.platform.boomi.com/">
        <ProcessProperties>
            <ProcessProperty>
              <Name>accountID</Name>
                <Value>#{account_to_push.account_id}</Value>
            </ProcessProperty>
        </ProcessProperties>
       </ProcessExecutionRequest>}


    result = HTTParty.post('https://api.boomi.com/api/rest/v1/' + ENV["BOOMI_ACCOUNT"] + '/executeProcess',
      :body => xml_data,
      :headers => xml_header,
      :basic_auth => xml_credentials
    )

    render xml: result.body
  end
end


Here is the backend work for creating the correctly formatted HTTParty Post request XML for the ExecuteProcess feature in the BoomiAPI!  I can provide the shapes used and how to access the Dynamic Process Property being passed back

 

Hope this helps someone.

Outcomes