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
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
import { getHubspotAccessToken, hubspotAuth } from '../auth';
import {
createTrigger,
DynamicPropsValue,
TriggerStrategy,
} from '@activepieces/pieces-framework';
import {
customObjectDropdown,
customObjectPropertiesDropdown,
} from '../common/props';
import { DedupeStrategy, Polling, pollingHelper } from '@activepieces/pieces-common';
import { isNil } from '@activepieces/pieces-framework';
import { chunk } from '@activepieces/pieces-framework';
import { Client } from '@hubspot/api-client';
import dayjs from 'dayjs';
import { FilterOperatorEnum } from '../common/types';
import { MAX_SEARCH_PAGE_SIZE, MAX_SEARCH_TOTAL_RESULTS } from '../common/constants';
import { crmObjectOutputSchema } from '../output-schemas';
import { AppConnectionValueForAuthProperty } from '@activepieces/pieces-framework';
type Props = {
customObjectType?: string;
propertyName?: DynamicPropsValue;
};
const polling: Polling<AppConnectionValueForAuthProperty<typeof hubspotAuth>, Props> = {
strategy: DedupeStrategy.TIMEBASED,
async items({ auth, propsValue, lastFetchEpochMS }) {
const client = new Client({ accessToken: getHubspotAccessToken(auth), numberOfApiCallRetries: 3 });
const customObjectType = propsValue.customObjectType as string;
const propertyToCheck = propsValue.propertyName?.['values'] as string;
const propertiesToRetrieve = [propertyToCheck];
const items = [];
// For test, we only fetch 10 custom objects
if (lastFetchEpochMS === 0) {
const response = await client.crm.objects.searchApi.doSearch(customObjectType, {
limit: 10,
properties: propertiesToRetrieve,
sorts: ['-hs_lastmodifieddate'],
});
items.push(...response.results);
return items.map((item) => ({
epochMilliSeconds: dayjs(item.properties['hs_lastmodifieddate']).valueOf(),
data: item,
}));
}
//fetch updated custom objects
const updatedCustomObjects = [];
let after: string | undefined;
do {
const response = await client.crm.objects.searchApi.doSearch(customObjectType, {
limit: MAX_SEARCH_PAGE_SIZE,
after,
sorts: ['-hs_lastmodifieddate'],
filterGroups: [
{
filters: [
{
propertyName: propertyToCheck,
operator: FilterOperatorEnum.HasProperty,
},
{
propertyName: 'hs_lastmodifieddate',
operator: FilterOperatorEnum.Gt,
value: lastFetchEpochMS.toString(),
},
],
},
],
});
after = response.paging?.next?.after;
updatedCustomObjects.push(...response.results);
// Stop fetching if it exceeds max search results or will encounter 400 status
if (
!isNil(after) &&
parseInt(after) + MAX_SEARCH_PAGE_SIZE > MAX_SEARCH_TOTAL_RESULTS
) {
break;
}
} while (after);
if (updatedCustomObjects.length === 0) {
return [];
}
// Avoid VALIDATION_ERROR: The maximum number of inputs supported in a batch request for property histories is 50
const batchApiChunks = chunk(updatedCustomObjects, 50);
// Fetch custom objects with property history
const batchApiResps = await Promise.all(
batchApiChunks.map((batch) => {
return client.crm.objects.batchApi.read(customObjectType, {
propertiesWithHistory: [propertyToCheck],
properties: propertiesToRetrieve,
inputs: batch.map((customObject) => {
return {
id: customObject.id,
};
}),
});
})
);
const updatedCustomObjectsWithPropertyHistory = batchApiResps.flatMap(
(resp) => resp.results
);
for (const customObject of updatedCustomObjectsWithPropertyHistory) {
const history = customObject.propertiesWithHistory?.[propertyToCheck];
if (!history || history.length === 0) {
continue;
}
const propertyLastModifiedDateTimeStamp = dayjs(history[0].timestamp).valueOf();
if (propertyLastModifiedDateTimeStamp > lastFetchEpochMS) {
const { propertiesWithHistory, ...item } = customObject;
items.push(item);
}
}
return items.map((item) => ({
epochMilliSeconds: dayjs(item.properties['hs_lastmodifieddate']).valueOf(),
data: item,
}));
},
};
export const newCustomObjectPropertyChangeTrigger = createTrigger({
auth: hubspotAuth,
name: 'new-custom-object-property-change',
classification: 'READ',
displayName: 'New Custom Object Property Change',
description: 'Triggers when a specified property is updated on a custom object.',
aiMetadata: {
description:
'Fires when the chosen property changes on a record of the selected HubSpot custom object type. Each event represents one custom-object record whose selected property was modified since the last poll, returning the record. Use to react to specific field updates on a custom object rather than every change.',
},
propertyGroups: [
{
key: 'watch',
display: 'section',
label: 'Property to Watch',
icon: 'filter',
props: ['customObjectType', 'propertyName'],
},
],
props: {
customObjectType: customObjectDropdown,
propertyName: customObjectPropertiesDropdown({
displayName: 'Property',
required: true,
isSingleSelect: true,
}),
},
outputSchema: crmObjectOutputSchema,
type: TriggerStrategy.POLLING,
async onEnable(context) {
await pollingHelper.onEnable(polling, context);
},
async onDisable(context) {
await pollingHelper.onDisable(polling, context);
},
async test(context) {
return await pollingHelper.test(polling, context);
},
async run(context) {
return await pollingHelper.poll(polling, context);
},
sampleData: {
createdAt: '2024-12-22T15:20:16.121Z',
archived: false,
id: '21583829313',
properties: {
hs_createdate: '2024-12-22T15:20:16.121Z',
hs_lastmodifieddate: '2024-12-22T15:20:16.818Z',
hs_object_id: '21583829313',
pet_name: 'Oreo',
},
updatedAt: '2024-12-22T15:20:16.818Z',
},
});