feat: Salesforce - on booking, write to custom fields [CAL-4570] (#17257)

* Save and enable custom fields

* Write to custom fields on booking

* Add validations
This commit is contained in:
Joe Au-Yeung authored and GitHub committed 2024-10-24 03:07:13 +00:00
1 parent d6dbbd1472
commit 817d8738a9
4 files changed
+148 -1

No files matched your search

@@ -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 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑"
@@ -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 (
<AppCard
returnTo={`${WEBAPP_URL}${pathname}?tabName=apps`}
@@ -109,6 +116,98 @@ const EventTypeAppCard: EventTypeAppCardComponent = function EventTypeAppCard({
</>
) : null}
<div className="mt-4">
<Switch
label={t("on_booking_write_to_event_object")}
labelOnLeading
checked={onBookingWriteToEventObject}
onCheckedChange={(checked) => {
setAppData("onBookingWriteToEventObject", checked);
}}
/>
{onBookingWriteToEventObject ? (
<div className="ml-2 mt-2">
<div className="grid grid-cols-3 gap-4">
<div>{t("field_name")}</div>
<div>{t("value")}</div>
</div>
<div>
{...Object.keys(onBookingWriteToEventObjectMap).map((key) => (
<div className="mt-2 grid grid-cols-3 gap-4" key={key}>
<div>
<InputField value={key} readOnly />
</div>
<div>
<InputField value={onBookingWriteToEventObjectMap[key]} readOnly />
</div>
<div>
<Button
StartIcon="trash"
variant="icon"
color="destructive"
onClick={() => {
const newObject = onBookingWriteToEventObjectMap;
delete onBookingWriteToEventObjectMap[key];
setAppData("onBookingWriteToEventObjectMap", newObject);
}}
/>
</div>
</div>
))}
<div className="mt-2 grid grid-cols-3 gap-4">
<div>
<InputField
value={newOnBookingWriteToEventObjectField.field}
onChange={(e) =>
setNewOnBookingWriteToEventObjectField({
...newOnBookingWriteToEventObjectField,
field: e.target.value,
})
}
/>
</div>
<div>
<InputField
value={newOnBookingWriteToEventObjectField.value}
onChange={(e) =>
setNewOnBookingWriteToEventObjectField({
...newOnBookingWriteToEventObjectField,
value: e.target.value,
})
}
/>
</div>
</div>
</div>
<Button
className="mt-2"
size="sm"
disabled={
!(newOnBookingWriteToEventObjectField.field && newOnBookingWriteToEventObjectField.value)
}
onClick={() => {
if (
Object.keys(onBookingWriteToEventObjectMap).includes(
newOnBookingWriteToEventObjectField.field.trim()
)
) {
showToast("Field already exists", "error");
return;
}
setAppData("onBookingWriteToEventObjectMap", {
...onBookingWriteToEventObjectMap,
[newOnBookingWriteToEventObjectField.field.trim()]:
newOnBookingWriteToEventObjectField.value.trim(),
});
setNewOnBookingWriteToEventObjectField({ field: "", value: "" });
}}>
{t("add_new_field")}
</Button>
</div>
) : null}
</div>
{eventType.schedulingType === SchedulingType.ROUND_ROBIN ? (
<div className="mt-4">
<Switch
@@ -171,8 +171,26 @@ export default class SalesforceCRMService implements CRM {
};
private salesforceCreateEvent = async (event: CalendarEvent, contacts: Contact[]) => {
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 [];
}
}
}
+2
View File
@@ -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({