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 { createAction, Property } from '@activepieces/pieces-framework';
import { clickfunnelsAuth } from '../common/constants';
import { multiTagsDropdown, teamsDropdown, workspacesDropdown } from '../common/props';
import { clickfunnelsApiService } from '../common/requests';

export const updateOrCreateContact = createAction({
  auth: clickfunnelsAuth,
  name: 'updateOrCreateContact',
  displayName: 'Update or Create Contact',
  description:
    'Searches for a contact by email and updates it, or creates a new one if not found.',
  audience: 'both',
  aiMetadata: {
    description:
      'Upserts a ClickFunnels contact keyed on email address: updates the matching contact if one exists, otherwise creates it. Use to add or sync a contact (name, phone, tags, custom attributes) without first checking for a duplicate. Idempotent — repeating with the same email and fields converges to the same contact rather than creating duplicates. Custom-attribute keys that collide with default contact fields will error.',
    idempotent: true,
  },
  props: {
    teamId: teamsDropdown(['auth']),
    workspaceId: workspacesDropdown(['auth', 'teamId']),
    emailAddress: Property.ShortText({
      displayName: 'Email',
      description:
        "The contact's email address. This is used to find and (update/create) the contact.",
      required: true,
    }),
    firstName: Property.ShortText({
      displayName: 'First Name',
      required: false,
    }),
    lastName: Property.ShortText({
      displayName: 'Last Name',
      required: false,
    }),
    phoneNumber: Property.ShortText({
      displayName: 'Phone Number',
      required: false,
    }),
    tagIds: multiTagsDropdown(['auth', 'workspaceId']),
    customAttributes: Property.Object({
      displayName: 'Custom Attributes',
      description: 'A key-value object for custom contact data. Keys that are default properties on the Contact resource or variations of it will result in an error. E.g., first_name, First Name, etc. are not valid inputs.',
      required: false,
    }),
  },
  async run({auth, propsValue}) {
    const payload = {
      contact: {
        email_address: propsValue.emailAddress,
        first_name: propsValue.firstName,
        last_name: propsValue.lastName,
        phone_number: propsValue.phoneNumber,
        tag_ids: propsValue.tagIds,
        custom_attributes: propsValue.customAttributes
      },
    };

    const response = await clickfunnelsApiService.upsertContact(auth.props, propsValue.workspaceId as string, payload);

    return response
  },
});