Updating custom fields' values

Let's say you've got a custom Deal field named "Appointed manager" and a Deal called "Harvey Dent". In this Deal, you want to update the value of the custom field from "Batman" to "Joker".

Follow the next steps to see how to do it.

📘

In this example, we're going to update the value of a custom Deal field, but you can adjust and apply this tutorial to the custom fields of Organization, Person, and Product as well.



Step 1: GET the key for the custom field named "Appointed manager"


First, create a file getDealFields.php and follow our tutorials on how to find the API token and how to get the company domain.

To make a GET request, you'll need the correct URL meant for getting Deal fields, an example would look like this https://companydomain.pipedrive.com/api/v1/dealFields?start=0&api_token=659c9fddb16335e48cc67114694b52074e812. All available endpoints and their URLs are described in our API Reference.

Method

URL

Useful for

PUT

/dealFields/{id}

Updating a custom Deal field

PUT

/organizationFields/{id}

Updating a custom Organization field

PUT

/personFields/{id}

Updating a custom Person field

PUT

/productFields/{id}

Updating a custom Product field

Pass field selectors

To improve the request, it would be smart to pass in field selectors to indicate which fields you'd like to fetch instead of getting all Deal fields. This will later make the output short and sweet, and it'll be very convenient for you to find the key (the field API key) for the custom field "Appointed manager".

An example URL with the key and name (to know which belongs to which) field selectors looks like this: https://companydomain.pipedrive.com/api/v1/dealFields:(key,name)?start=0&api_token=659c9fddb16335e48cc67114694b52074e812

Here's an example of what the request should look like in PHP. Don't forget to replace the data in the example with yours (the api_token and the company_domain).

<?php
// Content of getDealFields.php
  
// Pipedrive API token
$api_token = '659c9fddb16335e48cc67114694b52074e812';
  
// Pipedrive company domain
$company_domain = 'theunicorntail';
 
// URL for getting Deal Fields
$url = 'https://' . $company_domain . '.pipedrive.com/api/v1/dealFields:(key,name)?start=0&api_token=' . $api_token;
 
// GET request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   
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 a PHP array
$result = json_decode($output, true);
 
// Check if data returned in the result is not empty
if (empty($result['data'])) {
    exit('Error: ' . $result['error'] . PHP_EOL);
}
 
// Print out full data
print_r($result['data']);

Execute the code by using the php getDealFields.php command in the command line.



Step 2: Check the payload of the GET request you just made


If the request was successful, you'll learn from the output that the key (field API key) for the custom field "Appointed manager" is dcf558aac1ae4e8c4f849ba5e668430d8df9be12:

{
    "success": true,
    "data": [
        {
            "key": "dcf558aac1ae4e8c4f849ba5e668430d8df9be12",
            "name": "Appointed manager"
        }
    ]
}


Step 3: Send the new value by making a PUT request


First, create a file updateDeal.php.

To make a PUT request, you'll need the correct URL meant for updating a Deal field, an example would look like this https://companydomain.pipedrive.com/api/v1/567?api_token=659c9fddb16335e48cc67114694b52074e812.

With the PUT request, you need to pass along the key (field API key) as a parameter and add a new value to it. In this case, you need to change the value of the "Appointed manager" custom field from "Batman" to "Joker".

Here's an example of what the PUT request should look like in PHP. Don't forget to replace the data in the example for your actual deal_id (how to find the Deal ID), the api_token and the company_domain.

<?php
// Content of updateDeal.php
   
// Pipedrive API token
$api_token = '659c9fddb16335e48cc67114694b52074e812';
  
// Pipedrive company domain
$company_domain = 'theunicorntail';
  
// Pass custom field API key as parameter and add the new value
$data = array(
  'dcf558aac1ae4e8c4f849ba5e668430d8df9be12' => 'Joker'
);
  
// The Harvey Dent Deal ID
$deal_id = 260;
  
// URL for updating a Deal
$url = 'https://' . $company_domain . '.pipedrive.com/api/v1/deals/' . $deal_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);
  
// 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 a PHP array
$result = json_decode($output, true);
  
// Check if the data returned in the result is not empty
if (empty($result['data'])) {
    exit('Updating failed' . PHP_EOL);
}
  
// Check if the updating was successful
if (!empty($result['data']['id']))
 
echo 'The value of the custom field was updated successfully!';
const pipedrive = require('pipedrive');
const defaultClient = pipedrive.ApiClient.instance;

// Configure authorization by settings api key
// PIPEDRIVE_API_KEY is an environment variable that holds real api key
defaultClient.authentications.api_key.apiKey = process.env.PIPEDRIVE_API_KEY;
 
async function updatingCustomFieldValue() {
    try {
        console.log('Sending request...');
        
        const DEAL_ID = 158; // An ID of Deal which will be updated
        const fieldsApi = new pipedrive.DealFieldsApi();
        const dealsApi = new pipedrive.DealsApi();

        // Get all Deal fields (keep in mind pagination)
        const dealFields = await fieldsApi.getDealFields();
        // Find a field you would like to set a new value to on a Deal
        const appointedManagerField = dealFields.data.find(field => field.name === 'Appointed manager');

        const updatedDeal = await dealsApi.updateDeal(DEAL_ID, {
            [appointedManagerField.key]: 'Joker'
        });

        console.log('The value of the custom field was updated successfully!', updatedDeal);
    } catch (err) {
        const errorToLog = err.context?.body || err;

        console.log('Updating failed', errorToLog);
    }
}

updatingCustomFieldValue();v

And now execute the code by using the php updateDeal.php command in the command line.



Step 4: Check the payload of the PUT request you just made


Find the new value of the same key (field API key) you used as a parameter in the PUT request. Check if the new value of this key is now "Joker".

The original payload is probably quite bulky (unfortunately, the field selector works only for GET requests), so here's the section you should look for:

{
    "success": true,
    "data": {
        "id": 260,
        "title": "Harvey Dent",
        "add_time": "2018-09-07 12:08:09",
        "update_time": "2018-09-07 12:57:52",
        "d9841077efc3a2c43b371b72cd1d0d682dddf968": null,
        "cbe7a7df3df2590be065b39df863912c6f030007": null,
        "dcf558aac1ae4e8c4f849ba5e668430d8df9be12": "Joker",   
        }
    }
}

And there you go, you have now updated the value within a custom Deal field named "Appointed manager" from "Batman" to "Joker" in your Deal called "Harvey Dent". You can also check the change from the web app.


Did this page help you?