API Connection Error (GravityForms)

Hi @crmspace,

Thanks for another fast reply!

No, the WordPress Webhooks plugin Iā€™m using doesnā€™t have that option. I could have it customized but Iā€™m hoping for an easier solution. I was pondering trying ā€œWebtoleadā€, itā€™s a module in the SuiteCRM store, but it doesnā€™t have any reviews.

Thanks

Karen

Hey, if youā€™re just trying to push form fills to a lead or contact, I use 3rd Party Integration plugin. It works for Gravity Forms and Contact Form 7. I wrote a tutorial on my blog, itā€™s for Contact Form 7, but works pretty much the same for Gravity Forms. Plus, itā€™s a free Plugin that works with WordPress.

https://igosalesandmarketing.com/how-to-integrate-suitecrm-and-contact-form-7/

1 Like

Karen Iā€™m almost thereā€¦ @crmspace in gravity forms webhooks you can design the output. Withā€¦

add_filter( ā€˜gform_webhooks_request_data_1ā€™, ā€˜modify_dataā€™, 10, 4 );
function modify_data( $request_data, $feed, $entry, $form ){
$request_data = array(
ā€˜dataā€™ => array(
ā€˜typeā€™ => ā€˜Leadsā€™,
ā€˜attributesā€™ => array(
ā€˜nameā€™ => ā€˜new leadā€™,
ā€˜first_nameā€™ => rgar( $entry, ā€˜1.3ā€™ ),
ā€˜last_nameā€™ => rgar( $entry, ā€˜1.6ā€™ ),
ā€˜email1ā€™ => rgar( $entry, ā€˜2ā€™ ),
ā€˜account_nameā€™ => rgar( $entry, ā€˜4ā€™ ),
)
)
);
return $request_data;
}

Note: _1 in the filter limits to form id:1
The 1.3, 1.6, etc are the field IDā€™s from the form.

The output is:

[body] => {ā€œdataā€:{ā€œtypeā€:ā€œLeadsā€,ā€œattributesā€:{ā€œnameā€:ā€œnew lead",ā€œfirst_nameā€:ā€œPaultestā€,ā€œlast_nameā€:ā€œtestingā€,ā€œemail1ā€:"pablotest@testing.comā€,ā€œaccount_nameā€:ā€œmy companyā€}}}

It all looks right to me but Iā€™m getting, "code: 401; body: {ā€œerrorā€:ā€œaccess_deniedā€,ā€œmessageā€:ā€œThe resource owner or authorization server denied the request.ā€,ā€œhintā€:ā€œError while decoding to JSONā€} "

Iā€™ve tested the setup in postman and it works. Without the new JSON, it connects and gives me the error about the missing"/data:" . Anyone see anything wrong in the JSON output? Ideas?

Whoohoo! Iā€™ve successfully used Gravity Forms Webhook Add on to create a lead from a Gravity Form. Presumably, I can now create/update any record from a Gravity Form. The key thing I was missing was the content type:

Once I added that header, the Gravity Forms function works to populate the data correctly!

1 Like

Hi Paul, is there a step by step tutorial out there to set up the using the webhooks plugin?

Hey @cammie57, none that I know of, I kind of pieced it together. I definitely want to make a walk-through how to video on it. One stumbling block is that my process is still overly complicated (even though it does work). The issue I have is the V8 Api needs an access token. Webhooks doesnā€™t generate an access token on connection, so Iā€™m using Postman to initially connect and then create the access token. I use the access token in Webhooks to connect to suitecrm. This worksā€¦ until the access token expires.

So in a production environment, Iā€™d have to somehow hack the DB and make the token valid for like 5 years or something. If you want the instructions I have so far Iā€™d be happy to send them to you.

This would be a great feature if I could get a simpler way to get it working. Presumably, you could create/update any record in SuiteCRM with Gravity Forms, fairly easily by using the Webhook plugin.

This has been kind of a side project of mine when I have a spare moment just because the potential is so great. With WordPress, Gravity Forms and SuiteCRM with a pretty easy way to integrate all 3 it would be awesome!

Hereā€™s a PDF of my procedure (work in process) that I have so far.

production_api_webhooks_suitecrm_gravityforms [iGoWIKI].zip (163.2 KB)

Hi, @pstevens

Why you donā€™t use API V4.1?

I was really struggling with the V4.1 API. The V8 documentation was so much clearer. (Iā€™m kind of a newbie to APIā€™s). I also found a V8 Postman config file that I downloaded and was super helpful. I havenā€™t been able to connect via 4.1 yet. It would be great if I just needed a key to connect and use the V4.

Do you have any postman settings you can share to connect via V4.1 to say add a record. That would be super helpful!

If I can get that working, thatā€™s way simpler to configure and Iā€™ll definitely make a video for the community.

@pstevens

I am using SOAP for API V4.1. Itā€™s very easy. Which problem are you have?

Iā€™m struggling with what credentials to send to gain access and also what URL to use to add a new ā€œleadā€ letā€™s say.

@pstevens
Itā€™s working for me.

$location='https://example1.com/service/v4_1/soap.php?wsdl'
$login_parameters = array(
    "user_auth" => array(
        "user_name" => 'api_user',
        "password" => '01234567890123456789012345678901' //MD5 hash of password
    )
);
/* if using https without correct certificate */
$streamContext=stream_context_create([
    'ssl' => [
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed'=>true
    ],
]);
$client = new SoapClient($location,array('stream_context'=>$streamContext));
$session = $this->client->__soapCall('login',$login_parameters);
$record=$this->client->__soapCall(
    'set_entry',array(
        $session->id,'Leads',array(
            array('name'=>'first_name','value'=>'name_1 name_2',
            array('name'=>'email1','value'=>'mail@example2.com',
            array('name'=>'phone_mobile','value'=>'+0123456789',
            array('name'=>'description','value'=>'text',
            array('name'=>'assigned_user_id','value'=>'01234567-0123-0123-0123-012345678901')
        )
    )
);
$client->__soapCall('logout',$session->id);

Thanks Dude, thatā€™s super helpful! However my problem is that the plugin in question can only submit requests via JSON or FORM. Iā€™m not sure if I can implement it like that with the plugin. With V8 I was able to use JSON to submit. (Again this API stuff is new to me).

Thank you! I subscribe to your youtube channel. You make very helpful videos! This API stuff is new to me, but Iā€™m going to try to see what I can do with the info you posted. Looking forward to the video if you get to the bottom of this. Looks very promising!

I didnā€™t even use Gravity Forms Webhook Add on as I believe it has a fee.
I went into wp-content/themes/salient/fuctions.php. I use salient but use whatever is your current theme in WP.

Then I entered this:


add_action('gform_after_submission','post_to_third_party', 10, 2);

function post_to_third_party($entry, $form){


	//Log In
    $url = 'https://WEBSITE.COM/service/v4_1/rest.php';
    
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    // Set the POST arguments to pass to the Sugar server
    $parameters = array(
                        'user_auth' => array(
                        'user_name' => 'USERNAME',
                        'password' => md5('PASSWORD'),
                        ),
                      );

    $json = json_encode($parameters);

    $postArgs = array(
                      'method' => 'login',
                      'input_type' => 'JSON',
                      'response_type' => 'JSON',
                      'rest_data' => $json,
                    );

    curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);

    $response = curl_exec($curl);

    // Convert the result from JSON format to a PHP array
    $result = json_decode($response);

    if ( !is_object($result) ) {
        die("Error handling result.\n");
    }

    $session = $result->id;

	//Get data
	$parameters = array(
		'session' => $session,
		'module' => 'Leads',
		'name_value_list' => array (
				array('name' => 'survey_id_c',                'value' => 'XXXXX'),

				array('name' => 'primary_address_street',     'value' => rgar($entry,'3.1')),
				array('name' => 'primary_address_city',       'value' => rgar($entry,'3.3')),
				array('name' => 'primary_address_state',      'value' => rgar($entry,'3.4')),
				array('name' => 'primary_address_postalcode', 'value' => rgar($entry,'3.5')),
							
				array('name' => 'first_name',                 'value' => rgar($entry,'1.3')),
				array('name' => 'last_name',                  'value' => rgar($entry,'1.6')),
				array('name' => 'phone_work',                 'value' => rgar($entry,'2')),    
				array('name' => 'email1',                     'value' => rgar($entry,'4')),

				array('name' => 'qweb_01_c',                     'value' => rgar($entry,'9')),
				array('name' => 'qweb_02_c',                     'value' => rgar($entry,'7')),
				array('name' => 'qweb_03_c',                     'value' => rgar($entry,'10')),
				array('name' => 'qweb_04_c',                     'value' => rgar($entry,'6')),

			),
	);

	$json = json_encode($parameters);
    $postArgs = 'method=set_entry&input_type=JSON&response_type=JSON&rest_data=' . $json;
    
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
    
    $response = curl_exec($curl);
    $result = json_decode($response,true);    
    $recordId = $result['id'];

Nice @johnwreford Canā€™t wait to try that! Iā€™ve been struggling to find an easy way to send Gravity Form fills into SuiteCRM. This looks even simpler than the webhooks plugin.

Hey all I managed to get SuiteCRM API version 8 working with Gravity Forms webhooks! Iā€™m able to retrieve the auth key and then refresh it when expired and connect and post new leads to SuiteCRM using the key. Havenā€™t tried it with other modules. Iā€™ll be adding to my blog soon (when I get a minute) and post link. If anyone needs in the meantime reach out to me.

1 Like

What PHP version are you using with API V8?

Iā€™m struggling to make get the access token. Iā€™m using SuiteCRM 8.6, API V8 and have tried with PHP 8.1 and 8.2 with no success. Any tips will be welcome.

Iā€™ve been using it on all versions of PHP since 7.1 up to 8.1 without any issues. Here are some key tips:

Turn on logging in Gravity Forms, Iā€™ve setup a number of Webhooks with different systems with it and this info is super helpful for troubleshooting.

Hereā€™s from my personal notes, itā€™s been a while since I set this up, so my memory is a little fuzzy:

Getting and updating the access token

Gravity forms needs to get the access token from SuiteCRM first, then use the access token to connect to SuiteCRM to update, get, and add records.

You need to add a function to WordPressā€™s functions.php to be able to do this:

You need to enter the appropriate client_id, client_secret. The grant type is ā€œclient_credentialsā€ make sure you create this grant type in SuiteCRM Oath2

Note the _1 means this applies to form id 1

/*****add gf forms webhooks header for suitecrm**********/
add_filter( 'gform_webhooks_request_headers_1', function ( $request_headers ) {
    $token = GFCache::get( 'suitecrm_token' );
 
    if ( ! $token ) {
        $post_url = 'https://xxxxxxxxxxxxxxxxxxxxxxxxx/Api/access_token';
        $body     = json_encode(array(
            'client_id'     => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
            'client_secret' => 'GravityForms',
            'grant_type'    => 'client_credentials',
			
        ));
        $headers  = array( 'Content-Type' => 'application/vnd.api+json',
							'Accept' => 'application/vnd.api+json',
							);
 
        $response = wp_remote_post( $post_url, array( 'body' => $body, 'headers' => $headers ) );
        gf_webhooks()->log_debug( 'gform_webhooks_request_headers: response => ' . print_r( $response, true ) );
 
        if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) == 200 ) {
            $response_body_json  = wp_remote_retrieve_body( $response );
            $response_body_array = json_decode( $response_body_json, true );
 
            if ( isset( $response_body_array['access_token'] ) ) {
                $token = $response_body_array['access_token'];
                GFCache::set( 'suitecrm_token', $token, true, $response_body_array['expires_in'] - 30 );
            }
        }
    }
 
    if ( $token ) {
        gf_webhooks()->log_debug( 'gform_webhooks_request_headers: token added.' );
        $request_headers['Authorization'] = 'Bearer ' . $token;
		$request_headers['grant_type'] = 'client_credentials';

    }
 
    return $request_headers;
} );

Modify the Output

Now that you have the access credentials in the header, you have to modify the output to be in a format accepted by SuiteCRM. You can add a function where the _1 is the form it applies to.

/****GF Webhook for SuiteCRM****/
add_filter( 'gform_webhooks_request_data_1', 'modify_suite_data', 50, 4 );
function modify_suite_data( $request_data, $feed, $entry, $form ){
$request_data = array(
'data' => array(
'type' => 'Leads',
'attributes' => array(
'name' => 'new lead',
'first_name' => rgar( $entry, '1.3' ),
'last_name' => rgar( $entry, '1.6' ),
'email1' => rgar( $entry, '2' ),
'account_name' => rgar( $entry, '4' ),
)
)
);
gf_webhooks()->log_debug( 'request modified' );
return $request_data;
}

Thanks @pstevens.

I think my issue is my server. Iā€™m using Nginx. That one comes with itā€™s own issues.

This post has been helpful to me in case somebody uses he same server: