Asking for PHP help adding "auto_merge_duplicates" to $person in WordPress

Hello,

I was asked to look into why submitted forms on a WordPress website was creating duplicate entries on Pipedrive when the client name was slightly different between submissions. The website Pipedrive API integration was already coded by someone else, so I am Johnny-come-lately here.

Working with support I was advised that we should add the following to the Person object:

POST /v1/persons

request body:

{ “_meta”: { “auto_merge_duplicates”: true }, “name”: “John Smith”, “email”: “john@example.com”, … }

With the _meta.auto_merge_duplicates enabled, the API will first try to look up an existing person with the same details as provided; if a match is found it will update the existing record with the supplied data instead of adding a new one. if no match is found it will add it as a new person.

I was also told this would then filter duplicates using the following logic:

email && (org_id || name || phone)

So this should filter out duplicates, and instead simply update the existing Pipedrive record.

The last support suggestion was to add the following:

$data = array("name" => "Harry Potter", 
                "org_id" => "6",
                "_meta" => array("auto_merge_duplicates" => true));                                                      

$data_string = json_encode($data);                                                                                   
                                                                                                                    
$ch = curl_init('https://api.pipedrive.com/v1/persons?api_token=APITOKENHERE');                                                                     
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   
                                                                                                                     
$result = curl_exec($ch);
curl_close($ch);

But it is not working. Pipedrive support ran with it as far as they could, but there was no one with good PHP coding skills on staff to help out, and they suggested I post the code here in the hopes someone might spot what is wrong with this implementation.

So here is the code in the functions.php file right now. It does work to create a new “Person” but we are still getting duplicates created using test submissions with identical email addresses and phone numbers or names.

Any help is greatly appreciated!

Rick Stewart

---- code as is —

add_action( 'wpcf7_submit', 'pipedrive_form' );
function pipedrive_form( $wpcf7 ) {
	$submission = WPCF7_Submission::get_instance();
	if ( $submission ) {
		$posted_data = $submission->get_posted_data();
	} else {
		return;
	}

	if ( empty( $posted_data['your-name'] ) || empty( $posted_data['your-email'] ) ) {
		return;
	}

	$api_token = "fa99be49xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
	$phone     = "";

	if ( isset( $posted_data['phone'] ) ) {
		$phone = $posted_data['phone'];
	}

	$person = array(
		'_meta'                                    => array( 'auto_merge_duplicates' => true ),
		'name'                                     => $posted_data['your-name'],
		'email'                                    => $posted_data['your-email'],
		'phone'                                    => $phone,
		'3eac661f7637be3fxxxxxxxxxxxxxxxxxxxxxxx ' => $posted_data['country'],
	);

	$person_string = json_encode( $person );

	$ref = "";

	if ( isset( $posted_data['referred'] ) ) {
		$ref = $posted_data['referred'];
	}

	$goal = "";

	if ( isset( $posted_data['goal'] ) ) {
		$goal = $posted_data['goal'];
	}

	$deal = array(
		'title'                                    => $posted_data['your-name'] . ' deal',
		'6aeddd791eaaxxxxxxxxxxxxxxxxxxxxxxxxxxxx' => $goal,
		'cb8bf17d1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' => $ref,

	);

	$host = "";

	if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
		$host = $_SERVER['HTTP_REFERER'];
	}

	if ( ( strpos( $host, '/live' ) !== false || strpos( $host, '/affiliate-apply' ) !== false ) || ( ! isset( $posted_data['your-name'] ) || ! isset( $posted_data['your-email'] ) ) ) {
		return;
	}

	$deal['8588f2fbaxxxxxxxxxxxxxxxxx'] = $host;
	$deal['12d3ebd9xxxxxxxxxxxxxxxxx '] = $_COOKIE['wp_referrer'];
	$url                                = "https://api.pipedrive.com/v1/persons?api_token=" . $api_token;

	$ch = curl_init();
	curl_setopt( $ch, CURLOPT_URL, $url );
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt( $ch, CURLOPT_POST, true );
	curl_setopt( $ch, CURLOPT_POSTFIELDS, $person_string );
	curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
			'Content-Type: application/json',
			'Content-Length: ' . strlen( $person_string ),
		) );
	
	$output = curl_exec( $ch );
	curl_close( $ch );
}

Hi Rick

I can’t really see anything wrong with the code, but I also can’t find any documentation about the “auto_merge_duplicates” meta flag anywhere.

If you’re experienced with the browser developer tool, you could make a request at:
https://developers.pipedrive.com/docs/api/v1/#!/Persons/post_persons
and edit and resend the request with the auto_merge_duplicates flag set in the request body.

It probably makes no sense, but feel free to ask if this is still an issue :slight_smile:

1 Like