Updating custom fields' values
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 entities as well.
If you are updating a single option and/or multiple option custom Product field, the process is a little different. You have to pass the
id
of the option instead of the value. Please take a look at the section below.
How to update custom fields
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”.
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={YOURAPITOKEN}
. All available endpoints and their URLs are described in our API Reference.
Method | URL | Useful for |
---|---|---|
GET | /dealFields | Getting all deal fields |
GET | /organizationFields | Getting all organization fields |
GET | /personFields | Getting all person fields |
GET | /productFields | Getting all product fields |
Pass field selectors
To improve the request, it would be wise to pass in field selectors to indicate which fields you'd like to fetch instead of getting all Deal fields. This will 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={YOURAPITOKEN}
Here’s an example of what the request should look like in PHP. Don’t forget to use your own api_token
and company_domain
.
<?php
// Content of getDealFields.php
// Pipedrive API token
$api_token = '{YOURAPITOKEN}';
// Pipedrive company domain
$company_domain = '{COMPANYDOMAIN}';
// 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 with the deal_id
being 567
would look like this: https://{COMPANYDOMAIN}.pipedrive.com/api/v1/deals/567?api_token={YOURAPITOKEN}
.
With the PUT
request, you need to pass along the key
(field API key) as a parameter and add a new value. 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 with your deal_id
(how to find the Deal ID), api_token
and company_domain
.
<?php
// Content of updateDeal.php
// Pipedrive API token
$api_token = '{YOURAPITOKEN}';
// Pipedrive company domain
$company_domain = '{COMPANYDOMAIN}';
// 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!';
//All tutorial Node.Js code examples are for reference only and shouldn’t be used in production code as is. In production, a new pipedrive.ApiClient() instance should be initialised separately for each request.
const pipedrive = require('pipedrive');
const defaultClient = new pipedrive.ApiClient();
// 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(defaultClient);
const dealsApi = new pipedrive.DealsApi(defaultClient);
// 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();
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.
How to update a single/multiple option custom product field
To update a single option and/or multiple option custom product field, you have to pass the id
of the option or an array of IDs to update multiple possible values. Read on to find out how to do this with our example tutorial.
Let’s say you have a multiple option custom product field called “Vehicle add-ons” with three options: “armor”, “titanium wheels” and “thrusters”.
For one of your products – “Batmobile”, the “armor” option has already been selected. You now want to add a second option, “titanium wheels”, to the product.
Step 1: GET the key and options for the custom field named “Vehicle add-ons”
First, create a file getProductFields.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 for getting Product fields. An example would look like this https://{COMPANYDOMAIN}.pipedrive.com/api/v1/productFields?start=0&api_token={YOURAPITOKEN}
. All available endpoints and their URLs are described in our API Reference.
Method | URL | Useful for |
---|---|---|
GET | /productFields | Getting all product fields |
Pass field selectors
To obtain a short and sweet output, the fields you want to pass are key
,name
and options
. An example URL with these fields would look like this: https://{COMPANYDOMAIN}.pipedrive.com/api/v1/productFields:(key,name,options)?start=0&api_token={YOURAPITOKEN}
.
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 getProductFields.php
// Pipedrive API token
$api_token = '{YOURAPITOKEN}';
// Pipedrive company domain
$company_domain = '{COMPANYDOMAIN}';
// URL for getting Deal Fields
$url = 'https://' . $company_domain . '.pipedrive.com/api/v1/productFields:(key,name,options)?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']);
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 “Vehicle add-ons” is 576da0ff55f3635ae48bfe1416854dfc2d3c692a
. You will then see the options
for your multiple option custom field with their relevant label
and id
:
{
"success": true,
"data": [
{
"key": "576da0ff55f3635ae48bfe1416854dfc2d3c692a",
"name": "Vehicle add-ons",
"options": [
{
"label": "armor",
"id": 11
},
{
"label": "titanium wheels",
"id": 12
},
{
"label": "thrusters",
"id": 13
}
]
}
]
}
Step 3: Send the additional option by making a PUT request
First, create a file updateProduct.php
.
To make a PUT request, you’ll need the correct URL for updating a Product field. An example with the product_id
being 789
would look like this:
https://{COMPANYDOMAIN}.pipedrive.com/api/v1/products/789?api_token={YOURAPITOKEN}
.
With the PUT
request, you must pass along the key
(field API key) as a body parameter and add the relevant options
as an array of IDs.
Here’s an example of what the request should look like in PHP. Remember to replace the data in the example with yours (the api_token
and the company_domain
).
<?php
// Content of updateProduct.php
// Pipedrive API token
$api_token = '{YOURAPITOKEN}';
// Pipedrive company domain
$company_domain = '{COMPANYDOMAIN}';
// Pass the multiple option field's key as a parameter and add the new values as an array
$data = array(
'576da0ff55f3635ae48bfe1416854dfc2d3c692a' => [11,12]
);
// The Batmobile Product ID
$product_id = 789;
// URL for updating a Product
$url = 'https://' . $company_domain . '.pipedrive.com/api/v1/products/' . $product_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 multiple option custom field was updated successfully!';
//All tutorial Node.Js code examples are for reference only and shouldn’t be used in production code as is. In production, a new pipedrive.ApiClient() instance should be initialised separately for each request.
const pipedrive = require('pipedrive');
const defaultClient = new pipedrive.ApiClient();
// 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 PRODUCT_ID = 789; // The ID of the Product which will be updated
const fieldsApi = new pipedrive.ProductFieldsApi(defaultClient);
const productsApi = new pipedrive.ProductsApi(defaultClient);
// Get all Product fields (keep in mind pagination)
const productFields = await fieldsApi.getProductFields();
// Find the field you would like to set new values to on a Product
const vehicleAddOnsField = productFields.data.find(field => field.name === 'Vehicle add-ons');
const updatedProduct = await productsApi.updateProduct(PRODUCT_ID, {
[vehicleAddOnsField.key]: [11,12]
});
console.log('The value of the custom field was updated successfully!', updatedProduct);
} catch (err) {
const errorToLog = err.context?.body || err;
console.log('Updating failed', errorToLog);
}
}
updatingCustomFieldValue();
And now execute the code by using the php updateProduct.php
command in the command line.
Step 4: Check the payload of the PUT request you just made
Find the updated options of the same key
(field API key) you used as a parameter in the PUT
request. Check if the new value of this key
now includes the IDs of both options that you wanted: 11
for armor
and 12
for titanium wheels
.
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": 789,
"name": "Batmobile",
"576da0ff55f3635ae48bfe1416854dfc2d3c692a": "11,12",
}
}
Updated 21 days ago