diff --git a/apps/web/public/static/locales/en/common.json b/apps/web/public/static/locales/en/common.json index 7be8c20963..db8b629f57 100644 --- a/apps/web/public/static/locales/en/common.json +++ b/apps/web/public/static/locales/en/common.json @@ -2689,6 +2689,9 @@ "reschedule_with_different_timeslot": "Reschedule with different timeslot", "we_dont_have_id_for_the_new_event": "We don't have ID for the new event. Please go to the Routing Form and save it again.", "rerouted_booking_successfully_redirecting_to_booking_page": "Rerouted booking successfully. Redirecting to booking page", + "on_booking_write_to_event_object": "On booking, write to event object", + "field_name": "Field name", + "add_new_field": "Add new field", "you_dont_have_access_to_reroute_this_booking": "You don't have access to reroute this booking", "form_response_not_found": "Form response not found", "ADD_NEW_STRINGS_ABOVE_THIS_LINE_TO_PREVENT_MERGE_CONFLICTS": "↑↑↑↑↑↑↑↑↑↑↑↑↑ Add your new strings above here ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑" diff --git a/packages/app-store/salesforce/components/EventTypeAppCardInterface.tsx b/packages/app-store/salesforce/components/EventTypeAppCardInterface.tsx index ed232e6626..a039260068 100644 --- a/packages/app-store/salesforce/components/EventTypeAppCardInterface.tsx +++ b/packages/app-store/salesforce/components/EventTypeAppCardInterface.tsx @@ -8,7 +8,7 @@ import type { EventTypeAppCardComponent } from "@calcom/app-store/types"; import { WEBAPP_URL } from "@calcom/lib/constants"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { SchedulingType } from "@calcom/prisma/enums"; -import { Switch, Alert, Select } from "@calcom/ui"; +import { Switch, Alert, Select, Button, InputField, showToast } from "@calcom/ui"; import { SalesforceRecordEnum } from "../lib/recordEnum"; import type { appDataSchema } from "../zod"; @@ -25,6 +25,8 @@ const EventTypeAppCard: EventTypeAppCardComponent = function EventTypeAppCard({ const createLeadIfAccountNull = getAppData("createLeadIfAccountNull"); const createNewContactUnderAccount = getAppData("createNewContactUnderAccount"); const createEventOn = getAppData("createEventOn") ?? SalesforceRecordEnum.CONTACT; + const onBookingWriteToEventObject = getAppData("onBookingWriteToEventObject") ?? false; + const onBookingWriteToEventObjectMap = getAppData("onBookingWriteToEventObjectMap") ?? {}; const { t } = useLocale(); const recordOptions = [ @@ -45,6 +47,11 @@ const EventTypeAppCard: EventTypeAppCardComponent = function EventTypeAppCard({ checkOwnerOptions.find((option) => option.value === roundRobinSkipCheckRecordOn) ?? checkOwnerOptions[0] ); + const [newOnBookingWriteToEventObjectField, setNewOnBookingWriteToEventObjectField] = useState({ + field: "", + value: "", + }); + return ( ) : null} +
+ { + setAppData("onBookingWriteToEventObject", checked); + }} + /> + {onBookingWriteToEventObject ? ( +
+
+
{t("field_name")}
+
{t("value")}
+
+
+ {...Object.keys(onBookingWriteToEventObjectMap).map((key) => ( +
+
+ +
+
+ +
+
+
+
+ ))} +
+
+ + setNewOnBookingWriteToEventObjectField({ + ...newOnBookingWriteToEventObjectField, + field: e.target.value, + }) + } + /> +
+
+ + setNewOnBookingWriteToEventObjectField({ + ...newOnBookingWriteToEventObjectField, + value: e.target.value, + }) + } + /> +
+
+
+ +
+ ) : null} +
+ {eventType.schedulingType === SchedulingType.ROUND_ROBIN ? (
{ + const appOptions = this.getAppOptions(); + + const customFieldInputsEnabled = + appOptions?.onBookingWriteToEventObject && appOptions?.onBookingWriteToEventObjectMap; + + const customFieldInputs = customFieldInputsEnabled + ? await this.ensureFieldsExistOnObject(Object.keys(appOptions?.onBookingWriteToEventObjectMap), "Event") + : []; + + const confirmedCustomFieldInputs: { + [key: string]: any; + } = {}; + + for (const field of customFieldInputs) { + confirmedCustomFieldInputs[field] = appOptions.onBookingWriteToEventObjectMap[field]; + } + const createdEvent = await this.salesforceCreateEventApiCall(event, { EventWhoIds: contacts.map((contact) => contact.id), + ...confirmedCustomFieldInputs, }).catch(async (reason) => { if (reason === sfApiErrors.INVALID_EVENTWHOIDS) { this.calWarnings.push( @@ -507,4 +525,29 @@ export default class SalesforceCRMService implements CRM { ...(recordType === SalesforceRecordEnum.LEAD && { Company: company }), }; } + + private async ensureFieldsExistOnObject(fieldsToTest: string[], sobject: string) { + const conn = await this.conn; + + const fieldSet = new Set(fieldsToTest); + const foundFields: string[] = []; + + try { + const salesforceEntity = await conn.describe(sobject); + const fields = salesforceEntity.fields; + + for (const field of fields) { + if (foundFields.length === fieldSet.size) break; + + if (fieldSet.has(field.name)) { + foundFields.push(field.name); + } + } + + return foundFields; + } catch (e) { + console.error(e); + return []; + } + } } diff --git a/packages/app-store/salesforce/zod.ts b/packages/app-store/salesforce/zod.ts index 274e3e820a..4fe37699e6 100644 --- a/packages/app-store/salesforce/zod.ts +++ b/packages/app-store/salesforce/zod.ts @@ -13,6 +13,8 @@ export const appDataSchema = eventTypeAppCardZod.extend({ createEventOn: z.nativeEnum(SalesforceRecordEnum).default(SalesforceRecordEnum.CONTACT).optional(), createNewContactUnderAccount: z.boolean().optional(), createLeadIfAccountNull: z.boolean().optional(), + onBookingWriteToEventObject: z.boolean().optional(), + onBookingWriteToEventObjectMap: z.record(z.any()).optional(), }); export const appKeysSchema = z.object({