Skip to content

Scheduling infrastructure for absolutely everyone.

fix: Salesforce Stricter Website querying to prevent wrong account match during contact owner check (#20133)

Hariom Balhara3a181f1dac88
diff --git a/packages/app-store/salesforce/lib/CrmService.test.ts b/packages/app-store/salesforce/lib/CrmService.test.ts
index 560059bcefe..b33849d764a 100644
--- a/packages/app-store/salesforce/lib/CrmService.test.ts
+++ b/packages/app-store/salesforce/lib/CrmService.test.ts
@@ -511,4 +511,13 @@ describe("SalesforceCRMService", () => {
});
});
});
+
+ describe("getAllPossibleAccountWebsiteFromEmailDomain", () => {
+ it("should return all possible account websites from email domain", () => {
+ const result = service.getAllPossibleAccountWebsiteFromEmailDomain("example.com");
+ expect(result).toEqual(
+ "example.com, www.example.com, http://www.example.com, http://example.com, https://www.example.com, https://example.com"
+ );
+ });
+ });
});
diff --git a/packages/app-store/salesforce/lib/CrmService.ts b/packages/app-store/salesforce/lib/CrmService.ts
index db355bcc601..cfdcca0677b 100644
--- a/packages/app-store/salesforce/lib/CrmService.ts
+++ b/packages/app-store/salesforce/lib/CrmService.ts
@@ -883,6 +883,17 @@ export default class SalesforceCRMService implements CRM {
});
}
+ public getAllPossibleAccountWebsiteFromEmailDomain(emailDomain: string) {
+ return [
+ emailDomain,
+ `www.${emailDomain}`,
+ `http://www.${emailDomain}`,
+ `http://${emailDomain}`,
+ `https://www.${emailDomain}`,
+ `https://${emailDomain}`,
+ ].join(", ");
+ }
+
private async getAccountIdBasedOnEmailDomainOfContacts(email: string) {
const conn = await this.conn;
const emailDomain = email.split("@")[1];
@@ -890,13 +901,16 @@ export default class SalesforceCRMService implements CRM {
log.info("getAccountIdBasedOnEmailDomainOfContacts", safeStringify({ email, emailDomain }));
// First check if an account has the same website as the email domain of the attendee
const accountQuery = await conn.query(
- `SELECT Id, Website FROM Account WHERE Website IN ('${emailDomain}', 'www.${emailDomain}',
- 'http://www.${emailDomain}', 'http://${emailDomain}',
- 'https://www.${emailDomain}', 'https://${emailDomain}') LIMIT 1`
+ `SELECT Id, Website FROM Account WHERE Website IN (${this.getAllPossibleAccountWebsiteFromEmailDomain(
+ emailDomain
+ )}) LIMIT 1`
);
if (accountQuery.records.length > 0) {
- const account = accountQuery.records[0] as { Id: string };
- log.info("Found account based on email domain", safeStringify({ accountId: account.Id }));
+ const account = accountQuery.records[0] as { Id: string; Website: string };
+ log.info(
+ "Found account based on email domain",
+ safeStringify({ emailDomain, accountWebsite: account.Website, accountId: account.Id })
+ );
return account.Id;
}
@@ -923,13 +937,26 @@ export default class SalesforceCRMService implements CRM {
log.info("Querying first account matching email domain", safeStringify({ emailDomain }));
// First check if an account has the same website as the email domain of the attendee
const accountQuery = await conn.query(
- `SELECT Id, OwnerId, Owner.Email FROM Account WHERE Website LIKE '%${emailDomain}%' LIMIT 1`
+ `SELECT Id, OwnerId, Owner.Email FROM Account WHERE Website IN (${this.getAllPossibleAccountWebsiteFromEmailDomain(
+ emailDomain
+ )}) LIMIT 1`
);
if (accountQuery.records.length > 0) {
- log.info("Found account matching email domain", safeStringify({ emailDomain }));
+ const account = accountQuery.records[0] as {
+ Id?: string;
+ OwnerId?: string;
+ Owner?: { Email?: string };
+ Website?: string;
+ };
+
+ log.info(
+ "Found account matching email domain",
+ safeStringify({ emailDomain, accountWebsite: account.Website, accountId: account.Id })
+ );
+
return {
- ...(accountQuery.records[0] as { Id?: string; OwnerId?: string; Owner?: { Email?: string } }),
+ ...account,
Email: undefined,
};
}
diff --git a/packages/app-store/salesforce/lib/__tests__/CrmService.integration.test.ts b/packages/app-store/salesforce/lib/__tests__/CrmService.integration.test.ts
index 0e98fc451d5..48d59a93570 100644
--- a/packages/app-store/salesforce/lib/__tests__/CrmService.integration.test.ts
+++ b/packages/app-store/salesforce/lib/__tests__/CrmService.integration.test.ts
@@ -178,8 +178,8 @@ describe("SalesforceCRMService", () => {
it("(Lookup-3) should fallback to account having most number of contacts matched by emailDomain when Lookup-1 and Lookup-2 fails", async () => {
const crmService = new SalesforceCRMService(credential, appOptions);
- const account1OwnerEmail = "[email protected]";
- const account2OwnerEmail = "[email protected]";
+ const account1OwnerEmail = "[email protected]";
+ const account2OwnerEmail = "[email protected]";
const emailDomain = "example.com";
const lookingForEmail = `test@${emailDomain}`;
const account1 = salesforceMock.addAccount({
diff --git a/packages/app-store/salesforce/lib/__tests__/salesforceMock.ts b/packages/app-store/salesforce/lib/__tests__/salesforceMock.ts
index 63bcf63ce0a..f51ae250a7d 100644
--- a/packages/app-store/salesforce/lib/__tests__/salesforceMock.ts
+++ b/packages/app-store/salesforce/lib/__tests__/salesforceMock.ts
@@ -45,6 +45,7 @@ export const createSalesforceMock = () => {
// Query parser and responder
const handleQuery = (query: string) => {
// Simple SOQL parser
+ console.log({ query });
const fromMatch = query.match(/FROM\s+(\w+)/i);
const whereMatch = query.match(/WHERE\s+(.+?)(?:\s+LIMIT|\s+ORDER|\s*$)/i);
const limitMatch = query.match(/LIMIT\s+(\d+)/i);
@@ -77,6 +78,7 @@ export const createSalesforceMock = () => {
return { records: [] };
}
+ console.log({ whereClause });
// Apply where clause filtering (basic implementation)
if (whereClause) {
if (whereClause.includes("Email =")) {
@@ -134,6 +136,14 @@ export const createSalesforceMock = () => {
}
}
+ if (whereClause.includes("Website IN")) {
+ const websitesMatch = whereClause.match(/Website IN \((.+)\)/i);
+ if (websitesMatch) {
+ const websites = websitesMatch[1].split(",").map((w) => w.trim());
+ result = result.filter((r) => websites.includes(r.Website));
+ }
+ }
+
if (whereClause.includes("IsActive = true")) {
result = result.filter((r) => r.IsActive === true);
}