Adding a Deal from webform that adds both DEAL and PERSON - HELP!

Hi all, I’m trying to post a deal from my website that will update both deal and the person attached to the deal.

So Far I have got it working so that a deal adds with a title / value and a persons name. However in the form on my website I have their name / email / phone number and a message.

When using the pipedrive “form” it posts ok but I need a postcode lookup and some other stuff, so i have to use my own form.

From looking i need to somehow post that form I have but then fetch / get the PERSON and update them all in the same call.

bit stuck i have a feeling i need to use ajax or something but i am a bit of a noob…

Hi Duane,
Let’s see if I can help you one step at a time… Where, exactly are you stuck right now?

The more specific you can make the question, the better I can help you.

So i have a php page on my website that posts to pipedrive api.

This works, i can create a new deal:

$deal = array(
                'title' => "$address1, $address2, $town, $postcode, $email, $number, $message",
                'value' => "995",
                '11c901dda315b2114a19a34cde88f0bb3283875a' => 'Website',
                'org_id' => '',
                'person_id' => "$name",
            );

            $url = 'https://coppenwalllimited.pipedrive.com/v1/deals?api_token=' . $api_token;

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $deal);

            //echo 'Sending request...' . PHP_EOL;

            $output = curl_exec($ch);
            curl_close($ch);

            // Create an array from the data that is sent back from the API
            // As the original content from server is in JSON format, you need to convert it to PHP array
            $result = json_decode($output, true); 

This is good but I’m having to do the following for the deal title.

‘title’ => “$address1, $address2, $town, $postcode, $email, $number, $message”,

But the DEAL api end point doesn’t have EMAIL / NUMBER / MESSAGE as these are PERSON fields.

My problem is I cannot seem to update both person and deal at the same time.

Thanks for responding by the way! much appreciated.

1 Like

Ok. That’s an easy fix :slight_smile:

  1. You first need to add the person, passing all the information you need (name, email, address, etc.)
  2. When you add the person, in the response you’ll see its id.
  3. You can pass the new person’s id to the deal, as person_id (instead of $name)
 <?php
            if (isset($_REQUEST['email']))  {
            $admin_email = "team@coppenwall.com";
            $email = $_REQUEST['email'];
            $subject = "Valuation Request from Coppenwall Website";
            $message = $_REQUEST['message'];
            $address1 = $_REQUEST['address_line_1'];
            $address2 = $_REQUEST['address_line_2'];
            $town = $_REQUEST['posttown'];
            $postcode = $_REQUEST['postcode'];
            $name = $_REQUEST['name'];
            $number = $_REQUEST['number'];
            $body = "$name, $email, $number, $address1, $address2, $town, $postcode, $message";
            
                
            // Pipedrive API token
            $api_token = 'xxxxx';

            // PERSON
            // New name for the Person and new id of the organization they will belong to
            $person = array(
                'name' => "$name",
                'email' => "$email",
                'phone' => "$phone",
                'org_id' => "$name",
                '9a9f1c6d65d874cce6509a6978c1a3ba1eb8b2b1' => "$phone@txtlocal.co.uk",
            );

            // Person's ID
            $person_id = "$name";

            // URL for updating a Person
            $url = 'https://coppenwalllimited.pipedrive.com/v1/persons/' . $person_id . '?api_token=' . $api_token;
                
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $person);

            //echo 'Sending request...' . PHP_EOL;

            $output = curl_exec($ch);
            curl_close($ch);

            // Create an array from the data that is sent back from the API
            // As the original content from server is in JSON format, you need to convert it to PHP array
            $result = json_decode($output, true);
                
                
                
                
             // Deal title and Organization ID
            $deal = array(
                'title' => "$address1, $address2, $town, $postcode, $email, $number, $message",
                'value' => "995",
                '11c901dda315b2114a19a34cde88f0bb3283875a' => 'Website',
                'org_id' => '',
                'person_id' => "$person_id",
            );

            $url = 'https://coppenwalllimited.pipedrive.com/v1/deals?api_token=' . $api_token;

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $deal);

            //echo 'Sending request...' . PHP_EOL;

            $output = curl_exec($ch);
            curl_close($ch);

            // Create an array from the data that is sent back from the API
            // As the original content from server is in JSON format, you need to convert it to PHP array
            $result = json_decode($output, true);     

                
            echo '
                <div>
                    <div class="row">
                        <div class="text-center">
                            <div class="display-t">
                                <div class="display-tc thanks-social book-thanks" data-animate-effect="fadeIn">
                                    <h1><i class="material-icons thanks-check animate-box">check_circle</i></h1>
                                    <h1 class="animate-box">Thank you</h1>
                                    <p class="">Your details have been sent, a member of the team will be in touch soon.</p>
                                    <a href="https://twitter.com/coppenwall" target="_blank" title="Twitter"><span class="icon-twitter-social-logotype btg"></span></a>
                                    <a href="https://www.facebook.com/coppenwall/" target="_blank" title="Facebook"><span class="icon-facebook-app-logo btg"></span></a>
                                    <a href="https://www.instagram.com/coppenwall/" target="_blank" title="Instagram"><span class="icon-instagram-social-network-logo-of-photo-camera btg"></span></a>
                                    <br><br><br><a href="/" class="btn btn-primary">Coppenwall Homepage</a><br><br>
                                </div>
                            </div>
                        </div>
                    </div>
                </div><br><br>
                ';
                
                
            }

            else  {
          ?>

Hi i tried that still something not quite right, apologies but I’ve just pasted the code in here (not the safest I know) but I figured you may spot something.

Implemented a similar logic recently and your post gave me a nice starting point.

As dani mentioned, you need to send the id, that you receive in the response when creating the person. Some things that seem incorrect in your code:

'org_id' => "$name"

org_id needs to be the id of the organization, not just a name. If no organization exists yet, you have to create it first and take the id from the response.

After creating the person, you might want to check if it was successful, seeing as you already have the response in an associated array (via json_decode), just check for

$result['success']

If it was successful, there will also be a field id in data (you can see the exact response on the API page when you test requests or you can also use Postman for sending requests).

$result['data']['id'] 

Use this when creating the deal:

'person_id' => $result['data']['id']

Our process is:

  1. create organization -> get id
  2. create person already with org_id -> get id
  3. create deal already with org_id, person_id

Hope it might help

1 Like

Thanks for this, I did get it working in the end - I just switched my code round and added a person first then got the response from that person and added a new deal using the person id. works a charm!

Thanks for your help.

Hey @duane have you or anyone in here have worked with CallTools? I am trying to use the Leads I get from CallTools into my DEAL and CONTACT sections. I am not much of a developer but I figure I might ask anyway, thank you.