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

4.0 KB
Raw Blame
import { biginAuth } from '../auth';
import { createAction, Property } from '@activepieces/pieces-framework';
import { tagsDropdown, usersDropdown } from '../common/props';
import { biginApiService } from '../common/request';

export const createCompany = createAction({
  auth: biginAuth,
  name: 'createCompany',
  classification: 'WRITE',
  displayName: 'Create Company',
  description: 'Creates a Company Record',
  audience: 'both',
  aiMetadata: { description: 'Creates a new company (account) record in Bigin CRM with a name plus optional phone, website, billing address, owner, tags, and description. Use to add an organization the business deals with. Not idempotent: each call creates a new company even if one with the same name already exists, so search first if you need to avoid duplicates.', idempotent: false },
  props: {
    accountName: Property.ShortText({
      displayName: 'Account Name',
      description: 'Provide the name of the company',
      required: true,
    }),
    phone: Property.ShortText({
      displayName: 'Phone',
      description: 'Provide a phone number for the company',
      required: false,
    }),
    website: Property.ShortText({
      displayName: 'Website',
      description: 'Provide a website URL for the company',
      required: false,
    }),
    tag: tagsDropdown('Accounts'),
    description: Property.LongText({
      displayName: 'Description',
      description:
        'Provide additional descriptions or notes related to the company',
      required: false,
    }),
    owner: usersDropdown,
    billingStreet: Property.ShortText({
      displayName: 'Billing Street',
      description: 'The street address of the company',
      required: false,
    }),
    billingCity: Property.ShortText({
      displayName: 'Billing City',
      description: 'The city where the company is located',
      required: false,
    }),
    billingState: Property.ShortText({
      displayName: 'Billing State',
      description: 'The state or province where the company is located',
      required: false,
    }),
    billingCountry: Property.ShortText({
      displayName: 'Billing Country',
      description: 'The country of the company',
      required: false,
    }),
    billingCode: Property.ShortText({
      displayName: 'Billing Code',
      description: 'The ZIP or postal code of the company',
      required: false,
    }),
  },
  async run(context) {
    try {
      const {
        accountName,
        owner,
        phone,
        website,
        tag,
        description,
        billingStreet,
        billingCity,
        billingState,
        billingCountry,
        billingCode,
      } = context.propsValue;

      const record: Record<string, any> = {
        Account_Name: accountName,
        Phone: phone,
        Website: website,
        Description: description,
        Billing_Street: billingStreet,
        Billing_City: billingCity,
        Billing_State: billingState,
        Billing_Country: billingCountry,
        Billing_Code: billingCode,
        Owner: owner ? { id: owner } : undefined,
        Tag: Array.isArray(tag) && tag.length > 0 ? tag.map((t) => ({ name: t })) : undefined,
      };

      Object.keys(record).forEach((k) => {
        const v = (record as any)[k];
        if (
          v === undefined ||
          v === null ||
          (typeof v === 'string' && v.trim() === '') ||
          (Array.isArray(v) && v.length === 0)
        ) {
          delete (record as any)[k];
        }
      });

      const response = await biginApiService.createCompany(
        context.auth.access_token,
        (context.auth as any).api_domain,
        { data: [record] }
      );

      return {
        message: 'Company created successfully',
        data: response.data[0],
      };
    } catch (error: any) {
      console.error('Error creating company:', error);
      throw new Error(
        error instanceof Error
          ? `Failed to create company: ${error.message}`
          : 'Failed to create company due to an unknown error'
      );
    }
  },
});