Salesforce Apex Trigger Examples

Before writing the Apex Code in Salesforce you should be aware of what are Apex triggers in Salesforce. Apex triggers help us to execute the custom actions either after the event to the records in Salesforce or sometimes before the occurrence of events to the records. Examples of customized actions can be insertions or deletions.

Salesforce Trigger examples are enforced for a particular Salesforce object using a trigger or by applying the trigger context variables. For that, you need to call a particular class method from a trigger and you can use several methods in a trigger for the restriction of saving operations.

Salesforce Trigger examples are helpful in the maintenance of records and their successful management. If you want to perform certain operations based on selected conditions, you need to enforce Salesforce Trigger examples that will help you in the modification of the database for restricting specific operations from execution.

Here are the headings which are going to be discussed in the following digital piece-

1.What is an Apex trigger in Salesforce?

2.How to provide Apex trigger in Salesforce?

3.Salesforce Apex Trigger Examples

4.What are apex trigger context variables in the CRM system of Salesforce?

5.Parting tips

What is an Apex trigger in Salesforce?

Apex trigger in Salesforce is defined as the top-level standardized objects that are responsible for the performance of related tasks which cannot be executed with the use of point and click tools in Salesforce.

The interface of Salesforce does not support some selected tasks for example if you want to enforce validation of a field value or you are in a requirement to update a field on a particular record, the workflow rules and the validation rules are executed using triggers.

These top-level standard objects compile custom objects, some account objects, and few child objects. Whenever the triggers are created, they become active by default. The active triggers are automatically fired by the salesforce after the occurrence of a database event.

How to provide Apex trigger in Salesforce?

For the provision of Apex trigger in Salesforce, you need to call the trigger syntax. The syntax of a trigger is totally different from the syntax of the class definition. The definition of a particular trigger is initiated with the keyword-Trigger.

After using the keyword you have to specify the name of the trigger. Following that the Salesforce object being associated with the particular Trigger needs to be fired and the several relevant conditions.

The syntax definition for a trigger is as follows-

trigger Trigger Name on Object Name (trigger events) {code block}

For the execution of a trigger after or before certain operations such as insert, delete, update, undelete, etc, you need to remark the specification of a multiple Trigger event.

The following is the list of the events that need to be specified-

1. before insert

2. after insert

3. before update

4. after update

5. before delete

6. after delete

7. After undelete

Salesforce Apex Trigger Examples

Before the insertion of the records of the objects, you need to appear for the run of the trigger in Salesforce. As soon as the related records are deleted, you can appear for the trigger run. In addition, the trigger scenarios in Salesforce can be such that the trigger Run is detected after the fixation of the record from the recycle bin.

If you are ready to give a trigger outline, you need to attend to triggers specified in the settings of object management for a particle object whose trigger access needs to be met. The following example illustrates the firing of a simple Trigger before the insertion of an account that includes a return message to the debug log.

Step 1

The first step is to click File | New | Apex Trigger. This option is provided in the developer console.

Step 2

Now you need to enter the name of the trigger and make the selection of the account. Finally, you can click on the submit button.

Step 3

You need to make the replacement of the default code with the relevant Trigger name.

Step 4

You can press Control + S buttons so that you can save the following entered code.

Step 5

After saving the code you need to test the trigger and for that proceed with the creation of an account.

First of all, you need to make the collection of the bag which is provided in the open execute anonymous window. After that, you have to add the following code in the new window and finally proceed with clicking the execute button.

Account a = new Account (Name=’Test Trigger’);

Insert a;

Step 6

The last step is to find the keyword that you have specified with system dot debug. This whole process is getting conducted in the debug log and finally, the execution of the trigger is displayed by the log.

You should be aware of the kinds of triggers. There are basically two types of triggers in Salesforce. Following are the triggers in examples salesforce.

1. before triggers which are used for updating or validating the values of the records. You should keep in mind that these activities must be completed before they are saved to the database.

2. After triggers are basically used for accessing the field values which are already affixed by the system. The after Trigger has the capability to affect the modifications and changes existing in other records. After triggers can be fired for the records but are available as read-only. The trigger examples in salesforce are as follows-

The trigger example in Salesforce–

You have received a requirement for a particular business organization that demands the creation of a new voice record when there is a change of customer’s “customer’s status” field from inactive to an active state.

The trigger needs to be created on the APEX_Customer__c object and for that, you need to follow the below steps-

1. Firstly, you have to navigate to the object and make a click on the customer.

2. Now you have to press the new button which is provided in the related list of

A. Triggers. Now, add the following salesforce trigger example code-

In this particular example, Trigger. new is the context variable used which is responsible for the storage of the current values of the records within the trigger context.

These records can be updated or inserted with the help of Trigger context since this context variable comprises of records of customer object which is already present in the updated state.

Another salesforce trigger example-

You can also use the other context variable such as- trigger.OldMap, trigger. old, trigger.newMap, etc. If you want to make an invoice record suitable for the business requirement.

B. One of the salesforce triggers examples-

If you want to illustrate the need to update a particular operation on the records of the customer. This trigger example covers the situation where you need to insert and voice record based on a particular condition. The condition is that the customer status change should be from inactive to an active state, which is not planned for all times.

One of the context variables- trigger.oldMap, and enforces this process. This context variable helps us to save or store the key in the name of Record ID and the following values are stored in the form of old record values.

What are apex trigger context variables in the CRM system of Salesforce?

1. is Executing

This context variable returns the true value in case the Apex Code enforced in the current context is a trigger.

2. is Insert

The true value is returned in case the trigger option was fired or enforced for the insertion operation reason. It is done from the platform of user interface of Salesforce.

3. is Update

The true value is returned in case the fired trigger bears the reason for the update operation. It is executed on the user interface of Salesforce.

4. is Delete

The true value is returned in case the fired trigger bears the reason for the delete operation. One canuse API as one of the platforms of Salesforce.

5. is Before

The true value is returned in case the fired trigger before the saving of a record.

6. is After

The true value is returned in case the fired trigger after the completion of the process of saving of all the records.

7. is Undelete

The true value is returned in case the fired trigger after the recovery of the record. The recovery of the data or a record can be extracted from the bin named as Recycle bin.

8. new

The various kinds of Object records are returned by this variable. The modification of the record can be enforced only in the case of before triggers.

Parting tips

We hope that you have received the desired amount of information related to the salesforce Trigger examples. The Salesforce marketing cloud is always available for the delivery of your queries and the operation’s performance. Take the advantage of the blog and proceed with the implementation process of Trigger examples.

Leave a Reply

Your email address will not be published. Required fields are marked *