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

1.9 KB
Raw Blame
import { Property, createAction } from '@activepieces/pieces-framework';
import { codaAuth } from '../auth';
import { codaClient } from '../common/types';
import { docIdDropdown, tableIdDropdown, tableRowsDynamicProps } from '../common/props';
import { isNil } from '@activepieces/pieces-framework';
import { updateRowActionOutputSchema } from '../output-schemas';

export const updateRowAction = createAction({
	auth: codaAuth,
	name: 'update-row',
	classification: 'WRITE',
	displayName: 'Update Row',
	description: 'Updates an existing row in the selected table.',
	audience: 'human',
	aiMetadata: { description: 'Overwrite cell values on an existing Coda table row, identified by its row ID or a unique row name. Use when you already know which row to change; use Upsert Row instead when the row may not yet exist. Idempotent — repeating with the same values leaves the row in the same state.', idempotent: true },
	props: {
		docId: docIdDropdown,
		tableId: tableIdDropdown,
		rowIdOrName: Property.ShortText({
			displayName: 'Row ID or Name',
			required: true,
		}),
		rowData: tableRowsDynamicProps,
	},
	outputSchema: updateRowActionOutputSchema,
	async run(context) {
		const { docId, tableId, rowIdOrName, rowData } = context.propsValue;
		const client = codaClient(context.auth);

		const cells = Object.entries(rowData ?? {})
			.filter(([, value]) => value !== undefined && value !== null && value !== '')
			.map(([columnId, value]) => ({
				column: columnId,
				value: value,
			}));

		if (cells.length === 0) {
			throw new Error('Provide at least one column value to update the row.');
		}

		const payload = {
			row: {
				cells: cells,
			},
		};

		const response = await client.updateRow(docId, tableId, rowIdOrName, payload, {
			disableParsing: false,
		});

		const rowId = response.id;

		if (isNil(rowId)) {
			throw new Error(`Unexpected error occured : ${JSON.stringify(response)}`);
		}

		return { rowId, requestId: response.requestId };
	},
});