fix: salesforce event creation duplicate error (#17292)

* fix: salesforce event creation duplicate error

* chore: message
This commit is contained in:
Udit Takkar
2024-10-24 17:11:12 +00:00
committed by GitHub
parent c29e37a03f
commit ca5b83f876
2 changed files with 36 additions and 19 deletions
+35 -18
View File
@@ -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;
}
+1 -1
View File
@@ -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) {