Integrating Gravity Forms with SuiteCRM API v4_1: A Comprehensive Guide

Home » Mississauga Digital Agency Blog » Integrating Gravity Forms with SuiteCRM API v4_1: A Comprehensive Guide
SuiteCRM Gravity Forms and Wordpress Integration

How to Integrate SuiteCRM, Gravity Forms and WordPress with API Ver. 4_1

In this blog post, you’ll learn how to use Gravity Forms with SuiteCRM API v4_1 to seamlessly push leads from WordPress into SuiteCRM. We’ll provide a free plugin download, detailed code examples, and a walkthrough video.

This guide is perfect if you want to:

  • Automatically capture lead data from your website into SuiteCRM.
  • Simplify lead management by integrating WordPress forms directly with SuiteCRM.
  • Use a ready-to-go WordPress plugin designed specifically for this purpose.

You may be wondering why we just don’t use the Gravity Forms Webhooks add on to do this. The answer is that the Gravity Forms Webhooks is a great plugin and works with many API’s. However, there are a couple of reasons why I don’t use it in this case. Firstly, it doesn’t handle getting the session key. Of course you could do this manually with Postman, but you’d either have to renew it regularily, or write code to get the session key regularily. In addition, because the field data is in an array “rest_data”, Gravity Forms Webhooks doesn’t submit the request in this manner. Just a single level array with all the fields in it. So you’d have to write code anyway to re-format the webhooks output. So, taking this into account, it’s just easy to do the whole thing via code.


1. Setting Up Gravity Forms

To begin, you’ll need to create a form in Gravity Forms with the following fields:

  1. First Name (Field ID: 1.3)
  2. Last Name (Field ID: 1.6)
  3. Email (Field ID: 2)
  4. Company Name (Field ID: 3)
  5. Phone Number (Field ID: 4)
  6. Question (Field ID: 5)

Ensure that each field has a unique ID, as we will map these IDs to SuiteCRM fields in the plugin.


2. Creating the WordPress Plugin

Here’s the code for a basic WordPress plugin that integrates Gravity Forms with SuiteCRM:

Plugin Code:

<?php
/**
 * Plugin Name: Gravity Forms SuiteCRM Integration
 * Description: Push leads from Gravity Forms to SuiteCRM using API v4_1.
 * Version: 1.0
 * Author: Your Name
 */

function get_suitecrm_session_key() {
    static $session_id = null;

    if ($session_id) {
        return $session_id;
    }

    $api_url = 'https://your-suitecrm-instance/service/v4_1/rest.php';
    $username = 'your-username';
    $password = md5('your-password'); // MD5 hash of the password

    $login_payload = [
        'method' => 'login',
        'input_type' => 'JSON',
        'response_type' => 'JSON',
        'rest_data' => json_encode([
            'user_auth' => [
                'user_name' => $username,
                'password' => $password,
                'version' => '1.2'
            ],
            'application' => 'GravityForms'
        ])
    ];

    $login_response = wp_remote_post($api_url, [
        'body' => $login_payload,
        'timeout' => 15,
        'sslverify' => false
    ]);

    if (is_wp_error($login_response)) {
        error_log('SuiteCRM Login Error: ' . $login_response->get_error_message());
        return null;
    }

    $login_body = json_decode(wp_remote_retrieve_body($login_response), true);
    if (!empty($login_body['id'])) {
        $session_id = $login_body['id'];
        return $session_id;
    }

    error_log('SuiteCRM Login Failed: ' . wp_remote_retrieve_body($login_response));
    return null;
}

// Hook into Gravity Forms submission for form ID 1
add_action('gform_after_submission_1', 'push_lead_to_suitecrm', 10, 2);

function push_lead_to_suitecrm($entry, $form) {
    $session_id = get_suitecrm_session_key();

    if (!$session_id) {
        return; // Exit if session key retrieval fails
    }

    // Prepare lead data
    $lead_data = [
        'method' => 'set_entry',
        'input_type' => 'JSON',
        'response_type' => 'JSON',
        'rest_data' => json_encode([
            'session' => $session_id,
            'module_name' => 'Leads',
            'name_value_list' => [
                ["name" => "first_name", "value" => rgar($entry, '1.3')],
                ["name" => "last_name", "value" => rgar($entry, '1.6')],
                ["name" => "email1", "value" => rgar($entry, '2')],
                ["name" => "account_name", "value" => rgar($entry, '3')],
                ["name" => "phone_work", "value" => rgar($entry, '4')],
                ["name" => "description", "value" => rgar($entry, '5')],
                ["name" => "lead_source", "value" => "Web Site"]
            ]
        ])
    ];

    // Push lead to SuiteCRM
    $lead_response = wp_remote_post('https://your-suitecrm-instance/service/v4_1/rest.php', [
        'body' => $lead_data,
        'timeout' => 15,
        'sslverify' => false
    ]);

    if (is_wp_error($lead_response)) {
        error_log('SuiteCRM Lead Creation Error: ' . $lead_response->get_error_message());
        return;
    }

    $lead_body = json_decode(wp_remote_retrieve_body($lead_response), true);
    if (!empty($lead_body['id'])) {
        error_log('SuiteCRM Lead Created: ID ' . $lead_body['id']);
    } else {
        error_log('SuiteCRM Lead Creation Failed: ' . wp_remote_retrieve_body($lead_response));
    }
}

How the Plugin Works:

  1. Login to SuiteCRM: The plugin first logs into SuiteCRM using the provided credentials and retrieves a session ID.
  2. Prepare Lead Data: It maps the Gravity Forms fields to SuiteCRM lead fields.
  3. Push Lead to SuiteCRM: The plugin sends a set_entry request to SuiteCRM to create a new lead.
  4. Error Logging: If any errors occur during login or lead creation, they are logged for debugging.

3. Duplicating the Function for Another Form

If you have multiple forms in Gravity Forms that need to push data to SuiteCRM, you can easily duplicate the function by creating a new hook specific to the new form ID.

  1. Copy the Function: Duplicate the push_lead_to_suitecrm function and rename it to something unique, such as push_lead_form_2_to_suitecrm.
  2. Update the Gravity Forms Hook: Change the hook to match the new form ID.Example:
add_action('gform_after_submission_2', 'push_lead_form_2_to_suitecrm', 10, 2);
  1. Modify the Field Mapping: Update the field IDs in the $lead_data array to match the new form’s field IDs.

Example Code for a Second Form

Here’s a complete example for a second form:

// Hook into Gravity Forms submission for form ID 2
add_action('gform_after_submission_2', 'push_lead_form_2_to_suitecrm', 10, 2);

function push_lead_form_2_to_suitecrm($entry, $form) {
    $session_id = get_suitecrm_session_key();

    if (!$session_id) {
        return; // Exit if session key retrieval fails
    }

    // Prepare lead data
    $lead_data = [
        'method' => 'set_entry',
        'input_type' => 'JSON',
        'response_type' => 'JSON',
        'rest_data' => json_encode([
            'session' => $session_id,
            'module_name' => 'Leads',
            'name_value_list' => [
                ["name" => "first_name", "value" => rgar($entry, '2.3')],
                ["name" => "last_name", "value" => rgar($entry, '2.6')],
                ["name" => "email1", "value" => rgar($entry, '3')],
                ["name" => "account_name", "value" => rgar($entry, '4')],
                ["name" => "phone_work", "value" => rgar($entry, '5')],
                ["name" => "description", "value" => rgar($entry, '6')],
                ["name" => "lead_source", "value" => "Web Site"]
            ]
        ])
    ];

    // Push lead to SuiteCRM
    $lead_response = wp_remote_post('https://your-suitecrm-instance/service/v4_1/rest.php', [
        'body' => $lead_data,
        'timeout' => 15,
        'sslverify' => false
    ]);

    if (is_wp_error($lead_response)) {
        error_log('SuiteCRM Lead Creation Error: ' . $lead_response->get_error_message());
        return;
    }

    $lead_body = json_decode(wp_remote_retrieve_body($lead_response), true);
    if (!empty($lead_body['id'])) {
        error_log('SuiteCRM Lead Created: ID ' . $lead_body['id']);
    } else {
        error_log('SuiteCRM Lead Creation Failed: ' . wp_remote_retrieve_body($lead_response));
    }
}

Summary

  • Use a unique function name for each form.
  • Hook into gform_after_submission_{form_id} with the appropriate form ID.
  • Update the field mappings to match the new form fields.

Of course with futher development, I could make an admin area where the user enters the username and password, etc. This should be enough to get you started with development to suite your own needs.


4. How to Get the Plugin

You can download the plugin for free by visiting this landing page and entering your name and email. Once you submit the form, you’ll receive a download link. Then just install it in WordPress and you’re ready to begin the Tutorial.


5. YouTube Video Tutorial

We’ve also created a detailed walkthrough video showing you how to set up Gravity Forms, install the plugin, and test the integration. Watch it below:


6. Conclusion

Integrating Gravity Forms with SuiteCRM via the API v4_1 offers a robust solution for automating lead capture directly from your WordPress site. By leveraging this step-by-step guide and the free plugin provided, you can

  • streamline your CRM workflow
  • reduce manual data entry
  • ensure that every potential lead is tracked in real-time.

Whether you’re a small business owner looking to simplify your lead management or a developer aiming to build custom integrations, this guide equips you with the tools to get started quickly. If you need further assistance with SuiteCRM development, API integrations, or WordPress customization, feel free to reach out. I specialize in SuiteCRM consulting and development and would be happy to help you optimize your CRM processes.


Need help with SuiteCRM development or custom integrations?
Get in touch with me for consulting or development services tailored to your specific needs. Let’s build a solution that works for you!

,
About the Author