function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Fiona BurnistonFiona Burniston 

How to populate the last event date into a custom field?

Hi,

I'm looking to create a custom field on the contact object which will store the last completed event date logged against the record.

I've tried to create this through the use of formula fields and the 'last activity date' but unfortunatly this brings through the date of previous tasks and any future events scheduled. Can someone recommend a solution? Appreciate this may only be accomplished through APEX.
 
Best Answer chosen by Fiona Burniston
Terence_ChiuTerence_Chiu
One thing I can think of would be a hybrid approach of Process Builder and Invocable Apex. You would build a Process Builder flow that performed the following:

Object: Event (at creation)
Condition :  If the Name Id (whoid) starts with 003 (indicating that the event is related to a contact record)
Action : Use a scheduled action that would fire 1 Hour after the End Date time.
The action you would use would be executing an Apex method with the Invocable Annotation. In Process builder you would pass the Name Id and the End Date Time as parameters.

You would use InvocableVariable annotation to store the Event's contact Id and End Date/Time in the class and use the Invocablemethod annotation so that the Apex class is available as an action within Process Builder. In your Apex method you would basically take the two parameters to update the custom contact date field.

Check out these links for more information on how to use Process Builder and Invocable Apex.

Documentation :

https://help.salesforce.com/servlet/servlet.FileDownload?file=015300000035wMDAAY
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableVariable.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm

Great simple blog post that should get you started:
http://bobbuzzard.blogspot.com/2015/02/lightning-process-builder-and-invocable.html

Here is my blog posts where I use Process Builder to execute Apex methods in a different use case:
https://tccloud.wordpress.com/2015/06/01/syncing-chatter-posts-on-case-feed-with-case-comments/
 

All Answers

Terence_ChiuTerence_Chiu
Fiona, are you considering completion of an Event based on whether or not the End Date of the event is the past or is there some other custom field or indicator ?
Fiona BurnistonFiona Burniston
Hi Terence,

You are correct, the completion of an event would be based on the end date being in the past.

Many Thanks,
Fiona
Terence_ChiuTerence_Chiu
One thing I can think of would be a hybrid approach of Process Builder and Invocable Apex. You would build a Process Builder flow that performed the following:

Object: Event (at creation)
Condition :  If the Name Id (whoid) starts with 003 (indicating that the event is related to a contact record)
Action : Use a scheduled action that would fire 1 Hour after the End Date time.
The action you would use would be executing an Apex method with the Invocable Annotation. In Process builder you would pass the Name Id and the End Date Time as parameters.

You would use InvocableVariable annotation to store the Event's contact Id and End Date/Time in the class and use the Invocablemethod annotation so that the Apex class is available as an action within Process Builder. In your Apex method you would basically take the two parameters to update the custom contact date field.

Check out these links for more information on how to use Process Builder and Invocable Apex.

Documentation :

https://help.salesforce.com/servlet/servlet.FileDownload?file=015300000035wMDAAY
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableVariable.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm

Great simple blog post that should get you started:
http://bobbuzzard.blogspot.com/2015/02/lightning-process-builder-and-invocable.html

Here is my blog posts where I use Process Builder to execute Apex methods in a different use case:
https://tccloud.wordpress.com/2015/06/01/syncing-chatter-posts-on-case-feed-with-case-comments/
 
This was selected as the best answer
Fiona BurnistonFiona Burniston
Hi all,

I'm having trouble writting the Invocable Annotation apex code. 

I've tried to simplyify the criteria by only wanting to show the last date of an event logged against the account and not the contact but I'm still getting errors. 

Any help would be greatly appreciated.

Thanks,
Fiona 
 
Terence_ChiuTerence_Chiu
Fiona, Would you mind providing the code you are working with so that we can get a better understanding?