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

3.7 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { folkAuth } from '../common/auth';
import { folkClient } from '../common/client';
import { folkProps } from '../common/props';

export const createCompany = createAction({
  auth: folkAuth,
  name: 'createCompany',
  classification: 'WRITE',
  displayName: 'Create Company',
  description: 'Create a new company in your Folk workspace. You can add emails, links, phone numbers, and assign it to a group.',
  audience: 'both',
  aiMetadata: {
    description: 'Creates a new company record in the Folk CRM, optionally attaching addresses, emails, phone numbers, URLs, an industry, and a group assignment. Use when a company does not yet exist in Folk; only the company name is required. Not idempotent: each call creates a separate company even with identical input, so guard against duplicates (e.g. search with Find Company first).',
    idempotent: false,
  },
  props: {
    name: Property.ShortText({
      displayName: 'Company Name',
      description: 'The name of the company',
      required: true,
    }),
    description: Property.LongText({
      displayName: 'Description',
      description: 'A short description of the company',
      required: false,
    }),
    industry: Property.ShortText({
      displayName: 'Industry',
      description: 'The industry the company operates in',
      required: false,
    }),
    addresses: Property.Array({
      displayName: 'Addresses',
      description: 'Physical addresses for the company. The first address is primary.',
      required: false,
      properties: {
        value: Property.ShortText({
          displayName: 'Address',
          required: true,
        }),
      },
    }),
    emails: Property.Array({
      displayName: 'Emails',
      description: 'Email addresses for the company. The first email is primary.',
      required: false,
      properties: {
        value: Property.ShortText({
          displayName: 'Email',
          required: true,
        }),
      },
    }),
    phones: Property.Array({
      displayName: 'Phone Numbers',
      description: 'Phone numbers for the company. The first phone is primary.',
      required: false,
      properties: {
        value: Property.ShortText({
          displayName: 'Phone Number',
          required: true,
        }),
      },
    }),
    urls: Property.Array({
      displayName: 'URLs',
      description: 'Website URLs for the company. The first URL is primary.',
      required: false,
      properties: {
        value: Property.ShortText({
          displayName: 'URL',
          required: true,
        }),
      },
    }),
    groupId: folkProps.group_id(false, 'Group ID'),
  },
  async run(context) {
    const { name, description, industry, addresses, emails, phones, urls, groupId } = context.propsValue;

    const companyData: any = {
      name,
    };

    if (description) companyData.description = description;
    if (industry) companyData.industry = industry;

    if (addresses && Array.isArray(addresses) && addresses.length > 0) {
      companyData.addresses = addresses.map((addr: any) => addr.value || addr);
    }

    if (emails && Array.isArray(emails) && emails.length > 0) {
      companyData.emails = emails.map((e: any) => e.value || e);
    }

    if (phones && Array.isArray(phones) && phones.length > 0) {
      companyData.phones = phones.map((p: any) => p.value || p);
    }

    if (urls && Array.isArray(urls) && urls.length > 0) {
      companyData.urls = urls.map((u: any) => u.value || u);
    }

    if (groupId) {
      companyData.groups = [{ id: groupId }];
    }

    const response = await folkClient.createCompany({
      apiKey: context.auth,
      data: companyData,
    });

    return {
      company: response.data,
      success: true,
    };
  },
});