Issue Syncing Custom Fields using API keys

Hello,

I am having trouble with some code. I have a series of custom fields that are associated to an organization. I want these custom organization fields to be compared to custom fields on the person side to determine if the values are different, if the value is different, take the organization value and copy that value to the person field.

I need this because we use an automation tool that pulls in contacts but can only pull in contacts based on persons fields/values and not organization values. This means we cannot use pipedrive filters to filter out out people we do not want to import based on organization fields.

The solution seems to be:

Check Organziation Custom_PickList_Value
If different than
Person Custom_PickList_Value

Then copy… but i keep getting an error with this code and i cannot deduce what is the reason. It simply says it was not successful.

I am using the organization API key, followed by the two custom field API keys.

Any ideas?

import requests
import time

def copy_custom_field_value():
“”"Copies the value of a custom field to another custom field in Pipedrive.

Args:
api_token: Your Pipedrive API token.
custom_field_id_1: The API key of the custom field that you want to copy the value from.
custom_field_id_2: The API key of the custom field that you want to copy the value to.

Returns:
The response from the Pipedrive API.
“”"

api_token = ‘xxxxxxxxxxxxxxxxxxxxxxx’
custom_field_id_1 = ‘xxxxxxxxxxxxxxxxxxxxxxxxx’
custom_field_id_2 = ‘xxxxxxxxxxxxxxxxxxxx’

response = requests.get(‘Log in’.format(custom_field_id_1))

if response.status_code == 200:
data = response.json()
value_1 = data[‘value’][‘value’]

response = requests.get('https://api.pipedrive.com/v1/customFields/{}'.format(custom_field_id_2))

if response.status_code == 200:
  data = response.json()
  value_2 = data['value']['value']

  if value_1 != value_2:
    requests.put('https://api.pipedrive.com/v1/customFields/{}'.format(custom_field_id_2), data={'value': {'value': value_1}})

return response

def check_if_copy_custom_field_value_was_successful():
response = copy_custom_field_value()

if response.status_code == 200:
print(‘The copy_custom_field_value() function was successful.’)
else:
print(‘The copy_custom_field_value() function was not successful.’)

if name == ‘main’:

Run the function immediately

copy_custom_field_value()

Check if the function was successful

check_if_copy_custom_field_value_was_successful()

Run the function every day at 11 PM

schedule.every().day.at(‘23:00’).do(copy_custom_field_value)

Keep the code running indefinitely

while True:
schedule.run_pending()
time.sleep(1)