How To

Record the Last Event With A Contact

MeetingImage

Your company has a business model where multiple sales people representing different channels or product focuses, or an internal/external sales team, may interact with the same individual. There is a business need to track calls to the contact or events with the contact.

These two use cases will be covered in a 2-part article series.

  1. Record the last call made to a contact
  2. Record the last event with the contact

This article will cover the use case to track the last completed event date with the contact in Salesforce. While this can be done in custom code, we are able to accomplish this with a record triggered after save flow and hidden screen flow added to the lightning record page.

Few things to note:

  • This works in Lightning Experience.
  • This solution works where the contact is the primary contact of an event or in an organization where you have not enabled the feature “Allow Users to Relate Multiple Contacts to Tasks and Events.”

Here are a few lessons learned from implementing this use case:

  • If a previously created event is deleted from the contact record, upon the next visit to the detail contact record will recalculate and remove the deleted event from being evaluated for display on the Last Event Date field.
  • Salesforce considers an event as completed (i.e. moves it into Activity History) after the event’s start date/time has past.
  • All events, regardless of all day or not, will have a Date field (API name: ActivityDate), Start Date/Time (API name: StartDateTime) and End Date/Time (API name: EndDateTime).
  • Provide descriptions, where provided, in Salesforce. This may be tedious step, I know, but your future self will thank you when you are trying to remember what you configured or assist other/future admins when troubleshooting or enhancing what was built. This includes noting the data stored in a custom field, noting where a visual flow is invoked from, the purpose of a flow variable, what a visualforce page does, etc.

Business Use Case: Addison Dogster is a system administrator at Universal Container. Sammy Sunshine is a Sales Manager who requested that we track the most recent event with the contact in Salesforce.  

Solution: It would’ve been easy for Addison to throw this over the way to the developer to develop custom code. However, being the Awesome Admin that she is, Addison was able to help Sammy through declarative actions with one custom fields (Activity and Contact objects), a record triggered after save flow, screen flow and an update to the contact page layout(s) to add the Last Event Date field.

UpdateContactLastEventDate-AfterEventSave

The above record triggered after save flow will look up the most recent event for the contact and update the last event date on the contact (1) when a new event is created where the whoId is not blank and (2) 0 days after the event’s start date time where the whoId is not blank.

UpdateContactLastEventDate-PrimaryContact

The above screen flow will look up the most recent event for the contact and update the last event date on the contact and show blank screen as its last element. This will allow it to appear “hidden” on the lightning record page.

Quick Steps:

1. Create an Contact custom field (Customize | Contacts | Fields), create a new field that will track the most recent call task date.

LastEventDateCustomField.JPG

1a. Set the field level security to be visible for all applicable profiles and editable to profiles that would create events.

1b. Add this field to the contact page layout where you’d want to track the last event date. Mark this field on the page layout as Read Only.

Best practice tips for custom fields:

  • Don’t forget to provide a description so you and other/future admins know what these fields are used for.
  • Set the FLS (field level security) for each profile. Only make the field visible and editable for the profiles that need it. Do not just click the “Next” button when you are on the screen.
  • Select only the page layouts that the new fields need to be added to. By default, new fields are added to all page layouts. Once the field(s) is/are added, you may need to update the page layout to move the field(s) to the desired location on the page.

2. Create the screen flow shown above. In Classic, go to Create | Workflows & Approvals | Flows. In Lightning Experience, it is found under Process Automation | Flows. Click on “New Flow.” Select Screen Flow. Click on the Create button. This flow will perform a lookup of events for the specified Contact based on the smallest positive duration from the event date and current date/time and update the Last Event Date field with the most recent event that has passed.

A. First, we will create a Get Records called “Lookup Most Recent Primary Event.” This will lookup the contact’s events where the Event Subtype = Event, Start Date/Time is greater than or equal to the current date/time and the attendee (WhoID) is the contact. Then based on the query, take the event with the most recent start date/time (descending order).

You’re probably wondering why I’m using the most recent start date/time instead of the end date/time. This is because Salesforce considers an event as completed or moved into Activity History when the event’s start date/time has passed.

Best practice tip: Don’t forget to provide a description so you and other/future admins know what this get records flow element does.

Configure it as shown below:

  • Object: Event
  • Condition Requirements: All Conditions Are Met (AND)
    • StartDateTime Less Than or Equal {!$Flow.CurrentDateTime}
    • EventSubtype Equals Event
    • WhoId Equals {!$Record.WhoId}
  • Sort Event Records: Descending StartDateTime
  • How Many Records to Store: Only the first record
  • How to Store Record Data: Automatically store all fields

UpdateContactLastEventDate-AfterEventSave-GetRecords

B. Next, create an Update Records flow element called “Update Contact’s Last Event Date” to update Last Call Date field with the value the ActivityDate field in the Contact record where the ID matches the WhoId field. If in Classic, we will pass the the contactId in the recordId variable in flow from the visualforce page.

Best practice tip: Don’t forget to provide a description so you and other/future admins know what this update record flow element does.

Configure it as shown below:

  • How to Find Records to Update and Set Their Vales: Specify conditions to identify records, and set fields individually
  • Object: Contact
  • Condition Requirements for Records to Update: All Conditions Are Met (AND)
    • Id Equals {!$Record.WhoId}
  • Set Field Values for the Contact Records:
    • Last_Event_Date__c: {!Lookup_Most_Recent_Primary_Event.ActivityDate}

UpdateContactLastEventDate-AfterEventSave-UpdateRecord

C. Create a Screen flow element called “Blank Screen.” Under Configure Frame deselect both Show Header and Show Footer.

Best practice tip: Don’t forget to provide a description so you and other/future admins know what this screen flow element does.

Configure it like this:

UpdateContactLastEventDate-PrimaryContact-Screen

D. Add the subflow Send Flow Fault Email. For instructions on how to create this, go to Step 2 of blog post: Maximize Maintainability With Process Builder and Componentized Visual Workflow.

E. Connect the flow elements and fault connectors to match the below…

UpdateContactLastEventDate-PrimaryContact-Connectors

Can’t seem to get the fault connector to appear? Create a temporary flow element (circled below), draw your first connector to that temporary flow element. Then, draw another connector to the send element. This connector has the word “FAULT” in it.

Once that is completed, delete the temporary flow element you created. This is the end result.

F. Name your flow and save.

Best practice tip: Don’t forget to provide a description so you and other/future admins know what this flow is used for, where it is invoked from.

UpdateContactLastEventDate-PrimaryContact-Properties

G. Click the Activate button.

3. Add the screen flow created in Step 2 to the contact lightning record page via Lightning App Builder.

LightningAppBuilder

4. Clone the flow created in Step 2 above. Open the flow from Step 2. 

A. Click the Save As button, Select “A New Flow”, provide a new name “Update Contact Last Event Date – After Event Save“, select “Record Triggered Flow” as the Type.

Best practice tip: Don’t forget to provide a description so you and other/future admins know the purpose of this record triggered flow.

UpdateContactLastEventDate-AfterEventSave-Properties

B. Click Edit in the Trigger section in Start.

Configure it to match below.

  • Trigger the Flow When: A record is created or updated
  • Run the Flow: After the record is saved

UpdateContactLastEventDate-AfterEventSave-ConfigureTrigger

C. Click Edit in the Object section in Start.

Configure it to match the below…

  • Object: Event
  • Condition Requirements: All Conditions Are Met (AND)
    • WhoId Is Null False
  • When to Run the Flow for Updated Records: Only when a record is updated to meet the condition requirements

UpdateContactLastEventDate-AfterEventSave-ChooseObject

D. Click Edit in the Scheduled Paths section in Start.

Configure it to match the below…

  • Path Label: 0 Days After the Event Start Date/Time
  • Time Source: Event: Start Date Time
  • Offset Number: 0
  • Offset Options: Days After

UpdateContactLastEventDate-AfterEventSave-ConfigurePath

E. Draw the connectors for the paths and normal connector as shown.

UpdateContactLastEventDate-AfterEventSave-Connectors

F. Click Save and Activate.

4. If the “Run Flows” permission is not enabled for the profile who will need to update the Last Event Date, you need to enable this under System Permissions.

That’s it. Congrats, you’ve implemented the solution!

Now, before you deploy the changes to Production, you need to test your configuration changes.

  1. Login as a salesperson and navigate to a contact record.
  2. Log an event that has passed.
  3. Navigate back to the detailed contact record.
  4. Verify that the Last Event Date is updated to match the event start date/time from Step 2.
  5. Repeat this for an all day event that has passed.
  6. Verify that the Last Event Date is updated to match the event start date/time from Step 2.
  7. Log an event with a future start date/time.
  8. Verify that the Last Event Date has not been updated to match the event start date/time from Step 7.
  9. Once the event’s start date/time has passed, navigate back to the contact’s detailed record. Verify that the Last Event Date field has been updated to match the event start date/time from Step 7.
  10. Repeat Steps 1-9 for each profile that has access to this field.

Deployment Notes/Tips:

  • The contact custom field, contact page layout(s)*, lightning record page, flows can be deployed to Production in a change set.

*Assuming your page layouts are in sync with production (only includes the new custom field and not others in progress), you can deploy page layouts in a change set. Otherwise, if you are not sure, I suggest manually adding the new field to the contact page layout in Production to avoid accidentally overriding production with an older version.

  • The field level security for the custom fields and the “Run Flows” system permission enablement will need to be manually updated post deployment. I would caution against adding the related profiles in the changeset for deployment as the results of deploying a profile are not reliable. If you have a tool like Snapshot by Metazoa, you can use the tool to deploy field permissions and profile system permissions.
  • Activate the flows after they deployed in Production as flow are deployed as inactive.

4 thoughts on “Record the Last Event With A Contact

  1. This is a great tutorial on using process builder. I plan to check this out. Great work! You mentioned if you need this for reporting that it is better to go the apex code route since this only run when you view the record. There is still another declarative solution for this. You can use Rollup Helper to grab the last call or last event on a contact and it takes just a few clicks to the configure the solution. http://appexchange.salesforce.com/listingDetail?listingId=a0N30000009i3UpEAI

    Like

  2. HI Jen. I have a field called last scheduled meeting date on the contact object. Very similar to what you outlined here. The goal is to have the field update to the next activity date once the previous event has been changed by status “closed”. Do you think this can be controlled in Flow now and loop when the status is changed? without using process builder or visualforce to activate the flow

    Like

  3. At first glance, I think you can do this with a record triggered flow. Guess this blog post is a good candidate for a refresh in a better solution given feature enhancements.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s