Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.6 KB
Raw Blame
import { getHubspotAccessToken, hubspotAuth } from '../auth';
import { createAction, Property } from '@activepieces/pieces-framework';
import {

	getDefaultPropertiesForObject,
	standardObjectDynamicProperties,
	standardObjectPropertiesDropdown,
} from '../common/props';
import { OBJECT_TYPE } from '../common/constants';
import { MarkdownVariant } from '@activepieces/pieces-framework';
import { Client } from '@hubspot/api-client';
import { crmObjectOutputSchema } from '../output-schemas';

export const updateCompanyAction = createAction({
	auth: hubspotAuth,
	name: 'update-company',
	classification: 'WRITE',
	displayName: 'Update Company',
	description: 'Updates a company in HubSpot.',
	audience: 'both',
	aiMetadata: { description: 'Update properties on an existing HubSpot company identified by Company ID; only the supplied fields are changed. Applying the same field values repeatedly leaves the record in the same state, so it is idempotent. Use Create Company to add a new record, or a find action to obtain the ID first.', idempotent: true },
	outputSchema: crmObjectOutputSchema,
	propertyGroups: [
		{
			key: 'target',
			display: 'section',
			label: 'Company to Update',
			icon: 'tag',
			props: ['companyId'],
		},
		{
			key: 'changes',
			display: 'section',
			label: 'Changes',
			icon: 'text',
			props: ['changesInfo', 'objectProperties'],
		},
	],
	props: {
		companyId: Property.ShortText({
			displayName: 'Company ID',
			description: 'Map it from an earlier step like Find Company.',
			required: true,
		}),
		changesInfo: Property.MarkDown({
			value: 'Fields left empty keep their current value.',
			variant: MarkdownVariant.INFO,
		}),
		objectProperties: standardObjectDynamicProperties(OBJECT_TYPE.COMPANY, []),
		markdown: Property.MarkDown({
			variant: MarkdownVariant.INFO,
			value: `Returned by default: name, domain, industry, about_us, phone, address, address2, city, state, zip, country, website, type, description, founded_year, hs_createdate, hs_lastmodifieddate, hs_object_id, is_public, timezone, total_money_raised, total_revenue, owneremail, ownername, numberofemployees, annualrevenue, lifecyclestage, createdate, web_technologies.

Pick more under **Advanced**.`,
		}),
		additionalPropertiesToRetrieve: standardObjectPropertiesDropdown({
			objectType: OBJECT_TYPE.COMPANY,
			displayName: 'Additional Properties to Retrieve',
			required: false,
			advanced: true,
		}),
	},
	async run(context) {
		const companyId = context.propsValue.companyId;
		const objectProperties = context.propsValue.objectProperties ?? {};
		const additionalPropertiesToRetrieve = context.propsValue.additionalPropertiesToRetrieve ?? [];

		const companyProperties: Record<string, string> = {};

		// Add additional properties to the companyProperties object
		Object.entries(objectProperties).forEach(([key, value]) => {
			if ((Array.isArray(value) && value.length === 0)) {
        return;  
			}
			// Format values if they are arrays
			companyProperties[key] = Array.isArray(value) ? value.join(';') : value;
		});

		const client = new Client({ accessToken: getHubspotAccessToken(context.auth) });

		const updatedCompany = await client.crm.companies.basicApi.update(companyId, {
			properties: companyProperties,
		});
		// Retrieve default properties for the comapny and merge with additional properties to retrieve
		const defaultcompanyProperties = getDefaultPropertiesForObject(OBJECT_TYPE.COMPANY);

		const companyDetails = await client.crm.companies.basicApi.getById(updatedCompany.id, [
			...defaultcompanyProperties,
			...additionalPropertiesToRetrieve,
		]);

		return companyDetails;
	},
});