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 process builder/autolaunched visual workflow, visual workflow and a small snippet of visualforce to invoke the visual workflow.

Few things to note:

  • This works in Salesforce Classic and 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”. I’m still figuring out whether I can solve for the multiple contacts declaratively and will share the solution for sure.

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

  • You can only schedule actions in Process Builder if your process initiates when a record is created OR on the object node, the process is set to “Start the process when a record is created or edited”, AND on the criteria node, the checkbox “Do you want to execute the actions only when specified changes are made to the record?” is checked. Only criteria on which this is checked will have the Scheduled Actions option available.
  • You can initiate a visual workflow from a detail record page by embedding a visualforce page that invokes the visual workflow in an object’s page layout. To see the results of the visual workflow, you need to refresh the detail record. Since this only updates when you refresh the page, if the business need is to be able to report on this data, then you will have to go the code route. If all you want to do is see the last time you met with a contact on the record, then this will suffice.
  • 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).
  • For any data actions (fast and record lookup, create, update and delete actions) performed in visual workflow, best practice is to include a flow element to send an email to your Salesforce administrator about the fault.

Flow trick: To getting the Fault connector to appear, either draw the regular connector link to another flow element or connect it to a temporary flow element. Draw the fault connector to the Send Email element. Then, go back and delete the regular connector.

  • Scheduled actions from visual workflow can be found on the Flows screen under Paused and Waiting Interviews. Unlike time based workflows, they are not listed under Monitor | Time-Based Workflow.

PausedandWaitingInterviews.JPG

  • 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 two custom fields (Activity and Contact objects), a process builder, two visual workflow, a visualforce page that invokes the visual workflow and an update to the contact page layout(s) to add the Last Event Date field and the visualforce page.

UpdateLastEventDateProcessBuilder

UpdateLastEventDateVisualWorkflow

Quick Steps:

  1. Create an Activity custom field (Customize | Activity | Activity Custom Fields), create a new field that will calculate the difference between today’s date and the event date (i.e. task due date, or API Name: ActivityDate). We will use this to determine the most recent call task date.

EventDiffinDaysCustomField.JPG

1a. Set the field level security accordingly – make it visible to system admins and most likely, not other users.

1b. This formula field is used for processing so you do not need to add it to a event page layout.

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

LastEventDateCustomField.JPG

2a. Set the field level security to be visible for all applicable profiles and editable to profiles that would create call tasks.

2b. Add this field to the contact page layout where you’d want to track the last call 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.

3. Create a visual workflow (Create | Workflows & Approvals | Flows). This flow will perform a lookup of events for the specified Contact based on the smallest positive duration from the event date and today’s date and update the Last Event Date field with the most recent event that has passed.

A. We need to create two variables that will be used in the visual workflow per the screenshots below. The variables will store the ContactID of the event and the ActivityDate of the event.

VarContactID.JPGvarActivityDate

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

B. Next, we will create a Record Lookup flow element. This will lookup the contact’s events that have a positive duration (difference between the activity date and today’s date), Event Subtype = Event, Start Date/Time is less 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 record lookup flow element will do.

LastEventDate-RecordLookup1.JPGLastEventDate-RecordLookup2

C. Make sure this flow element is “Set as Start Element.”

LastEventDate-RecordLookupStartingElement.JPG

D. Create a Record Update flow element to update Last Call Date field with the value in varActivityDate variable in the Contact record where the ID matches the varContactID variable that will be passed from the visualforce page.

LastEventDate-RecordUpdate1

LastEventDate-RecordUpdate2.JPG

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

E. As a best practice, where there is a DML action (fast or record create, update, delete or lookup action) in a flow, you should also include notification of a flow fault.

Note: To avoid “hardcoding” email addresses in a fault email, refer to a post Using Custom Metadata Type in Visual Workflow Fault Emails.

Select the Send Email flow element that is listed under Static Actions, not under Quick Actions.

StaticActions

Configure the body to show the fault message, subject and email address(es).

Body: Fault Message: {!$Flow.FaultMessage}

Subject: Error – Update Contacts Last Call Date

FaultEmail.JPG

Best practice tip: Don’t forget to provide a description so you and other/future admins know what this send email element is supposed to do.

You will need to draw a connector line between the Record Update and Send Email elements.

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.

UpdateLastEventDateVisualWorkflow2.JPG

F. Provide a name for your flow and ensure that the type is flow, not autolaunched flow.

Note: You may need to do a save as twice as a flow as it appears Salesforce, by default, does the first save as an autolaunch flow instead of a flow. Just delete the autolaunch flow later.

FlowProperties.JPG

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

G. Click the “Close” button.

H. On the flows screen, activate the flow.

LastEventDate-ActivateFlow.JPG

4. Create a visualforce page (Develop | Visualforce pages) that invokes the visual workflow.

Best practice tip: Don’t forget to provide a description so you and other/future admins know what this visualforce page is supposed to do.

ContactLastEventDateVisualforcepage.JPG

Don’t let this part scare you. I’m not a developer so if I can copy, paste and tweak, so can you. I quote Mike Gerholdt at the Dreamforce 15 Admin Keynote “There is no shame in copy and paste.”

Here are the contents of your visualforce page:

<apex:page standardController=”Contact“>
    <flow:interview name=”Update_Contact_Last_Event_Date_PrimaryContact“>
    <apex:param name=”varContactID” value=”{!Contact.Id}”/>

    </flow:interview>
</apex:page>

Let me explain the bolded items you will need to modify in your visualforce page:

  • standardController: Reference the object (API name) that this visual flow will execute on.
  • interview name: This is the name of your Flow Name.
  • param name: This is the variable name in your flow. In this example, we are passing the contact record ID as variable varContactID used in the visual flow. Note: The variable name must match exactly to the variable name in your visual flow.

5. Update the contact page layout to add the visualforce page. Set the visualforce page properties to 0 width and 0 height since the visualforce page does not need to show on the actual page layout.

ContactLastEventDateVisualforcepage-Properties.JPG

6. Add the newly created visualforce page to the profile(s) that need to execute the visual workflow. By default, it is automatically added to the System Administrator profile.

EnableVisualforceAccess.JPG

7. 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.

Now, we need to create a process for new or edits to an existing event using Process Builder and an autolaunched Visual Workflow.

8. Clone the visual workflow created in Step 3 above (Create | Workflows & Approvals | Flows).

A. Locate and open the flow. Click on the “Save As” button, Select “New Flow”as Save As and “Autolaunched 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 visual flow, should indicate that this is invoked from a process builder.

UpdateLastEventDateVisualWorkflow-NewFlow.JPG

B..Click the “Close” button.

C. On the flows screen, activate the flow.

9. Create the process builder (Create | Workflows & Approvals | Process Builder)

A. Click on the new button. Provide a name for your process and a description.

PBUpdateLastEventDate-Properties.JPG

B. This process will run on the Event object.

PBUpdateLastEventDate-EventObject.JPG

C. There is only one node criteria in this process that will execute when the event attendee (WhoID) is not blank which will invoke the autolaunched visual flow created in Step 8 immediately and again after the event’s end date/time.

PBUpdateLastEventDate-CriteriaNode.JPG

C. We want to have an immediate action to invoke the visual workflow to update the last event date on the contact so it can refresh the date if there have been any event changes that would cause the last event date field to update. We will pass the event’s WhoID as the flow variable varContactID.

PBUpdateLastEventDate-ImmediateAction.JPG

D. Now, we also want a scheduled action to invoke the autolaunched visual flow created in Step 8, after the event’s start date/time:

PBUpdateLastEventDate-ScheduledAction.JPG

F. Invoke the visual workflow and pass the event’s WhoID as the flow variable varContactID.

PBUpdateLastEventDate-ScheduledAction1.JPG

G. Activate the process builder.

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 activity and contact custom fields, contact page layout(s)*, process builder and visual workflows 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 Dreamfactory, you can use the tool to deploy field permissions and profile system permissions.
  • Activate the visual workflows and process builder after they deployed in Production as visual workflows and process builders are deployed as inactive.

 

Advertisements

2 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

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s