From 3589767099d131e964ae026d179126a203e13fb9 Mon Sep 17 00:00:00 2001 From: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Date: Thu, 14 Nov 2024 10:29:21 -0500 Subject: [PATCH] feat: Salesforce when create contacts under an account if creating leads is chosen [CAL-4713] (#17630) --- .../components/EventTypeAppCardInterface.tsx | 26 ++- .../app-store/salesforce/lib/CrmService.ts | 160 +++++++++++++----- 2 files changed, 134 insertions(+), 52 deletions(-) diff --git a/packages/app-store/salesforce/components/EventTypeAppCardInterface.tsx b/packages/app-store/salesforce/components/EventTypeAppCardInterface.tsx index 684fce1c5a..7170bee195 100644 --- a/packages/app-store/salesforce/components/EventTypeAppCardInterface.tsx +++ b/packages/app-store/salesforce/components/EventTypeAppCardInterface.tsx @@ -93,6 +93,20 @@ const EventTypeAppCard: EventTypeAppCardComponent = function EventTypeAppCard({ value: "", }); + // Used when creating events under leads or contacts under account + const CreateContactUnderAccount = () => { + return ( + { + setAppData("createNewContactUnderAccount", checked); + }} + /> + ); + }; + return ( +
+ +
) : null} {createEventOnSelectedOption.value === SalesforceRecordEnum.ACCOUNT ? ( <>
- { - setAppData("createNewContactUnderAccount", checked); - }} - /> +
{ - return await conn - .sobject(createEventOn) - .create( - this.generateCreateRecordBody({ - attendee, - recordType: createEventOn, - organizerId, - }) - ) - .then((result) => { - if (result.success) { - createdContacts.push({ id: result.id, email: attendee.email }); - } - }); + return await this.createAttendeeRecord({ + attendee, + recordType: SalesforceRecordEnum.CONTACT, + organizerId, + }).then((result) => { + createdContacts.push(...result); + }); }) ); } + if (createEventOn === SalesforceRecordEnum.LEAD) { + // Base this off of the first contact + const attendee = contactsToCreate[0]; + + if (appOptions.createNewContactUnderAccount) { + // Check for an account + const accountId = await this.getAccountIdBasedOnEmailDomainOfContacts(attendee.email); + + if (accountId) { + const createdAccountContacts = await this.createNewContactUnderAnAccount({ + attendee, + accountId, + organizerId, + }); + + if (createdContacts.length > 0) { + createdContacts.push(...createdAccountContacts); + } + } + } + + await this.createAttendeeRecord({ attendee, recordType: SalesforceRecordEnum.LEAD, organizerId }).then( + (result) => { + createdContacts.push(...result); + } + ); + } + if (createEventOn === SalesforceRecordEnum.ACCOUNT) { if (!appOptions.createNewContactUnderAccount && !appOptions.createLeadIfAccountNull) { this.setDoNotCreateEvent(true); @@ -441,34 +464,16 @@ export default class SalesforceCRMService implements CRM { let contactCreated = false; if (accountId && appOptions.createNewContactUnderAccount) { - // First see if the contact already exists and connect it to the account - const userQuery = await conn.query(`SELECT Id, Email FROM Contact WHERE Email = '${attendee.email}'`); - if (userQuery.records.length) { - const contact = userQuery.records[0] as { Id: string; Email: string }; - await conn.sobject(SalesforceRecordEnum.CONTACT).update({ - // The first argument is the WHERE clause - Id: contact.Id, - AccountId: accountId, - }); - return [{ id: contact.Id, email: contact.Email }]; - } + const createdAccountContacts = await this.createNewContactUnderAnAccount({ + attendee, + accountId, + organizerId, + }); - await conn - .sobject(SalesforceRecordEnum.CONTACT) - .create({ - ...this.generateCreateRecordBody({ - attendee, - recordType: SalesforceRecordEnum.CONTACT, - organizerId, - }), - AccountId: accountId, - }) - .then((result) => { - if (result.success) { - createdContacts.push({ id: result.id, email: attendee.email }); - contactCreated = true; - } - }); + if (createdContacts.length > 0) { + createdContacts.push(...createdAccountContacts); + contactCreated = true; + } } if (!accountId && appOptions.createLeadIfAccountNull && !contactCreated) { @@ -629,6 +634,42 @@ export default class SalesforceCRMService implements CRM { return dominantAccountId; } + private async createAttendeeRecord({ + attendee, + recordType, + organizerId, + accountId, + }: { + attendee: Attendee; + recordType: SalesforceRecordEnum; + organizerId?: string; + accountId?: string; + }) { + const conn = await this.conn; + + return await conn + .sobject(recordType) + .create({ + ...this.generateCreateRecordBody({ + attendee, + recordType: recordType, + organizerId, + }), + AccountId: accountId, + }) + .then((result) => { + if (result.success) { + return [{ id: result.id, email: attendee.email }]; + } else { + return []; + } + }) + .catch((error) => { + this.log.error(`Error creating Salesforce contact for ${attendee.email} with error ${error}`); + return []; + }); + } + private generateCreateRecordBody({ attendee, recordType, @@ -864,6 +905,37 @@ export default class SalesforceCRMService implements CRM { return query.records[0] as Record; } + private async createNewContactUnderAnAccount({ + attendee, + accountId, + organizerId, + }: { + attendee: Attendee; + accountId: string; + organizerId?: string; + }) { + const conn = await this.conn; + + // First see if the contact already exists and connect it to the account + const userQuery = await conn.query(`SELECT Id, Email FROM Contact WHERE Email = '${attendee.email}'`); + if (userQuery.records.length) { + const contact = userQuery.records[0] as { Id: string; Email: string }; + await conn.sobject(SalesforceRecordEnum.CONTACT).update({ + // The first argument is the WHERE clause + Id: contact.Id, + AccountId: accountId, + }); + return [{ id: contact.Id, email: contact.Email }]; + } + + return await this.createAttendeeRecord({ + attendee, + recordType: SalesforceRecordEnum.CONTACT, + accountId, + organizerId, + }); + } + async findUserEmailFromLookupField( attendeeEmail: string, fieldName: string,