How to Create a Workflow to Query a Database and Create New Contacts in HubSpot for Each Record?

This topic was automatically generated from Slack. You can find the original thread here.

I need a workflow that can query a database and get a list of records, and, for each of those records, create a new contact in hubspot

To create a workflow that queries a database, retrieves a list of records, and creates a new contact in HubSpot for each record, you can follow these steps: 1. Trigger: Use an HTTP trigger, Scheduler trigger, or any other trigger that suits your use case. 2. Action: Query your database to get the list of records. 3. Action: Loop through each record and create a new contact in HubSpot. Here’s an example of how you can achieve this using Pipedream: ### Step 1: Set up the Trigger You can use an HTTP trigger or a Scheduler trigger to start the workflow. For example, if you want to run this workflow on a schedule, you can use the Scheduler trigger. ### Step 2: Query the Database Use a code step to query your database and retrieve the list of records. Here’s an example using a MySQL database: import mysql from 'mysql2/promise'; export default defineComponent({ async run({ steps, $ }) { const connection = await mysql.createConnection({ host: 'your-database-host', user: 'your-database-user', password: 'your-database-password', database: 'your-database-name', }); const [rows] = await connection.execute('SELECT * FROM your_table'); await connection.end(); return rows; }, }); ### Step 3: Create a New Contact in HubSpot Use the “Batch Create or Update Contact” action in HubSpot to create a new contact for each record. Here’s how you can set it up: 1. Search for the HubSpot app from the step menu. 2. Select the “Batch Create or Update Contact” action. 3. Connect your HubSpot account. 4. Pass the array of records from the previous step to this action. Here’s an example of how you can loop through the records and create a new contact in HubSpot: ``` import { axios } from “@pipedream/platform”; export default defineComponent({ props: { hubspot: { type: “app”, app: “hubspot”, }, }, async run({ steps, $ }) { const records = steps.query_database.$return_value; for (const record of records) { await axios($, { method: ‘POST’, url: 'https://api.hubapi.com/contacts/v

I need a workflow that can query a database and get a list of records, and, for each of those records, creates a new deal in hubspot

To create a workflow that queries a database, retrieves a list of records, and creates a new deal in HubSpot for each record, you can follow these steps: 1. Trigger: Use an appropriate trigger to start the workflow. This could be an HTTP trigger, a Scheduler trigger, or any other trigger that suits your use case. 2. Query Database: Add a code step to query your database and retrieve the list of records. 3. Create Deal in HubSpot: Use the “Create Deal” action from the HubSpot app to create a new deal for each record retrieved from the database. Here’s an example workflow: ### Step 1: Trigger You can use an HTTP trigger or a Scheduler trigger to start the workflow. ### Step 2: Query Database Add a code step to query your database. Replace the placeholder code with your actual database query logic. import { Client } from 'pg'; // Example for PostgreSQL. Use the appropriate client for your database. export default defineComponent({ async run({ steps, $ }) { const client = new Client({ user: 'your_database_user', host: 'your_database_host', database: 'your_database_name', password: 'your_database_password', port: 5432, // Replace with your database port }); await client.connect(); const res = await client.query('SELECT * FROM your_table_name'); // Replace with your query await client.end(); return res.rows; // Return the list of records }, }); ### Step 3: Create Deal in HubSpot Use the “Create Deal” action from the HubSpot app. You can loop through the records retrieved from the database and create a new deal for each record. ``` import { axios } from “@pipedream/platform”; export default defineComponent({ props: { hubspot: { type: “app”, app: “hubspot”, }, }, async run({ steps, $ }) { const records = steps.query_database; // Replace with the step name that queries the database for (const record of records) { await axios($, { method: “POST”, url: “https://api.hubapi.com/deals/v1/deal”, headers: { Authorization: Bearer ${this.hubspot.$auth.oauth_access_token}, },

When creating the deal in hubspot, do I run it as node code or do i actually use the hubspot action?

You can create a deal in HubSpot using either the pre-built HubSpot action in Pipedream or by writing custom Node.js code. Here are both methods: ### Using the HubSpot Action 1. Search for the HubSpot app from the step menu. 2. Select the Create Deal action. 3. Connect your HubSpot account. 4. Select one or more Property Groups or pass an array of strings from a previous step. 5. At the bottom of the step, click Test to run the action and test the configuration. For more details, you can refer to the Create Deal action documentation. ### Using Custom Node.js Code If you prefer to write custom code, you can use the following example to create a deal in HubSpot: ```
import { axios } from “@pipedream/platform”; export default defineComponent({ props: { hubspot: { type: “app”, app: “hubspot”, }, dealName: { type: “string”, label: “Deal Name”, }, amount: { type: “integer”, label: “Amount”, }, pipeline: { type: “string”, label: “Pipeline ID”, }, stage: { type: “string”, label: “Stage ID”, }, }, async run({ steps, $ }) { const data = { properties: [ { name: “dealname”, value: this.dealName }, { name: “amount”, value: this.amount }, { name: “pipeline”, value: this.pipeline }, { name: “dealstage”, value: this.stage }, ], }; return await axios($, { method: “POST”, url: https://api.hubapi.com/deals/v1/deal, headers: { Authorization: Bearer ${this.hubspot.$auth.oauth_access_token}, “Content-Type”: “application/json”, }, data, }); }, });

I need a workflow that can query a database and get a list of records, and, for each of those records, check to see if they are already a contact in hubspot, and if they are not, create a new contact in hubspot and create a new deal for them, and if they are already in hubspot as a contact, create a new deal only

To create a workflow that queries a database, checks for existing contacts in HubSpot, and creates new contacts and deals as needed, you can follow these steps: 1. Trigger: Set up a trigger to start the workflow. This could be an HTTP trigger, a scheduled trigger, or any other trigger based on your requirements. 2. Query Database: Use a code step to query your database and retrieve the list of records. 3. Check Contacts in HubSpot: For each record, check if the contact exists in HubSpot. 4. Create Contact and Deal: If the contact does not exist, create a new contact and then create a deal. If the contact exists, just create a deal. Here is an example workflow using Node.js code steps: ### Step 1: Trigger You can use an HTTP trigger or a scheduled trigger. For this example, let’s assume a scheduled trigger. ### Step 2: Query Database Use a code step to query your database. Replace the database query with your actual query logic. export default defineComponent({ async run({ steps, $ }) { // Replace with your database query logic const records = [ { email: "[test1@example.com](mailto:test1@example.com)", name: "Test User 1" }, { email: "[test2@example.com](mailto:test2@example.com)", name: "Test User 2" }, ]; return records; }, }); ### Step 3: Check Contacts in HubSpot and Create Contacts/Deals Use a code step to check if the contact exists in HubSpot and create contacts and deals as needed. ``` import { axios } from “@pipedream/platform”; export default defineComponent({ props: { hubspot: { type: “app”, app: “hubspot”, }, }, async run({ steps, $ }) { const records = steps.query_database; // Assuming the previous step is named “query_database” for (const record of records) { // Check if contact exists in HubSpot const contactExists = await axios($, { url: https://api.hubapi.com/contacts/v1/contact/email/${record.email}/profile, headers: { Authorization: Bearer ${this.hubspot.$auth.oauth_access_token}, }, validateStatus: (status) => status < 500, // Resolve only if the status code is less

I have a list of companies that I want to add to hubspot, and the number of companies in my query may change. With the current hubspot integration i can only create 1 company at a time. How can I create a workflow without writing custom code to loop through each of the new companies my query returns, and create a new company in hubspot?

You can achieve this by using Pipedream’s pre-built actions and a bit of workflow configuration. Here’s a step-by-step guide to create a workflow that loops through each company in your list and creates a new company in HubSpot without writing custom code: 1. Trigger the Workflow: Use an appropriate trigger to start your workflow. This could be an HTTP trigger, a schedule trigger, or any other trigger that fits your use case. 2. Fetch the List of Companies: Add a step to fetch the list of companies. This could be from an API, a database, or any other source. Ensure this step returns an array of companies. 3. Loop Through Companies: Use the “Loop” action to iterate over the list of companies. This action will allow you to run a set of steps for each item in the array. 4. Create Company in HubSpot: Within the loop, use the “Create Company” action from the HubSpot app to create a new company for each item in the list. ### Detailed Steps 1. Trigger the Workflow: - Choose a trigger that fits your use case (e.g., HTTP trigger, Scheduler, etc.). 2. Fetch the List of Companies: - Add a step to fetch the list of companies. For example, if you’re fetching from an API, you can use the “HTTP Request” action. 3. Loop Through Companies: - Add a “Loop” action to iterate over the list of companies. Configure the loop to use the array returned from the previous step. 4. Create Company in HubSpot: - Inside the loop, add the “Create Company” action from the HubSpot app. - Map the properties from the current item in the loop to the fields required by the HubSpot action. ### Example Workflow Configuration 1. Trigger: HTTP Trigger 2. Fetch Companies: HTTP Request to fetch companies 3. Loop: Loop through the array of companies 4. Create Company: HubSpot “Create Company” action inside the loop ### Using the HubSpot “Create Company” Action 1. Search for the HubSpot app from the step menu. 2. Select the Create Company action. 3. Connect your HubSpot account. 4. Select one or more Property Groups or pass an array of strings from a previous step. 5

I need a piece of code that, for each recordset returned, sends an individual webhook request with all of the data in json format to the webhook url endpoint “https://eodtl5sfuf5gz8r.m.pipedream.net

To achieve this, you can use a Pipedream workflow with a code step that iterates over each recordset and sends an individual webhook request for each record. Below is an example of how you can do this using Node.js: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { recordsets: { type: "object[]", label: "Recordsets", description: "Array of recordsets to send to the webhook", }, }, async run({ steps, $ }) { const webhookUrl = "https://eodtl5sfuf5gz8r.m.pipedream.net"; for (const recordset of this.recordsets) { await axios($, { method: "POST", url: webhookUrl, headers: { "Content-Type": "application/json", }, data: recordset, }); } return { success: true }; }, }); ### Explanation: 1. Imports: The axios constructor from the @pipedream/platform package is imported for making HTTP requests. 2. Props: The recordsets prop is defined to accept an array of recordsets. 3. Webhook URL: The webhook URL is defined as a constant. 4. Loop through Recordsets: The code iterates over each recordset and sends a POST request to the webhook URL with the recordset data in JSON format. 5. Return: The function returns a success message after all requests are sent. You can add this code as a step in your Pipedream workflow and configure the recordsets prop with the data you want to send. This will ensure that each recordset is sent as an individual webhook request to the specified endpoint. If you have any specific requirements or need further customization, please let me know!

I need a piece of code that, for each recordset returned in a previous step “steps.draft_jobs_today_date” takes the data from the recordsets in path “steps.draft_jobs_today_date.$return_value.recordsets” and sends an individual webhook request with all of the data in json format to the webhook url endpoint “https://eodtl5sfuf5gz8r.m.pipedream.net

To achieve this, you can iterate over each recordset returned in the previous step steps.draft_jobs_today_date and send an individual webhook request with all of the data in JSON format to the specified webhook URL endpoint. Below is the Pipedream component code to accomplish this: import { axios } from "@pipedream/platform"; export default defineComponent({ async run({ steps, $ }) { const recordsets = steps.draft_jobs_today_date.$return_value.recordsets; const webhookUrl = "https://eodtl5sfuf5gz8r.m.pipedream.net"; // Store each response and return them in this array const responses = []; for (const recordset of recordsets) { const resp = await axios($, { method: "POST", url: webhookUrl, data: recordset, }); responses.push(resp); } return responses; }, }); ### Explanation: 1. Import axios: Import the axios constructor from the @pipedream/platform package. 2. Define the component: Use defineComponent to define the Pipedream component. 3. Extract recordsets: Extract the recordsets from the previous step steps.draft_jobs_today_date.$return_value.recordsets. 4. Webhook URL: Define the webhook URL endpoint. 5. Iterate and send requests: Iterate over each recordset and send an HTTP POST request to the webhook URL with the recordset data. 6. Store responses: Store each response in an array and return the array of responses at the end. This code will send an individual webhook request for each recordset in the previous step’s output.

I need a piece of code that, using an email address from a webhook trigger “steps.trigger.event.body” and value “email_address” and check to see if they are already a contact in hubspot, and if they are not, create a new contact in hubspot, and if they are not, return the message “Contact already in Hubspot”

To achieve this, you can create a Pipedream component that checks if an email address from a webhook trigger exists in HubSpot. If the email address is not found, it will create a new contact; otherwise, it will return a message indicating that the contact already exists. Here’s the complete Pipedream component code: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { hubspot: { type: "app", app: "hubspot", }, }, async run({ steps, $ }) { const email = steps.trigger.event.body.email_address; // Check if the contact exists in HubSpot try { const response = await axios($, { url: `https://api.hubapi.com/contacts/v1/contact/email/${email}/profile`, headers: { Authorization: `Bearer ${this.hubspot.$auth.oauth_access_token}`, }, }); // If the contact exists, return a message return "Contact already in HubSpot"; } catch (error) { // If the contact does not exist, create a new contact if (error.response && error.response.status === 404) { const createResponse = await axios($, { method: "POST", url: `https://api.hubapi.com/contacts/v1/contact`, headers: { Authorization: `Bearer ${this.hubspot.$auth.oauth_access_token}`, }, data: { properties: [ { property: "email", value: email, }, ], }, }); return createResponse; } else { throw error; } } }, }); ### Explanation: 1. Props: The hubspot prop is defined to connect the HubSpot account. 2. Run Method: - Extract Email: The email address is extracted from the webhook trigger. - Check Contact: An HTTP GET request is made to check if the contact exists in HubSpot. - Handle Response: - If the contact exists, it returns “Contact already in HubSpot”. - If the contact does not exist (404 error), it proceeds to create a new contact with the provided email address. - Create Contact: An HTTP POST request is made to create a new contact in HubSpot

To accomplish your task, you need to create a Pipedream component that: 1. Takes an email address from the webhook trigger. 2. Checks if the email address is already a contact in HubSpot. 3. If the email address is not a contact, creates a new contact in HubSpot. 4. If the email address is already a contact, returns the message “Contact already in HubSpot”. Here is the code for the Pipedream component: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { hubspot: { type: "app", app: "hubspot", }, }, async run({ steps, $ }) { const email = steps.trigger.event.body.email_address; // Check if the email is already a contact in HubSpot try { const response = await axios($, { url: `https://api.hubapi.com/contacts/v1/contact/email/${email}/profile`, headers: { Authorization: `Bearer ${this.hubspot.$auth.oauth_access_token}`, }, }); // If the contact exists, return the message if (response) { return "Contact already in Hubspot"; } } catch (error) { // If the contact does not exist, create a new contact if (error.response && error.response.status === 404) { const createResponse = await axios($, { method: "POST", url: `https://api.hubapi.com/contacts/v1/contact`, headers: { Authorization: `Bearer ${this.hubspot.$auth.oauth_access_token}`, }, data: { properties: [ { property: "email", value: email, }, ], }, }); return createResponse; } else { throw error; } } }, }); ### Explanation: 1. Props: The hubspot prop allows the user to connect their HubSpot account. 2. Run Method: - Extracts the email address from the webhook trigger (steps.trigger.event.body.email_address). - Makes a GET request to HubSpot to check if the contact exists. - If the contact exists, it returns the message “Contact already in Hubspot”. - If the contact does not exist (404 error), it