Back to listing

Search HubSpot CRM

Category:
Function

ETL:
Extract, Transform

Notes

This is a very straightforward function for searching the CRM. 

The function searches a single parameter but can easily be expanded for more complicated matches. It then has three possible results: 

  1. A single record is found. In which case, you can call a function to act on that record. 
  2. Multiple records are found. In this event, you'll want to call a function that processes those records. 
  3. No records are found. Meaning that you may want to go back to the drawing board or call a `Create Record` function. 

Code

        

Language: JavaScript



 

let objectId;
const accessToken = `Bearer REPLACE_WITH_YOUR_PRIVATE_APP_KEY`;  // You'll want to use secrets to dynamically insert this token
 
// See if the record exists in HubSpot          
    function searchHubSpot(objectType, propertyName, searchTerm) {
        const data = {
            filterGroups: [
                {
                    filters: [
                        {
                            propertyName: propertyName,
                            operator: 'EQ',
                            value: searchTerm
                        }
                    ]
                }
            ]
        };
 
        const config = {
            headers: {
                'Content-Type': 'application/json',
                'Authorization': accessToken
            }
        };
 
        axios.post(`https://api.hubapi.com/crm/v3/objects/${objectType}/search`, data, config)
            .then(response => {
                const { total, results } = response.data;
                if (total === 1) {
                    console.log("Record exists");
                    objectId = results[0].properties.hs_object_id;
                    // Call processing function for single record
                } else if (total > 1 {
                    console.log("Multiple records exist");
                   // Call processing function for multiple records
                } else {
                    console.log("Record doesn't exist");
                    // Call processing function for no records
                }
            })
            .catch(error => {
                console.log("Error while checking if record exists:", error.message);
            });
    }
 

How can SpotDev support your business?

HubSpot Migrations

Move from Salesforce, Dynamics, Pipedrive or any CRM to HubSpot with SpotDev.

Learn more

HubSpot Integrations

Add advanced functionality to your HubSpot portal with API development services.

Learn more

RevOps

Align your sales, marketing and service teams to break down silos and trigger growth.

Learn more