Update the phone numbers of a person

Hi,

In the api documentation of the update person endpoint ( https://developers.pipedrive.com/docs/api/v1/#!/Persons/put_persons_id ) it is noted that it’s possible to update one or more phone numbers by pasting them in the same format as outputted by the get method. I tried several approaches like:

    [{"label": "mobile", "value": "00123456", "primary": true}, {"label": "mobile", "value": "0098765", "primary": false}]
    "00123456", "0092875645"

that resulted all in having one phone number with the complete text.
So what is the right syntax to update more than one phone number for a contact?

thanks Christian

I tried it also in python:

import requests
data = {'phone': [{"label": "work", "value": "1234567", "primary": True}, {"label": "work", "value": "0098765", "primary": False}]}
resp = requests.put("https://api.pipedrive.com/v1/persons/2569?api_token=mytoken", data=data)

which ends in the fact that the first phone number is set to “primary” and the second one remains completely untouched. Has anyone an Idea where this comes from?

Hi Christian,
I think it’s just a formatting issue. Both of the snippets you posted are not the correct JSON format.

Make sure you send a JSON that looks exactly like this:

{
    "phone": [
        {
            "label": "work",
            "value": "1234",
            "primary": true
        },
        {
            "label": "home",
            "value": "4567",
            "primary": false
        }
    ]
}

Let me know if it helps :slight_smile:

1 Like

Hi,

that helped. Thanks.

To post here a working example in python:

import requests
import json
data = {'phone': [{"label": "work", "value": "1234", "primary": True}, {"label": "work", "value": "5678", "primary": False}]}
headers = {'content-type': 'application/json'}
resp = requests.put("https://api.pipedrive.com/v1/persons/2569?api_token=my_token", data=json.dumps(data), headers=headers)
1 Like

I can’t for the life of me get this to work in PHP. I’ve tried creating arrays exactly as shown above, but the entire array ends out getting stuffed into the phone’s “value” field. Does anyone have working code they can share?

Thanks all in advance,
Dovid

$data = array(
  'phone' => array(
    array(
      'label' => 'work',
      'value' => '1234',
      'primary' => True,
    ),
    array(
      'label' => 'home',
      'value' => '4567',
      'primary' => False,
    ),
  )
);

// Deal ID
$person_id = 1234;

// URL for updating a Deal
$url = 'https://' . $company_domain . '.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_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

echo 'Sending request...' . PHP_EOL;

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

I think this worked for me… I tried it in php before finding the solution in python. Maybe that helps…

Thanks for the quick reply. I’m doing something a drop more complicated - creating a new contact via the “persons” POST command. But I’m sending the identical array and it’s still just not working.

Here are a few lines from the code I’m using (simplified):

	$person = array(
	"name" => "$first $last",
	"first_name" => $first,
	"last_name"=> $last
);
$person["email"] = $email;
$person["phone"] = array(
	array(
		'label' => 'work',
		'value' => '1234',
		'primary' => True,
	),
	array(
		'label' => 'home',
		'value' => '4567',
		'primary' => False,
	),
);

Running print_r on $person results in this:

Array ( [name] => Joe Schmoe [first_name] => Joe [last_name] => Schmoe [email] => me@abcd.com [phone] => Array ( [0] => Array ( [label] => work [value] => 1234 [primary] => 1 ) [1] => Array ( [label] => home [value] => 4567 [primary] => ) ) [d3d0c1b860cbc26111b30599621499c14b77519c] => 124847 ) 

So far so good I think. I try to load it in with these commands:

curl_setopt( $ch, CURLOPT_POSTFIELDS, $person );
$output = curl_exec( $ch );

And within Pipedrive everything else goes in fine, but the phone field just has the value “Array” in it and nothing more - as if it saw an array and had no idea what to do with it and so called it an “array” and quit.

I guess if I’m desperate, I could create the person in one step and then add his phones in a separate step, via the code you sent (assuming it works). But I’m sure there’s supposed to be a way to do this in one step, but it just ain’t working for me.

Thanks so much again.

Try changing this line, from

curl_setopt( $ch, CURLOPT_POSTFIELDS, $person );

to

curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($person));

Let me know if it works.

1 Like

Yes, hooray!!! That did it!

Thanks - such a small technicality!
Dovid

1 Like

Awesome! Glad it helped :smiley:

Hi,
I’m having a similar problem. I can’t make this work to send the phone number as mobile. I’ve tried several ways, but none seems to work.
self.data['phone'] = [{"label": "mobile","value": dados['tel'],"primary": True}]

This way, the phone field is somehow setted as ‘label’, which is bizarre.

Please, note that I already have a dict in my class and it is what I send to the API. If try this, works, but Pipedrive consider this as the work phone for my contact:
self.data['phone'] = 123456

How can I set what kindo of phone I’m sending via API?

Of course, I’ve tried @cklos way, but it did not work for me. What am I missing, guys?

@vbrito

I think this should be seen with the rest of the code :thinking:
Can you send a more comprehensive snippet with the preparation and sending of the request?
You can only leave things related to the phone field.

Of course! So, just to clarify, what I do now is this:
self.data['phone'] = dados['tel'] Where dados['tel'] is a number, like 123456… And self.data is a dict() with others informations about the person I’m trying to add.

After that, I send this dict to the API this way:

url = 'https://' + config['domain'] + '/v1/persons/?api_token=' + config['token']
req = requests.post(url, data=self.data ) 

It works, but Pipedrive consider this phone number as work phone number of the client. What I want to do is set it as mobile, instead of work.

So, I tried to work self.data['phone'] as array and as dict, like I mentioned before. It did not work and had a weird behavior. Pipedrive created the person with phone number as “label” and I have no idea why.

Basically, it turns this
self.data['phone'] = [{"label": "mobile","value": dados['tel'],"primary": True}]
into this
"phone":[{"label":"","value":"label","primary":true}]

Also, I have other process that updates a person if it has already been created. Maybe the only way to do what I want is through PUT method and not POST method, therefore it’s only possible to do when updating? Thats the only difference between what I’m doing and what @cklos did, I guess.

It looks good… I suspect something strange happens when the library you’re using is preparing the data for the actual request.

Can you print the body of the request you’re sending?

Hi, I made it work!
What was key here was the header, which I did not configure before, and I had to convert my dict into a json when I’m sending it. So, turns out this was indeed the right to send it.

    self.data['phone'] = [{"label": "mobile","value": dados['tel'],"primary": True}]
    url = 'https://' + config['domain'] + '/v1/persons/?api_token=' + config['token']
    headers = {'content-type': 'application/json'}
    json_data = json.dumps(self.data)

    req = requests.post(url, data=json_data, headers=headers)
2 Likes