Apex Trigger’s New Design

As we know that salesforce keeps updating 3 times a year. With these releases, Salesforce introduces many new features in the apex code also. Earlier I was using the following approach when writing a trigger on any object.

But after getting Switch When and Trigger.operationType, I decided to use the below design for writing the trigger logic. This is more readable and looks more clear.

Salesforce Spring ’19 Release Notes for Apex Trigger.operationType

Are you still writing two checks for detecting trigger events like before insert, before update, after update etc. For example Trigger.isInsert && Trigger.isBefore for before insert ? Now Salesforce made this easy to obtain the DML Operation Information.

Get more information here
Obtain DML Operation Information Using Trigger.operationType Context Variable

So basically this new context variable will make our code more readable and contains all information about DML Operations.

Earlier if we need to write trigger on the before insert event, then we need to use two context variables as below.

Now after Trigger.operationType context variable. we just need to write only one check and its more user friendly.

Similarly we can use for other trigger events.

Trigger.operationType returns the System.TriggerOperation enum, and System.TriggerOperation enum has following values : BEFORE_INSERT, BEFORE_UPDATE, BEFORE_DELETE,AFTER_INSERT, AFTER_UPDATE, AFTER_DELETE, and AFTER_UNDELETE.

Hope you will start using this new update in your next trigger task.

Share Lightning Notes and Attachments with Community Users

Having issue accessing images, files, Notes or Attachments on the community ? Let’s find the reason and the solution to make it work. Lets Share Lightning Notes and Attachments with Community Users.

See  Salesforce Files for more details about how notes and attachments are stored in Saleforce. To check the data model of new file system check Content Objects.  Once you are get familiar with the content objects data model, you will know that ContentDocumentLink plays role for access and sharing.

Visibility field of ContentDocumentLink object Specifies whether file is available to all users, internal users, or shared users.

Visibility can have the following values.

  • AllUsers—The file is available to all users who have permission to see the file.
  • InternalUsers—The file is available only to internal users who have permission to see the file.
  • SharedUsers—The file is available to all users who can see the feed to which the file is posted. SharedUsers is used only for files shared with users, and is available only when an org has private org-wide sharing on by default. The SharedUsers value is available in API version 32.0 and later.

If we set visibility to AllUsers than files, Notes and Attachment will be visible to community users also. In order to set the visibility to AllUsers we need to create an Apex Trigger on the ContentDocumentLInk object.

Now your files attached to a record will be available to community if your community profile has access to that record.