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
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
import { createAction, Property } from '@activepieces/pieces-framework';
import { mycaseAuth } from '../auth';
import { createMyCaseApi } from '../common/mycase-api';
export const findOrCreateReferralSource = createAction({
auth: mycaseAuth,
name: 'find_or_create_referral_source',
displayName: 'Find or Create Referral Source',
description: 'Finds a referral source by name or creates a new one if it does not exist',
audience: 'both',
aiMetadata: { description: 'Resolve a MyCase referral source by exact (case-insensitive) name, creating it if no match exists, and return the source. Use to ensure a named referral source is present without risking duplicates. Idempotent: repeated calls with the same name return the existing source rather than creating another.', idempotent: true },
props: {
name: Property.ShortText({
displayName: 'Referral Source Name',
description: 'The name of the referral source to find or create',
required: true,
}),
},
async run(context) {
const api = createMyCaseApi(context.auth);
const referralSourceName = context.propsValue.name;
try {
// First, try to find the referral source
const findResponse = await api.get('/referral_sources', {
page_size: '1000'
});
if (findResponse.success && Array.isArray(findResponse.data)) {
const existingReferralSource = findResponse.data.find(
(rs: any) => rs.name && rs.name.toLowerCase() === referralSourceName.toLowerCase()
);
if (existingReferralSource) {
return {
success: true,
referral_source: existingReferralSource,
created: false,
message: `Referral source "${referralSourceName}" found`
};
}
}
// Referral source not found, create a new one
const requestBody = {
name: referralSourceName,
};
const createResponse = await api.post('/referral_sources', requestBody);
if (createResponse.success) {
return {
success: true,
referral_source: createResponse.data,
created: true,
message: `Referral source "${referralSourceName}" created successfully`
};
} else {
return {
success: false,
error: createResponse.error,
details: createResponse.details
};
}
} catch (error) {
return {
success: false,
error: 'Failed to find or create referral source',
details: error instanceof Error ? error.message : String(error)
};
}
}
});