Skip to content

Scheduling infrastructure for absolutely everyone.

fix: salesforce event creation duplicate error (#17292)

Udit Takkarca5b83f876c0
diff --git a/packages/app-store/salesforce/lib/CrmService.ts b/packages/app-store/salesforce/lib/CrmService.ts
index 96866a923bf..2b5a474529d 100644
--- a/packages/app-store/salesforce/lib/CrmService.ts
+++ b/packages/app-store/salesforce/lib/CrmService.ts
@@ -444,30 +444,47 @@ export default class SalesforceCRMService implements CRM {
return [{ id: contact.Id, email: contact.Email }];
}
- await Promise.all(
- contactsToCreate.map(async (attendee) => {
- return await conn
- .sobject(SalesforceRecordEnum.LEAD)
- .create(
- this.generateCreateRecordBody({
- attendee,
- recordType: SalesforceRecordEnum.LEAD,
- organizerId,
- })
- )
- .then((result) => {
- if (result.success) {
- createdContacts.push({ id: result.id, email: attendee.email });
- }
- });
- })
- );
+ for (const attendee of contactsToCreate) {
+ try {
+ const result = await conn.sobject(SalesforceRecordEnum.LEAD).create(
+ this.generateCreateRecordBody({
+ attendee,
+ recordType: SalesforceRecordEnum.LEAD,
+ organizerId,
+ })
+ );
+ if (result.success) {
+ createdContacts.push({ id: result.id, email: attendee.email });
+ }
+ } catch (error: any) {
+ if (error.name === "DUPLICATES_DETECTED") {
+ const existingId = this.getExistingIdFromDuplicateError(error);
+ if (existingId) {
+ console.log("Using existing record:", existingId);
+ createdContacts.push({ id: existingId, email: attendee.email });
+ }
+ } else {
+ console.error("Error creating lead:", error);
+ }
+ }
+ }
}
}
return createdContacts;
}
+ private getExistingIdFromDuplicateError(error: any): string | null {
+ if (error.duplicateResult && error.duplicateResult.matchResults) {
+ for (const matchResult of error.duplicateResult.matchResults) {
+ if (matchResult.matchRecords && matchResult.matchRecords.length > 0) {
+ return matchResult.matchRecords[0].record.Id;
+ }
+ }
+ }
+ return null;
+ }
+
private setDoNotCreateEvent(boolean: boolean) {
this.doNotCreateEvent = boolean;
}
diff --git a/packages/core/EventManager.ts b/packages/core/EventManager.ts
index c8e6ed3f47c..ab1da403fa0 100644
--- a/packages/core/EventManager.ts
+++ b/packages/core/EventManager.ts
@@ -976,7 +976,7 @@ export default class EventManager {
let success = true;
const createdEvent = await crm.createEvent(event, currentAppOption).catch((error) => {
success = false;
- log.warn(`Error creating crm event for ${credential.type}`, error);
+ log.warn(`Error creating crm event for ${credential.type}`, JSON.stringify(error));
});
if (createdEvent) {