chore: Add logging to Salesforce (#20167)

* Remove unused param

* Add log when no contacts are created

* Replace console.log with logger

* Add logging around `buildRecordUpdatePayload`

* Add details if field isn't being caught

* Revert change

* Type fixes

---------

Co-authored-by: Keith Williams <keithwillcode@gmail.com>
This commit is contained in:
Joe Au-Yeung
2025-03-18 19:51:02 +00:00
committed by GitHub
co-authored by Keith Williams
parent ff92979311
commit 40333c81c5
3 changed files with 33 additions and 6 deletions
@@ -609,17 +609,21 @@ export default class SalesforceCRMService implements CRM {
if (error.name === "DUPLICATES_DETECTED") {
const existingId = this.getExistingIdFromDuplicateError(error);
if (existingId) {
console.log("Using existing record:", existingId);
log.info("Using existing record:", existingId);
createdContacts.push({ id: existingId, email: attendee.email });
}
} else {
console.error("Error creating lead:", error);
log.error("Error creating lead:", error);
}
}
}
}
}
if (createdContacts.length === 0) {
// This should never happen
log.error(`No contacts created for these app options ${safeStringify(appOptions)}`);
}
return createdContacts;
}
@@ -816,6 +820,7 @@ export default class SalesforceCRMService implements CRM {
}
private async ensureFieldsExistOnObject(fieldsToTest: string[], sobject: string) {
const log = logger.getSubLogger({ prefix: [`[ensureFieldsExistOnObject]`] });
const conn = await this.conn;
const fieldSet = new Set(fieldsToTest);
@@ -835,7 +840,7 @@ export default class SalesforceCRMService implements CRM {
return foundFields;
} catch (e) {
console.error(e);
log.error(`Error ensuring fields ${fieldsToTest} exist on object ${sobject} with error ${e}`);
return [];
}
}
@@ -1039,6 +1044,7 @@ export default class SalesforceCRMService implements CRM {
bookingUid,
organizerEmail,
calEventResponses,
contactId,
});
this.log.info(
@@ -1065,6 +1071,7 @@ export default class SalesforceCRMService implements CRM {
bookingUid,
organizerEmail,
calEventResponses,
contactId,
}: {
existingFields: Field[];
personRecord: Record<string, any>;
@@ -1073,15 +1080,26 @@ export default class SalesforceCRMService implements CRM {
bookingUid?: string | null;
organizerEmail?: string;
calEventResponses?: CalEventResponses | null;
contactId: string;
}): Promise<Record<string, any>> {
const log = logger.getSubLogger({ prefix: [`[buildRecordUpdatePayload] ${contactId}`] });
const writeOnRecordBody: Record<string, any> = {};
for (const field of existingFields) {
const fieldConfig = onBookingWriteToRecordFields[field.name];
if (!fieldConfig) {
log.error(`No field config found for field ${field.name}`);
continue;
}
log.info(
`Processing field ${field.name} with type ${field.type} and config ${JSON.stringify(fieldConfig)}`
);
// Skip if field should only be written when empty and already has a value
if (fieldConfig.whenToWrite === WhenToWriteToRecord.FIELD_EMPTY && personRecord[field.name]) {
this.log.info(
log.info(
`Field ${field.name} on contactId ${personRecord?.Id} already exists with value ${
personRecord[field.name]
}`
@@ -1113,6 +1131,7 @@ export default class SalesforceCRMService implements CRM {
});
if (extractedText) {
writeOnRecordBody[field.name] = extractedText;
continue;
}
} else if (field.type === SalesforceFieldType.DATE && startTime && organizerEmail) {
const dateValue = await this.getDateFieldValue(
@@ -1123,9 +1142,16 @@ export default class SalesforceCRMService implements CRM {
);
if (dateValue) {
writeOnRecordBody[field.name] = dateValue;
continue;
}
}
}
log.error(
`No value found for field ${field.name} with value ${
personRecord[field.name]
}, field config ${JSON.stringify(fieldConfig)} and Salesforce config ${JSON.stringify(field)}`
);
}
return writeOnRecordBody;
@@ -1412,6 +1438,7 @@ export default class SalesforceCRMService implements CRM {
existingFields,
personRecord,
onBookingWriteToRecordFields: writeToRecordObject,
contactId: personRecord.Id,
});
await conn
.sobject(recordType)
+1 -1
View File
@@ -1046,7 +1046,7 @@ export default class EventManager {
const crm = new CrmManager(credential, currentAppOption);
let success = true;
const createdEvent = await crm.createEvent(event, currentAppOption).catch((error) => {
const createdEvent = await crm.createEvent(event).catch((error) => {
success = false;
// We don't know the type of the error here, so for an Error instance we can read message but otherwise we stringify the error
const errorMsg = error instanceof Error ? error.message : JSON.stringify(error);
+1 -1
View File
@@ -27,7 +27,7 @@ export default class CrmManager {
return crmService;
}
public async createEvent(event: CalendarEvent, appOptions?: any) {
public async createEvent(event: CalendarEvent) {
const crmService = await this.getCrmService(this.credential);
if (!crmService) return;
const { skipContactCreation = false, ignoreGuests = false } = crmService.getAppOptions() || {};