Open Documentation Menu

Platform events

Platform events allow you to connect unrelated processes within and outside of Salesforce.

Use platform events, for example, to react dynamically to the upload of a new ContentDocument document or DMS document and to process the documents further with your own flows.

Reacting to the upload of a Salesforce file

When a file is uploaded in Salesforce to Files or to the record under Files & attachments, a ContentDocument data record is created. The ContentDocument data record is linked to the corresponding data record. Use ContentDocument data records to react to the upload using an Apex trigger and then to trigger your own processes or execute your own Apex code.

Note

The following types of data records are not supported by data record flows:

  • ContentDocument

  • ContentDocumentLink

  • ContentVersion

If these unsupported data records are created, the reaction with an Apex trigger is required. In order to still use flows – and thus also all flow modules for work with DMS documents – follow the sections below.

Creating a platform event

Create a platform event in order to be able to react to the upload of a Salesforce file in a flow.

This is how it works 

  1. Navigate to Setup.

  2. Open Setup > Integrations > Platform Events.

  3. Select New Platform Event.

  4. Enter a unique label, plural label and a unique object name.

  5. Select Publish After Commit. This ensures that the new ContentVersion data record is saved correctly in the system before an upload attempt is started.

  6. Under Deployment Status, select the status Deployed. This enables you to use the event straight away.

  7. Save your changes.

  8. Under Custom Fields & Relationships, click New to add new fields.

  9. Select Text as the data type.

  10. Enter a unique field label and a field name and enter the length as follows:

    • ContentDocumentId - Length: 18

    • RelatedEntityId - Length: 18

    • RelatedEntityType - Length: 255

  11. Click Save.

Publishing the event when uploading a Salesforce file

If a file is uploaded in Salesforce, you can react to it with an Apex trigger with the ContentDocumentLink object. In the object, you can, for example, publish your own platform event with the necessary information in order to then react to it in a flow.

This is how it works 

  1. Open Developer Console.

  2. Select File > New > Apex Trigger

  3. Enter a name for the trigger.

  4. Under Object, select the option ContentDocumentLink.

  5. Replace the content of the trigger as shown in the following example:

    trigger ContentDocumentLinkTrigger on ContentDocumentLink(after insert) {
        List<ContentDocumentUpload__e> eventsToPublish = new List<ContentDocumentUpload__e>();
     
        // Collect necessary fields from each created ContentDocumentLink
        for (ContentDocumentLink createdLink : Trigger.NEW) {
            ContentDocumentUpload__e eventForLink = new ContentDocumentUpload__e(
                ContentDocumentId__c = createdLink.ContentDocumentId,
                RelatedEntityId__c = createdLink.LinkedEntityId,
                RelatedEntityType__c = String.valueOf(createdLink.LinkedEntityId.getSObjectType())
            );
     
            eventsToPublish.add(eventForLink);
        }
     
        EventBus.publish(eventsToPublish);
    }
Reacting to the upload of a DMS document

You can use the platform event UploadFinishedEvent (dvelop_docs_dev__UploadFinishedEvent__e) to react dynamically to the upload of a new DMS document. The event can be used by triggers, ProcessBuilder and flows.

Every successful upload of a DMS document triggers a new event. Failed upload attempts cannot be used with the event.

Overview of the available fields in the event 

The platform event dvelop_docs_dev__UploadFinishedEvent__e has the following fields that you can use in your flow or Apex trigger:

  • RecordId__c: The unique Salesforce ID of the object for which the upload was performed (Account, Opportunity, etc.).

  • RelatedDocumentId__c: The unique ID of the uploaded document in the corresponding DMS if the ID is provided in response to the upload.

  • RuntimeContext__c: The execution context in which the upload was performed. You can find possible values here: Salesforce Apex Reference Guide > Quiddity Enum.

  • UserId__c: The unique Salesforce ID of the user who performed the upload or on whose behalf the upload was performed.

  • Category__c: The unique ID of the category of the uploaded document in the corresponding DMS.

  • Properties__c: The properties of the uploaded document. The properties are described as follows:

    • values: Values entered for the property

    • key: Unique ID for the property in the corresponding DMS

Using the event in a flow

You can react to the upload of a DMS document with a platform event and a separate flow.

This is how it works 

  1. Navigate to Setup.

  2. Open Process Automation > Flows.

  3. Select New Flow.

  4. Define the type of the flow. 

  5. Select Platform Event-Triggered Flow.

  6. Under Start, you will find the start conditions for your flow. As the flow is triggered by an event, select the event UploadFinishedEvent under Platform Event.

  7. Click Save.

  8. Define a flow label and a flow API name.

Using the event in an Apex trigger

You can react to the upload of a DMS document with a platform event and your own Apex trigger.

This is how it works 

  1. Open Developer Console.

  2. Select File > New > Apex Trigger.

  3. Enter a name for the trigger. 

  4. Under Object, select the option dvelop_docs_dev__UploadFinishedEvent__e.

  5. Use the event in your trigger as shown in the following example:

    trigger UploadFinished on dvelop_docs_dev__UploadFinishedEvent__e (after insert) {
        for (dvelop_docs_dev__UploadFinishedEvent__e event : Trigger.NEW) {
            System.debug('Event RecordID: ' + event.dvelop_docs_dev__RecordId__c);
            System.debug('Event RelatedDocumentID: ' + event.dvelop_docs_dev__RelatedDocumentId__c);
            System.debug('Event RuntimeContext: ' + event.dvelop_docs_dev__RuntimeContext__c);
            System.debug('Event UserId: ' + event.dvelop_docs_dev__UserId__c);
            System.debug('Event CategoryKey: ' + event.dvelop_docs_dev__Category__C);
            System.debug('Event Properties: ' + event.dvelop_docs_dev__Properties__C);
        }
    }
Transferring a new Salesforce file to the DMS

You can use your own platform events that will be started after uploading Salesforce files. This enables you to transfer the files directly to the connected DMS.

The following sections show some example use cases.

Creating a flow

The simplest way to react to the upload of a Salesforce file is with a platform event and a separate flow.

This is how it works 

  1. Navigate to Setup

  2. Open Process Automation > Flows.

  3. Select New Flow.

  4. Define the type of the flow. 

  5. Select Platform Event-Triggered Flow.

  6. Under Start, you will find the start conditions for your flow. As the flow is triggered by an event, select a previously created event under Platform Event.

  7. Click Save.

  8. Enter a flow label and flow API name.

Execute action – Transfer new file to the DMS

Once you have created your flow with the basic modules, you can execute actions and access the data/fields of the platform event. For example, you can read the object type from the event and, based on this, store the uploaded ContentDocument data record in different DMS categories.

This is how it works 

  1. Add the Action element to the flow.

  2. Select the filter Dvelop or Apex action for Category or Type .

  3. Select the Upload ContentDocument action.

  4. Enter a label and an API name.

  5. For the input values, use the fields from the platform event, which you can access via the global variable $Record.

  6. Optional: Define the input value Preserve file after upload. Use the value True if you do not want the Salesforce file to be automatically deleted after it has been successfully uploaded. If you leave the input value blank or use False, the file will be deleted automatically once it has been uploaded.

  7. Optional: Define the input value Run as Service-User. Use the value True if the access details of the general service user are to be used. If you leave the input value blank or use False, the access details of the executing user are used.