feat: Salesforce RR Skip Accounts - Match On Email Domain (#17350)

* If RR skip is set to account, match on email domain of existing contacts

* Revert changes on main merge

* Refactor
This commit is contained in:
Joe Au-Yeung
2024-10-28 13:54:32 -04:00
committed by GitHub
parent 2efa593d2b
commit e6dbb19eff
@@ -320,7 +320,15 @@ export default class SalesforceCRMService implements CRM {
const results = await conn.query(soql);
if (results.records.length) {
const contact = results.records[0] as { AccountId: string };
soql = `SELECT Id, OwnerId FROM Account WHERE Id = '${contact.AccountId}'`;
if (contact) {
soql = `SELECT Id, OwnerId FROM Account WHERE Id = '${contact.AccountId}'`;
}
} else {
// If we can't find the exact contact, then we need to search for an account where the contacts share the same email domain
const accountId = await this.getAccountIdBasedOnEmailDomainOfContacts(attendeeEmail);
if (accountId) {
soql = `SELECT Id, OwnerId FROM Account WHERE Id = '${accountId}'`;
}
}
}
// If creating events on contacts or leads
@@ -398,13 +406,7 @@ export default class SalesforceCRMService implements CRM {
// Base this off of the first contact
const attendee = contactsToCreate[0];
const emailDomain = attendee.email.split("@")[1];
const response = await conn.query(
`SELECT Id, Email, AccountId FROM Contact WHERE Email LIKE '%@${emailDomain}' AND AccountId != null`
);
const accountId = this.getDominantAccountId(response.records as { AccountId: string }[]);
const accountId = await this.getAccountIdBasedOnEmailDomainOfContacts(attendee.email);
let contactCreated = false;
@@ -570,4 +572,15 @@ export default class SalesforceCRMService implements CRM {
return [];
}
}
private async getAccountIdBasedOnEmailDomainOfContacts(email: string) {
const conn = await this.conn;
const emailDomain = email.split("@")[1];
const response = await conn.query(
`SELECT Id, Email, AccountId FROM Contact WHERE Email LIKE '%@${emailDomain}' AND AccountId != null`
);
return this.getDominantAccountId(response.records as { AccountId: string }[]);
}
}