Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Garconis/6876d8f3877394d7c988718a415eced3 to your computer and use it in GitHub Desktop.
Save Garconis/6876d8f3877394d7c988718a415eced3 to your computer and use it in GitHub Desktop.
Pipedrive API + Zapier | Find the most recent open Deal ID or unarchived Lead ID of a given contact person's ID
// Convert the personId from input data to a number (needed for comparison later)
// inputData.personId is a value passed into this step from a previous step in the Zap
const personId = parseInt(inputData.personId, 10);
// Check if personId is provided; if not, stop the process with an error
if (!personId) {
callback('Error: personId is required but not provided in inputData.');
return;
}
// Your Pipedrive API token, used to authenticate requests to Pipedrive
const pipedriveApiToken = 'xxxxxxx';
// Define the URL to fetch deals from Pipedrive
// This URL fetches all deals associated with the authenticated account
const dealsUrl = `https://api.pipedrive.com/v1/deals?api_token=${pipedriveApiToken}`;
// Define the URL to fetch leads from Pipedrive
// This URL filters leads to include only those associated with the personId
// and that are not archived (still active)
const leadsUrl = `https://api.pipedrive.com/v1/leads?api_token=${pipedriveApiToken}&archived_status=not_archived&person_id=${personId}&sort=add_time`;
// Step 1: Fetch deals and check if there are any open deals for this person
fetch(dealsUrl)
.then((response) => response.json()) // Convert the response to JSON format
.then((data) => {
if (data && data.data) {
// Filter the list of deals to find "open" deals for the given personId
const openDeals = data.data.filter(
(deal) => deal.person_id && deal.person_id.value === personId && deal.status === 'open'
);
if (openDeals.length > 0) {
// Sort the open deals by the time they were added (newest first)
const sortedDeals = openDeals.sort((a, b) => new Date(b.add_time) - new Date(a.add_time));
// Pick the most recent deal (the first item in the sorted list)
const mostRecentDeal = sortedDeals[0];
// Create a comma-separated list of all deal IDs found
const allDealIDs = openDeals.map(deal => deal.id).join(', ');
// Provide a message based on how many deals were found
const message = openDeals.length > 1 ?
'Multiple open deals found. Returning the most recent one.' :
'One open deal found.';
// Return information about the most recent deal
callback(null, {
message,
mostRecentDeal: {
id: mostRecentDeal.id,
title: mostRecentDeal.title,
status: mostRecentDeal.status,
value: mostRecentDeal.value,
currency: mostRecentDeal.currency,
add_time: mostRecentDeal.add_time
},
allDealIDs
});
return; // Exit since we found an open deal
}
}
// Step 2: If no open deals are found, proceed to fetch leads
return fetch(leadsUrl)
.then((response) => response.json()) // Convert the response to JSON format
.then((leadData) => {
// Extract the list of leads from the response
const rawLeadsData = leadData && leadData.data ? leadData.data : [];
if (rawLeadsData.length > 0) {
// Since the API already sorted the leads by add_time, take the first one
const mostRecentLead = rawLeadsData[0];
// Create a comma-separated list of all lead IDs found
const allLeadIDs = rawLeadsData.map(lead => lead.id).join(', ');
// Provide a message based on how many leads were found
const message = rawLeadsData.length > 1 ?
'No open deals found. Returning the most recent lead.' :
'No open deals found. One lead found.';
// Return information about the most recent lead
callback(null, {
message,
mostRecentLead: {
id: mostRecentLead.id,
title: mostRecentLead.title,
add_time: mostRecentLead.add_time
},
allLeadIDs
});
} else {
// If no leads are found, provide a message indicating no matches
callback(null, {
message: 'No open deals or leads found.',
mostRecentDeal: null,
mostRecentLead: null,
allDealIDs: '',
allLeadIDs: ''
});
}
});
})
.catch((error) => {
// If something goes wrong during the API calls, return an error message
callback(`Error: ${error.message}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment