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.5 KB
Raw Blame
import { intercomAuth } from '../auth';
import { createAction, Property } from '@activepieces/pieces-framework';
import { intercomClient } from '../common';
import { findOrCreateLeadOutputSchema } from '../output-schemas';

export const findOrCreateLeadAction = createAction({
	auth: intercomAuth,
	name: 'find-or-create-lead',
	classification: 'WRITE',
	displayName: 'Find or Create Lead',
	description: 'Finds a lead by email, ID, or User ID, creating it if none is found.',
	audience: 'both',
	aiMetadata: {
		description:
			'Resolve a lead (role=lead prospect) to a single record: searches by email, Intercom ID, or external User ID and returns the match, or creates a new lead when none exists. Returns the lead plus a "created" flag. For signed-up users use Get Contact or Create User instead.',
		idempotent: true,
	},
	props: {
		searchField: Property.StaticDropdown({
			displayName: 'Search Field',
			required: true,
			defaultValue: 'email',
			options: {
				disabled: false,
				options: [
					{ label: 'Email', value: 'email' },
					{ label: 'ID', value: 'id' },
					{ label: 'User ID', value: 'external_id' },
				],
			},
		}),
		searchValue: Property.ShortText({
			displayName: 'Search Value',
			required: true,
		}),
		name: Property.ShortText({
			displayName: 'Full Name',
			required: false,
		}),
		phone: Property.ShortText({
			displayName: 'Phone',
			required: false,
		}),
		customAttributes: Property.Object({
			displayName: 'Custom Attributes',
			required: false,
		}),
	},
	outputSchema: findOrCreateLeadOutputSchema,
	async run(context) {
		const { searchField, searchValue, name, phone, customAttributes } = context.propsValue;

		const client = await intercomClient(context.auth);

		const existing = await client.contacts.search({
			query: {
				operator: 'AND',
				value: [
					{ field: searchField, operator: '=', value: searchValue },
					{ field: 'role', operator: '=', value: 'lead' },
				],
			},
			pagination: { per_page: 1 },
		});

		if (existing.data.length > 0) {
			return { created: false, lead: existing.data[0] };
		}

		if (searchField === 'id') {
			throw new Error(
				'No lead found with that Intercom ID. Intercom assigns IDs itself, so a lead can only be created when searching by Email or User ID.',
			);
		}

		const lead = await client.contacts.create({
			role: 'lead',
			email: searchField === 'email' ? searchValue : undefined,
			external_id: searchField === 'external_id' ? searchValue : undefined,
			name,
			phone,
			custom_attributes: customAttributes,
		});

		return { created: true, lead };
	},
});