Sobject Records in various Trigger Events

This small table is very helpful for the Beginner and Intermediate level Developers. It is also helpful for cracking the salesforce interviews. Basically it shows that in various Trigger events what does Sobject record collection hold. Here Sobject Records refers to below Trigger context variables.

  • Trigger.new: Returns a list of the new versions of the sObject records.
  • Trigger.newMap: A map of IDs to the new versions of the sObject records.
  • Trigger.old: Returns a list of the old versions of the sObject records.
  • Trigger.oldMap: A map of IDs to the old versions of the sObject records.

Trigger Events Trigger.new Trigger.newMap Trigger.old Trigger.oldMap
Before Insert NULL NULL NULL
After Insert NULL NULL
Before Update
After Update
Before Delete NULL NULL
After Delete NULL NULL
After Undelete NULL NULL

Resources

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.

 

 

Using Variable for Static Resource

Did you ever came across such requirement, when you have your static resource name in a variable and you have to use that variable to access static resource on your visualforce page. Lets see Using Variable for Static Resource.

Lets understand this with an example.
We have 3 static resources for 3 colours Red, Yellow and Green. On the Contact Object we have a pick-list field Colour (Colour__c) which have values Red, Yellow and Green.
Now we have a custom visualforce page and Apex class, the page shows some basic information of the contact record and it should show image stored in the static resource for the colour.

To achieve this We have to use

If we have name of static resource in an apex variable.

On the visualforce page we have to use

 

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 FISHERINV Function in Apex Salesforce

In last post (Excel FISHER Function in Apex Salesforce), we gone through the code for Excel FISHER function in apex. In this post we will see Excel FISHERINV function in apex salesforce. Basically FISHERINV function returns the inverse of the Fisher transformation.

Syntax of FISHERINV function is as follows in Excel

  • FISHERINV(y)
  • WHERE y is required
  • Value of y should be numeric.

APEX CODE

Let’s see the code for Excel FISHER function in Apex Salesforce.

Above code can be added in any apex class, after adding this code we can use it as per our requirement. Lets test it from developer console. Go to developer console and in Execute anonymous window paste the below code.

After executing above code snippet you will get below output.

Excel FISHERINV function in apex salesforce
Output in Developer console after executing code.

Thank you for reading 🙂

 

Switch to Lightning Experience Using Apex Salesforce

In this post we will see how we can Switch to Lightning Experience Using Apex salesforce. When we are logged in salesforce org, then we can switch to lightning experience using UI. But what if we want to switch other users to lightning experience, so there are two ways.

  1. Individually login as all user and click on Switch to Lightning Experience : (That’s NOT a good Solution).
  2. Update all users from apex code. (That’s a Good Solution).

So what needs to be done for second option.  First see the “UserPreferencesLightningExperiencePreferred” field on  User Object. Then we need to set this field to true using code. See the code snippet below, you can execute this code from developer console.

Apex Code

Use this if you need to mass update users to switch to lightning experience.

Excel FISHER Function in Apex Salesforce

In this post we will see how we can write Excel FISHER function in Apex Salesforce. Before going to the apex code lets first see some details about FISHER function. This function returns the fisher transformation at x.

Syntax of FISHER function is as follows in Excel

  • FISHER(x)
  • WHERE x is required
  • Value of x should be greater than -1 and less than 1 i.e -1 < x < 1.

Now see the code for Excel FISHER function in Apex Salesforce.

Above code can be added in any apex class, after adding this code we can use it as per our requirement. Lets test it from developer console. Go to developer console and in Execute anonymous window paste the below code.

After executing above code snippet you will get below output.

Excel Fisher Function In Apex Salesforce
Output in Developer console after selecting Debug

Hope this code will save your time, if you need to implement FISHER function in salesforce.

Happy Coding 🙂
799 The Coder

See some more post for Excel function in Apex

Excel CORREL function in Apex Salesforce

Today we will see salesforce apex equivalent code for Excel CORREL function. Excel CORREL function in Apex Salesforce can be used if we need to perform same calculation on salesforce data as excel CORREL  function.

Syntax of CORREL function is as follows in Excel

  • CORREL(array1, array2)
  • WHERE array1 and array2 both are Required.
  • We must ensure that array1 and array2 are same in size.

See the code below, code has static methods written you can put them in your util class/helper class.

 

Once we have above methods in our code, we can use it 🙂 , Now lets test the method from developer console, open developer console and write below code.

When you execute the above code, you will get the output as follows.

####> correlationCoeffient = 0.997054485501581486225379535213640

correlationCoeffient = 0.997054485501581486225379535213640

Hope this will help you  🙂

See more Excel functions in apex salesforce code

Happy Coding !

799 The Coder