Integrating Netsuite Forms with Drupal Webform

Submitted by Brandon Cone on 04/26/2013 - 09:14:am

Recently we did some work for a client to integrate a new Drupal website with their existing Netsuite CRM system*.  One of the issues we encountered was meeting their desire to use Drupal's Webform module while still recording the submissions within NetSuite.  Normally this is no big deal using a custom submit handler but NetSuite expects their forms to be handled a certain way so we had to get creative.

Because of Drupal's significantly useful hook system, we were able to accomplish this through a combination of webform (the actual form) configuration and a single custom module.

Here were the requirements from the Drupal side:

1. It's important that you are able to map the machine name of your webform fields to their NetSuite form counterparts.  There are a couple ways in which you an accomplish this.

A) We went the easy route and named the webform fields the same as the NetSuite form fields.  

B) Alternatively, you could create a mapping function to get the appropriate field name based on the machine name.  A switch statement would allow you to map multiple webform fields to the same NetSuite fields (if using multiple forms).

2. You need to know where to send the submitted form values.  In order to keep this configurable for our client, we implemented a hidden webform field for the submission URL.  We then were able to use the presence of this field to know if the form being viewed (through hook_form_alter) needed to go through our submission handler for NetSuite submission.  Using hook_form_alter, add your submission handler to the form's #submit array.

 

  1. /**
  2.  * Implements hook_form_alter()
  3.  */
  4. function my_module_form_alter(&$form, &$form_state, $form_id) {
  5.  
  6.   // If this is a Webform form instance.
  7.   if (strpos($form_id, 'webform_client_form') === 0) {
  8.  
  9.     // The form has a submission_url, so we need to add a submit handler to build
  10.     // the url for submission of the form values to NetSuite.
  11.     if (array_key_exists('submission_url', $form['submitted'])) {
  12.       // Don't overwrite the array or you'll lose the webform submission handler(s).
  13.       // Put our submit handler first in the array so that the ['values'] array is still as we expect.
  14.       $form['#submit'] = array_merge(array('my_module_submit_handler'), $form['#submit']);
  15.     }
  16.   }
  17. }

 

3. Iterate  the submitted values and build the URL for submission to NetSuite.  NetSuite expects that their contact forms will actually be loaded, filled out, and submitted so we have to deviate from the normal Drupal_http_request() method of submitting values via an HTTP_POST request. 

  1. /**
  2.  * Submit handler for webform submissions to netsuite
  3.  */
  4. function my_module_submit_handler($form, $form_state) {
  5.   $request_url = $form_state['values']['submitted']['submission_url'];
  6.   // The submission_url may or may not have the appended & for adding arguments/
  7.   if (!preg_match('/&$/', $request_url)) {
  8.     $request_url .= '&';
  9.   }
  10.   $values = array();
  11.   //We are assuming that the values and field names map 1 to 1 with
  12.   //the fields in NetSuite.
  13.   foreach ($form_state['values']['submitted'] as $field => $value) {
  14.     $values[$field] = urlencode($field) .'=' .urlencode($value);
  15.   }
  16.   $data_string = implode('&', $values);
  17.   //NetSuite does not allow POST operations and to get the form to submit it must be loaded
  18.   //so we set a session variable which is used to build an iframe on the next page load
  19.   //which will load the form so that it can auto-submit.
  20.   global $_SESSION;
  21.   $_SESSION['iframe_url'] = $request_url . $data_string;
  22. }
  23.  
  24. /**
  25.  * Implements hook_page_alter()
  26.  */
  27. function my_module_page_alter(&$page) {
  28.   if (isset($_SESSION) && array_key_exists('iframe_url',$_SESSION)) {
  29.     $page['footer']['form']['#markup'] = '<iframe frameborder="0" height="00" scrolling="no"
  30.      src="'.$_SESSION['iframe_url'] .'" width="00"></iframe>';
  31.     unset($_SESSION['iframe_url']);
  32.   }
  33. }

 

Requirements from the NetSuite side: 

1. Update your NetSuite contact forms so that they auto-submit when loaded.  This is the final piece to getting your Drupal webforms to submit to NetSuite.  After you have implemented step 3 above, the next page load will embed the NetSuite form (populated with the submitted values) into the next page load.  Following the instructions in this blog post will set your NetSuite form to auto-submit.

That's it.  Fill out a form and see it happen.  

* Netsuite provides more than just CRM services