feat: Salesforce: Write booking question values to attendee record in Salesforce [CAL-4731] (#17650)

This commit is contained in:
Joe Au-Yeung
2024-11-19 10:30:34 -07:00
committed by GitHub
parent 7c5292e13b
commit 6cfe9ff88d
3 changed files with 43 additions and 4 deletions
@@ -54,6 +54,7 @@ const EventTypeAppCard: EventTypeAppCardComponent = function EventTypeAppCard({
const fieldTypeOptions = [
{ label: t("text"), value: SalesforceFieldType.TEXT },
{ label: t("date"), value: SalesforceFieldType.DATE },
{ label: t("phone").charAt(0).toUpperCase() + t("phone").slice(1), value: SalesforceFieldType.PHONE },
];
const [writeToPersonObjectFieldType, setWriteToPersonObjectFieldType] = useState(fieldTypeOptions[0]);
@@ -8,7 +8,7 @@ import { WEBAPP_URL } from "@calcom/lib/constants";
import { HttpError } from "@calcom/lib/http-error";
import logger from "@calcom/lib/logger";
import { prisma } from "@calcom/prisma";
import type { CalendarEvent } from "@calcom/types/Calendar";
import type { CalendarEvent, CalEventResponses } from "@calcom/types/Calendar";
import type { CredentialPayload } from "@calcom/types/Credential";
import type { CRM, Contact, CrmEvent } from "@calcom/types/CrmService";
@@ -217,7 +217,13 @@ export default class SalesforceCRMService implements CRM {
await this.checkRecordOwnerNameFromRecordId(contacts[0].id, ownerId);
}
if (appOptions.onBookingWriteToRecord && appOptions.onBookingWriteToRecordFields) {
await this.writeToPersonRecord(contacts[0].id, event.startTime, event.organizer.email, event?.uid);
await this.writeToPersonRecord(
contacts[0].id,
event.startTime,
event.organizer.email,
event.responses,
event?.uid
);
}
return createdEvent;
};
@@ -770,6 +776,7 @@ export default class SalesforceCRMService implements CRM {
contactId: string,
startTime: string,
organizerEmail: string,
calEventResponses?: CalEventResponses | null,
bookingUid?: string | null
) {
const conn = await this.conn;
@@ -792,6 +799,7 @@ export default class SalesforceCRMService implements CRM {
startTime,
bookingUid,
organizerEmail,
calEventResponses,
});
// Update the person record
@@ -808,6 +816,7 @@ export default class SalesforceCRMService implements CRM {
startTime,
bookingUid,
organizerEmail,
calEventResponses,
}: {
existingFields: jsforce.Field[];
personRecord: Record<string, any>;
@@ -815,6 +824,7 @@ export default class SalesforceCRMService implements CRM {
startTime: string;
bookingUid?: string | null;
organizerEmail: string;
calEventResponses?: CalEventResponses | null;
}): Promise<Record<string, any>> {
const writeOnRecordBody: Record<string, any> = {};
@@ -828,8 +838,12 @@ export default class SalesforceCRMService implements CRM {
// Handle different field types
if (fieldConfig.fieldType === field.type) {
if (field.type === SalesforceFieldType.TEXT) {
writeOnRecordBody[field.name] = fieldConfig.value.substring(0, field.length);
if (field.type === SalesforceFieldType.TEXT || field.type === SalesforceFieldType.PHONE) {
writeOnRecordBody[field.name] = this.getTextFieldValue({
fieldValue: fieldConfig.value,
fieldLength: field.length,
calEventResponses,
});
} else if (field.type === SalesforceFieldType.DATE) {
const dateValue = await this.getDateFieldValue(
fieldConfig.value,
@@ -847,6 +861,29 @@ export default class SalesforceCRMService implements CRM {
return writeOnRecordBody;
}
private getTextFieldValue({
fieldValue,
fieldLength,
calEventResponses,
}: {
fieldValue: string;
fieldLength: number;
calEventResponses?: CalEventResponses | null;
}) {
let valueToWrite = fieldValue.substring(0, fieldLength);
if (!calEventResponses) return valueToWrite;
// Check if we need to replace any values with values from the booking questions
const regexValueToReplace = /\{(.*?)\}/g;
valueToWrite = valueToWrite.replace(regexValueToReplace, (match, captured) => {
return calEventResponses[captured]?.value.toString() ?? match;
});
// Trim incase the replacement values increased the length
return valueToWrite.substring(0, fieldLength);
}
private async getDateFieldValue(
fieldValue: string,
startTime: string,
@@ -12,6 +12,7 @@ export enum WhenToWriteToRecord {
export enum SalesforceFieldType {
DATE = "date",
TEXT = "string",
PHONE = "phone",
}
export enum DateFieldTypeData {