Google Apps Script function that creates new non-existing entities in Pipedrive

Hi please help me with this Google Apps Script function that creates new entities in Pipedrive CRM based on data in a Google Sheet. The function iterates over rows in the Sheet, retrieves data for a company and a person, checks if the company exists in Pipedrive, creates the company if it does not exist, and then creates a new person associated with that company.

  1. Get the ID of the Google Sheet containing the data.
  2. Get the sheet named “Feuille 1” from the Google Sheet.
  3. Get the last row of data in the sheet.
  4. Loop through each row of data starting from row 2 (i.e., skipping the header row).
  5. Get the company name, person name, person email, and person phone number from the current row of the sheet.
  6. Check if the company already exists in Pipedrive by making a request to the Pipedrive API’s /organizations/find endpoint using the company name as the search term.
  7. If the company exists, retrieve its ID.
  8. If the company does not exist, create a new company in Pipedrive using the company name and retrieve its ID.
  9. Create a new person in Pipedrive using the person’s name, email, phone number, and the company ID.
  10. Log the results of each step for debugging purposes.

Everything works, except that I don’t know how to check if the person already exists, you don’t have to create it, if it exists, you get its ID.

Can you please help me, providing the full code which will also check if the person already exists, not create it. So this script will not create duplicates for company and person in pipedrive.

Please find bellow my code :

function createPipedriveEntity() {
var sheetId = ‘My sheet Id’; // Replace “My sheet Id” with the ID of your own spreadsheet
var sheet = SpreadsheetApp.openById(sheetId).getSheetByName(“Sheet name”); // Replace “Sheet name” with the name of your spreadsheet
var last_row = sheet.getLastRow();

for (var row = 2; row <= last_row; row++) { // Start from the second row
var company_name = sheet.getRange(‘AT’ + row).getValue();
var person_name = sheet.getRange(‘X’ + row).getValue();
var person_email = sheet.getRange(‘N’ + row).getValue();
var person_phone = sheet.getRange(‘FQ’ + row).getValue();

// Check if the company already exists in Pipedrive
var organizationExists = false;
var apiToken = "INSERT_YOUR_API_TOKEN_HERE";
var organizationsUrl = "https://api.pipedrive.com/v1/organizations/find?term=" + company_name + "&api_token=" + apiToken;
var organizationsResponse = UrlFetchApp.fetch(organizationsUrl);
var organizations = JSON.parse(organizationsResponse.getContentText()).data;
if (organizations.length > 0) {
  organizationExists = true;
  var company_id = organizations[0].id;
  Logger.log("Company found: " + company_name + " (ID: " + company_id + ")");
}

// If the company does not exist, create it in Pipedrive
if (!organizationExists) {
  var url = "https://api.pipedrive.com/v1/organizations?api_token=" + apiToken;
  var payload = {
    "name": company_name
  };
  var options = {
    "method": "post",
    "contentType": "application/json",
    "payload": JSON.stringify(payload)
  };
  var response = UrlFetchApp.fetch(url, options);
  var company = JSON.parse(response);
  var company_id = company.data.id;
  Logger.log("Company created: " + company_name + " (ID: " + company_id + ")");
}

// Add the person to the company
var url = "https://api.pipedrive.com/v1/persons?api_token=" + apiToken;
var payload = {
  "name": person_name,
  "email": person_email,
  "phone": person_phone,
  "org_id": company_id
};
var options = {
  "method": "post",
  "contentType": "application/json",
  "payload": JSON.stringify(payload)
};
var response = UrlFetchApp.fetch(url, options);
var person = JSON.parse(response);
var person_id = person.data.id;
Logger.log("Person added: " + person_name + " (ID: " + person_id + ")");

}
}

1 Like

Hi @Purchase_Service
Welcome to the community :wave:
As I understand, the crux of the issue seems to be duplicate detection for persons. Please correct me if I am wrong.

On a high level, you can make use of the search endpoints to detect similar entries and then merge them. Did you try using the following endpoints?

https://developers.pipedrive.com/docs/api/v1/Persons#searchPersons

https://developers.pipedrive.com/docs/api/v1/Persons#mergePersons

Hi @Hem,

No, the crux of the problem is not duplicate detection for people.
I want to automate the creation of contact and person from google sheet to pipedrive.

It works for companies in my code above. But I can’t check if the persons exist before importing them.

The goal is to automate the import of company and person, but I need to check if it exists before starting the import.

Indeed my script being executed every minute, if I do not check if the person or the company already exists in pipedrive, I import thousands of duplicates.

@Hem

Yes i tried but I couldn’t write the good code for it.

Can you integrate this to my code ?

thanks in advance.

This topic was automatically closed after 10 days. New replies are no longer allowed.