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 { createAction, Property } from '@activepieces/pieces-framework';
import { MarkdownVariant } from '@activepieces/pieces-framework';
import { getHubspotAccessToken, hubspotAuth } from '../auth';
import {
	customObjectDropdown,
	customObjectDynamicProperties,
	customObjectPropertiesDropdown,
} from '../common/props';

import { Client } from '@hubspot/api-client';
import { crmObjectOutputSchema } from '../output-schemas';

export const updateCustomObjectAction = createAction({
	auth: hubspotAuth,
	name: 'update-custome-object',
	classification: 'WRITE',
	displayName: 'Update Custom Object',
	description: 'Updates a custom object record in HubSpot.',
	audience: 'both',
	aiMetadata: { description: 'Updates properties on an existing custom-object record identified by its custom object type and record ID, then returns the refreshed record. Use to modify a known custom-object record; for standard CRM objects use the dedicated update actions instead. Idempotent: applying the same property values converges to the same record state.', idempotent: true },
	outputSchema: crmObjectOutputSchema,
	propertyGroups: [
		{
			key: 'target',
			display: 'section',
			label: 'Custom Object to Update',
			icon: 'tag',
			props: ['customObjectType', 'customObjectId'],
		},
		{
			key: 'changes',
			display: 'section',
			label: 'Changes',
			icon: 'text',
			props: ['changesInfo', 'objectProperties'],
		},
	],
	props: {
		customObjectType: customObjectDropdown,
		customObjectId: Property.ShortText({
			displayName: 'Custom Object ID',
			description: 'Map it from an earlier step like Find Custom Object.',
			required: true,
		}),
		changesInfo: Property.MarkDown({
			value: 'Fields left empty keep their current value.',
			variant: MarkdownVariant.INFO,
		}),
		objectProperties: customObjectDynamicProperties,
		markdown: Property.MarkDown({
			variant: MarkdownVariant.INFO,
			value: `Returned by default: hs_object_id, hs_lastmodifieddate, hs_createdate.

Pick more under **Advanced**.`,
		}),
		additionalPropertiesToRetrieve: customObjectPropertiesDropdown({
			displayName: 'Additional Properties to Retrieve',
			required: false,
			advanced: true,
		}),
	},
	async run(context) {
		const customObjectType = context.propsValue.customObjectType as string;
		const customObjectId = context.propsValue.customObjectId as string;
		const objectProperties = context.propsValue.objectProperties ?? {};
		const additionalPropertiesToRetrieve =
			context.propsValue.additionalPropertiesToRetrieve?.['values'];

		let propertiesToRetrieve;
		try {
			if (Array.isArray(additionalPropertiesToRetrieve)) {
				propertiesToRetrieve = additionalPropertiesToRetrieve;
			}
			if (typeof additionalPropertiesToRetrieve === 'string') {
				propertiesToRetrieve = JSON.parse(additionalPropertiesToRetrieve as string);
			}
		} catch (error) {
			propertiesToRetrieve = [];
		}

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

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

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

		const updatedCustomObject = await client.crm.objects.basicApi.update(
			customObjectType,
			customObjectId,
			{
				properties: customObjectProperties,
			},
		);

		const customObjectDetails = await client.crm.objects.basicApi.getById(
			customObjectType,
			updatedCustomObject.id,
			propertiesToRetrieve,
		);

		return customObjectDetails;
	},
});