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

2.7 KB
Raw Blame
import {
    DedupeStrategy,
    HttpMethod,
    Polling,
    pollingHelper,
} from '@activepieces/pieces-common';
import {
    AppConnectionValueForAuthProperty,
    MarkdownVariant,
    Property,
    TriggerStrategy,
    createTrigger,
} from '@activepieces/pieces-framework';
import { querySalesforceApi } from '../common';

import dayjs from 'dayjs';
import { salesforceAuth } from '../..';
import { newUpdatedFileTriggerOutputSchema } from '../output-schemas';

export const newUpdatedFile = createTrigger({
    auth: salesforceAuth,
    name: 'new_updated_file',
    classification: 'READ',
    displayName: 'New or Updated File',
    description: 'Fires when a file is added or changed. Classic attachments are not included.',
    aiMetadata: {
        description: 'Fires whenever a Salesforce Files ContentDocument is created or modified, detected by polling for records whose SystemModstamp is later than the last poll. Each event represents the current state of one file and includes its fields plus a Type of ContentDocument. Does not fire for classic Attachments or Notes.',
    },
    outputSchema: newUpdatedFileTriggerOutputSchema,
    props: {
        scope_info: Property.MarkDown({
            value: 'Watches Salesforce Files. Classic attachments and notes are not included.',
            variant: MarkdownVariant.INFO,
        }),
    },
    sampleData: {
        "Id": "0697Q000002qB9iQAE",
        "Title": "My Document.pdf",
        "LastModifiedDate": "2025-10-10T12:00:00.000Z",
        "Type": "ContentDocument"
    },
    type: TriggerStrategy.POLLING,
    async test(ctx) {
        return await pollingHelper.test(polling, ctx);
    },
    async onEnable(ctx) {
        await pollingHelper.onEnable(polling, ctx);
    },
    async onDisable(ctx) {
        await pollingHelper.onDisable(polling, ctx);
    },
    async run(ctx) {
        return await pollingHelper.poll(polling, ctx);
    },
});

const polling: Polling<AppConnectionValueForAuthProperty<typeof salesforceAuth>, Record<string, unknown>> = {
    strategy: DedupeStrategy.TIMEBASED,
    items: async ({ auth, lastFetchEpochMS }) => {
        const isoDate = dayjs(lastFetchEpochMS).toISOString();

        const query = `
            SELECT FIELDS(ALL) 
            FROM ContentDocument 
            WHERE SystemModstamp > ${isoDate}
            ORDER BY SystemModstamp ASC
            LIMIT 200
        `;
        
        const response = await querySalesforceApi<{ records: { SystemModstamp: string }[] }>(HttpMethod.GET, auth, query);
        const records = response.body?.['records'] || [];

        return records.map((record: any) => ({
            epochMilliSeconds: dayjs(record.SystemModstamp).valueOf(),
            data: { ...record, Type: 'ContentDocument' },
        }));
    },
};