Back to listing

Create HubSpot record

Category:
Function

ETL:
Load

Notes

A simple function to create a new record in HubSpot. 

It's ideal for use alongside the `Search CRM` code snippet. If your search returns no results, use this snippet to create a record. 

Code

        

Language: JavaScript



 
function createRecord(objectType, associationRecord, associationCategory, associationType) {
    const data = {
      properties: {
        email: 'bob@bobness.co.uk',
        first_name: 'Bob',
        last_name: 'Bobness'
      },
      associations: [
        {
          to: {
            id: associationRecord
          },
          types: [
            {
              associationCategory: associationCategory,
              associationTypeId: associationType
            }
          ]
        }
      ]
    };
    console.log("The data is: " + JSON.stringify(data));
    axios.post(`https://api.hubapi.com/crm/v3/objects/objectType`, data, {
      headers: {
        'Authorization': accessToken,
        'content-type': 'application/json'
      }
    })
      .then(response => {
      console.log("Success creating record");
    })
      .catch(error => {
      console.log(`Error creating record. The error message from HubSpot was ${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