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.

 

 

SOQL query to fetch all fields of an object

Just in case if we need to fetch all fields with SOQL in salesforce, what we will do? SOQL query to fetch all fields of an object can be achieved with below approach.

  1. Write each field in SOQL
  2. Go to developer console, open the object, select all fields and click on query, then copy all fields from query tab.
  3. Write code in such a way that it will fetch all fields.

Now lets consider pros and cons of each approach. In first approach “Write each field in SOQL”, if any new field is created in future then code update required. Also we its time taking if there are too many fields.

In Second Approach, if any new fields is created in future then code update required.

In Third approach where we are writing code such that code doesn’t need any update if any new field added.

Apex Code

Below is the apex method for getting SOQL query to fetch all fields of an object.

This method can be used as below.

Hope this will help and increase productivity of developers.

 

Excel LINEST function in Apex Salesforce

As a developer we sometime need to implement excel function in code for meeting client’s requirement. Here I will share code for Excel LINEST function in apex. You can check LINEST function/method of excel here.

See below code method, you can use it in your apex class.

It will save your time if you need to write it from scratch in apex. I am open for your valuable feedback.

Happy Coding 🙂