Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Salesforce - optimize getContact method [CAL-4940] #18289

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

joeauyeung
Copy link
Contributor

@joeauyeung joeauyeung commented Dec 20, 2024

What does this PR do?

  • Aims to reduce the number of calls we're making to Salesforce by nesting queries
    • Removes the need to make a specific query for the record owner
    • Optimizes queries involving contacts and accounts
  • Removes a DB query when checking if the record owner is a host in the event type

We're seeing an improvement of 2 - 2.4 seconds to 1.2 - 1.4 seconds

  • Fixes #XXXX (GitHub issue number)
  • Fixes CAL-XXXX (Linear issue number - should be visible at the bottom of the GitHub issue description)

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • I have updated the developer docs in /docs if this PR makes changes that would require a documentation change. If N/A, write N/A here and check the checkbox.
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

How should this be tested?

  • Are there environment variables that should be set?
  • What are the minimal test data to have?
  • What is expected (happy path) to have (input and output)?
  • Any other important info that could help to test that PR

Checklist

  • I haven't read the contributing guide
  • My code doesn't follow the style guidelines of this project
  • I haven't commented my code, particularly in hard-to-understand areas
  • I haven't checked if my changes generate no new warnings

Copy link

vercel bot commented Dec 20, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
cal ⬜️ Ignored (Inspect) Visit Preview Dec 21, 2024 6:28pm
calcom-web-canary ⬜️ Ignored (Inspect) Visit Preview Dec 21, 2024 6:28pm

Comment on lines +22 to +26
hosts: {
user: {
email: string;
};
}[];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include hosts emails in the initial query here so we don't need to make a second DB query when checking if the CRM record owner is a host.

@@ -92,6 +92,7 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
select: {
name: true,
username: true,
email: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include email in the hosts query

}
}
// If creating events on contacts or leads
soql = `SELECT Id, Email, OwnerId, AccountId, Account.Owner.Email, Account.Website FROM ${SalesforceRecordEnum.CONTACT} WHERE Email = '${attendeeEmail}' AND AccountId != null`;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make use of SOQL nested queries to reduce the amount of calls we're making to Salesforce

if (!records.length) {
log.info("No records found");
return [];
}

// Handle owner information
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huge win is removing the need to make a separate query for the owner information


let records: ContactRecord[] = [];

// If falling back to contacts, check for the contact before returning the leads or empty array
if (
appOptions.createEventOn === SalesforceRecordEnum.LEAD &&
appOptions.createEventOnLeadCheckForContact
appOptions.createEventOnLeadCheckForContact &&
!forRoundRobinSkip
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only need this logic for event creation.

@@ -915,6 +826,51 @@ export default class SalesforceCRMService implements CRM {
return this.getDominantAccountId(response.records as { AccountId: string }[]);
}

private async getAccountBasedOnEmailDomainOfContacts(email: string) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to getAccountIdBasedOnEmailDomainOfContacts but this returns the whole account object. I would like to migrate to this method but that'll involve refactoring in other places.

@joeauyeung joeauyeung marked this pull request as ready for review December 20, 2024 07:48
@graphite-app graphite-app bot requested a review from a team December 20, 2024 07:48
@dosubot dosubot bot added crm-apps area: crm apps, salesforce, hubspot, close.com, sendgrid performance area: performance, page load, slow, slow endpoints, loading screen, unresponsive labels Dec 20, 2024
Copy link

graphite-app bot commented Dec 20, 2024

Graphite Automations

"Add consumer team as reviewer" took an action on this PR • (12/20/24)

1 reviewer was added to this PR based on Keith Williams's automation.

Copy link
Contributor

github-actions bot commented Dec 20, 2024

E2E results are ready!

@joeauyeung joeauyeung marked this pull request as ready for review December 20, 2024 15:50
@joeauyeung joeauyeung changed the title chore: Salesforce - optimize getContact method chore: Salesforce - optimize getContact method [CAL-4940] Dec 20, 2024
Copy link

linear bot commented Dec 20, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
consumer core area: core, team members only crm-apps area: crm apps, salesforce, hubspot, close.com, sendgrid performance area: performance, page load, slow, slow endpoints, loading screen, unresponsive ready-for-e2e
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants