Hello World Salesforce Apex Trigger

Write Your First Hello World Salesforce Apex Trigger – Triggers for Beginners

Apex Triggers for Beginners

The “Hello World” program is the simplest program used to illustrate the basic syntax of a programming language. The program outputs “Hello World” and is traditionally the first program written by all beginner developers.

Let’s write our first “Hello World” Salesforce Apex trigger which will update a Lead with “Hello” as the first name and “World” as the last name.

Requirement:
When a Lead is updated, update the lead’s first name to ‘Hello’ and last name to ‘World’

Lets write the trigger in the “Developer Console”. To access the developer console, click the drop down on your Name (on the top right), and then click on Developer Console.

Salesforce Developer Console
Salesforce Developer Console

 

Once the developer console is open, click on File >> Open >> Apex Trigger. Now we are ready to write our first trigger.

Open Apex Trigger in Developer Console
Open Apex Trigger in Developer Console

 

The Trigger Syntax:
Every trigger is defined by using the following syntax:

  trigger *triggerName* on *Object* (*before/after event*) {
       for (*Object* *Variable* : Trigger.New)
       **Trigger Actions & Code Block**
  }

 

Here is what the terms in the syntax mean:

  • ‘triggerName’ is the name of the trigger which describes it.
  • ‘Object’ is the name of the Object on which the trigger is created
  • ‘before/after event’ are a comma separated list of events like insert, update, delete
  • ‘Trigger.New’ is a list of sObject records
  • ‘Variable’ denote a piece of memory that can contain a data value. Descriptive text is used as variables.

Lets get back to our requirement and based on the above syntax, write our ‘Hello World’ Salesforce Apex trigger.
Remember, when a Lead is updated, the first name should be updated to “Hello” and the Last Name to “World“.

We will be using comments to describe the lines of code in our trigger. But what are comments? Comments are used to describe code and are ignored by the parser – meaning the comments are NOT treated as code and will not run.
Single line comments are preceded by // and multi line comments are wrapped in /* and */

Now here comes the trigger:

// Name of the trigger is HelloWorld, It is on the Lead Object and It will run before the lead is updated (event).
trigger HelloWorld on Lead (before update) {
        //for every lead record, denoted by variable ld,in the list that is being updated
        for (lead ld : Trigger.new) {
             // add the first name as 'Hello'
             ld.FirstName = 'Hello';
             // add the last name as 'World'
             ld.LastName = 'World';
         }
}

 

This is how it looks in the developer console.

Hello World Salesforce Apex Trigger
Hello World Salesforce Apex Trigger

 

Exercise to get used to the syntax and the basic trigger for beginners:

Write a trigger to update the first name of the contact as Taha and the last name as Syed and the Phone number as 000-111-9999


Posted

in

by

Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.