View on GitHub

files

Public FirmWorks Files for Salesforce

Documentation

File Events

File Events creates Salesforce Platform Events to allow you to create business logic based on Salesforce Content record changes. Salesforce does not allow Record Triggered flows on the Files objects, but the Platform Events published from the File Events package are based on the Content Document, Content Version, and Content Document Link. This allows you to create critical business logic based upon Salesforce File inserts, updates and deletions

For reference here is a high level image of the Salesforce File Object Structure and how it works with FirmWorks Files.

File Events Metadata 1

To get the File Events package please contact sales@getfirmworks.com or log a case with support

For more information on Salesforce Platform Event please see the Salesforce documentation below.

https://developer.salesforce.com/docs/atlas.en-us.platform_events.meta/platform_events/platform_events_intro.htm

Configuration and Setup

Once the File Events package has been installed you can use it right away. There are no permission sets to share or licenses to manage.

File Event Management

Specific Salesforce object events (before/after, insert, update, delete) can be disabled via an included custom metadata configuration record. This ability to turn on and off the generation of Platform events based on Content object’s DML is useful to control your business process work flows (e.g. bulk data updates).

The included Custom Metadata Type to control the Platform Event generation is called ‘Apex Trigger Setting’. To get to the Custom Metadata record - Enter Salesforce Setup and Type Metadata in the Quick Find and then select Custom Metadata Types. Click ‘Manage records’ to the left of the ‘Apex Trigger Setting’ Custom Metadata Type.

File Events Metadata 1

There are three records in this Metadata Type, one for each sObject Involved in Salesforce Files; Content Document, Content Version, and Content Document Link. Each record controls the trigger for that object and when it will publish the Platform Event.

To Fully Disable a File from creating a Platform Event uncheck all of the action type boxes (After Insert, After Update, After Delete, After Undelete), then save the record. The Example below would mean that we would not be creating Platform Events for the Content Version Object, thus automation could not be build from it using the File Events Package.

File Events Metadata 2

In the next example, we would only get Platform Events when a Content Document link was created or deleted.

File Events Metadata 3

Once we have established which Platform Events to get published we can continue on to building automation using Salesforce Platform Event Triggered Flows.

File Event Flows

Salesforce publishes excellent learning materials on the uses of Flows - Check out this free Trailhead before continuing. https://trailhead.salesforce.com/content/learn/trails/automate_business_processes

Platform Event Triggered flows are just like a record triggered flow but the record is just one of File Events Platform Events instead of a sObject.

To create a new Platform Event Triggered Flow go to Salesforce Setup and type Flows into the Quick Find and select Flows. then Click New Flow and Choose Platform Event Triggered Flow. Click Create to Start Building your new Flow.

File Events  Flows 1

You will need to select one of the following Platform Events to start building automation with:

File Events  Flows 2

Once you have selected which Platform Event to use, you can then continue building your automation like you would with a Record Triggered Flow. The next section will describe the fields on the three Platform Events in depth.

File Event Platform Event Glossary

Content Document Event

Action - This field will tell you what context the platform event was created in. It can be one of the following values:

Content Document Id - This is the Content Document Id that created the Platform Event.

Action - This field will tell you what context the platform event was created in. It can be one of the following values:

Content Document Id - This is the Content Document Id that created the Platform Event.

Content Document Link Id - This is the Id of the Content Document Link. This is a junction object that connects a Content Document and any other File enabled sObject.

Linked Entity Id - This is the Id for the sObject that is connected to the Content Document via the Content Document Link. This is a polymorphic lookup so it can be any object that allows files. In a Flow that uses this Id you will need to ascertain what the Object Name is before doing Get Elements to prevent errors.

Content Version Event

Action - This field will tell you what context the platform event was created in. It can be one of the following values:

Content Document Id - This is the Content Document Id that created the Platform Event.

Content Version Id - This is the Id of the Content Version. The Content Version is where the Tagging Data is stored in Salesforce. Use this object to view and update tags on the Files.

Considerations When Using File Events

File Events  Flows 1

Invocable Methods

Available in File Events version 1.0.2+

Salesforce does not provide a mechanism in Flows to query for deleted records. Primarily an issue when utilizing the ContentDocument afterDelete event. The event will return the Content Document Id of the deleted ContentDocument. However it is not possible to get the affected ContentDocumentLink junction object records to determine which related (linked) entity ids to take action with.

APEX Invocable action “Fetch Related Records For A Deleted Content Document”

The invocable action takes in a ContentDocumentId as a parameter and returns a list of Ids from the LinkedEntityId field on the deleted records. This is useful to update the related records in case the document was fulfilling a requirement as part of a workflow.

The invocable action is unable to be fully ‘bulkified’ and incurs 1 SOQL call per execution as Salesforce has the following 2 limitations on the ability to query for deleted ContentDocumentLink records.

Content Download Events - Available upon request as an unmanaged package due to Salesforce handling of namespaces

Content Download Events fire whenever a document’s contents are queried out of Salesforce. With this Event type an organization can use a flow to subscribe to the events and provide notifications, usage, and other metrics to a custom object.

With the custom metadata type firmworks__Content_Download_Setting__mdt - Manage Records Content Download Events

Information from when a document is downloaded

Example downloading from the main Salesforce Interface

{
  "CreatedById": "005Ea000003z4szIAA",
  "firmworks__Content_Id__c": "068Ea000000nqa9IAA",
  "CreatedDate": "2023-11-30T00:01:32Z",
  "firmworks__Context__c": "S1"
}

Example downloading from a public link (the created by should be the user that created the delivery method)

{
  "CreatedById": "005Ea000003z64hIAA",
  "firmworks__Content_Id__c": "068Ea000000nqQTIAY",
  "CreatedDate": "2023-11-30T00:20:39Z",
  "firmworks__Context__c": "DELIVERY"
}