Execute Anonymous Blocks
Learning Objectives
In this project, you’ll:
- Use the Developer Console to execute anonymous blocks of code.
- Create a new Apex class in Salesforce.
- Instantiate an object.
- Invoke a method.
- Create a list.
- Create a list FOR loop.
- Use data manipulation language (DML) to insert an sObject into the database.
- Use Salesforce Object Query Language (SOQL) to return data to Apex.
- Write an Apex trigger.
Introduction
Apex is an object-oriented programming language that uses syntax very similar to Java syntax. It enables developers to build complex business processes, customized user interfaces, and integrations with third-party systems, when declarative tools aren’t up to the task.
As a Salesforce admin, you already know how to do a lot using declarative tools. As you work through this project, you’ll begin to see how you can use Apex code to make things happen in your org. And you just might be inspired to try your hand at a bit of coding on your own.
In this project, you execute anonymous blocks to quickly evaluate Apex code. You also write some logic to create Bank Account functionality and write an Apex trigger to create a new contact every time a candidate record is saved.
Follow Along with Trail Together
Want to follow along with an expert as you work through this step? Take a look at this video, part of the Trail Together series.
Launch Your Trailhead Playground
You'll be completing this hands-on project in your own personal Salesforce environment, called a Trailhead Playground. Get your Trailhead Playground now by first logging in to Trailhead, and then clicking Launch at the bottom of this page. Your playground opens in a new browser tab or window. Keep the playground window open while you do this project. After you complete the project steps in your playground, come back to this window and click Verify step at the bottom of this page.
Write and Execute an Anonymous Block
- In your Trailhead Playground, click the setup gear and select Developer Console.
- In the Developer Console, click Debug | Open Execute Anonymous Window.
- If there is already code in the Enter Apex Code window, replace it with this code:
string tempvar = 'Enter_your_name_here'; System.debug('Hello World!'); System.debug('My name is ' + tempvar);
- Replace Enter_your_name_here with your name. Don’t delete the surrounding single quotes.
- Select Open Log.
- Click Execute. The execution log opens, displaying the result of running your code.
- Select Debug Only. The Details column displays the contents of the debug statements in your code.
- Examine the two USER_DEBUG events. These lines correspond to the two
System.debug
statements in your anonymous block. One of the DEBUG messages should contain your name.
Create a Custom Object
Build a custom object to store information about candidates who apply for jobs at your company. We use this object later.
- Return to your Trailhead Playground in your browser.
- Click the setup gear and select Setup.
- Click Object Manager.
- Click Create | Custom Object.
- Define the object as follows.
- Label:
Candidate
- Plural Label:
Candidates
- Object Name:
Candidate
- Record Name:
Candidate Number
- Data Type: Auto Number
- Display Format:
C-{0000}
- Starting Number:
1
- Label:
- Under Object Creation Options (which is available only when a custom object is first created), select Launch New Custom Tab Wizard after saving this custom object.
- Click Save.
This directs you to the New Custom Object Tab screen. Next, let’s make the tab.
Create a Custom Tab
If the tab wizard didn’t automatically launch, that’s OK. Click Home, enter Tabs
in the Quick Find box, and then select Tabs. In the Custom Object Tabs section, click New.
Follow these steps to create a tab for your custom object.
- If it isn’t already selected, select the Candidate object.
- Click Tab Style and choose any image.
- Click Next, Next, Save.
Create Custom Fields
Create custom fields for candidate first name, last name, and email.
- In the Candidate object, click Fields & Relationships, and then click New.
- Select Text, and then click Next.
- Define the field as follows:
- Field Label:
First Name
- Length:
50
- Field Label:
- Leave everything else as is, and click Next, Next, and Save & New.
- Define another field:
- Data Type: Text
- Field Label:
Last Name
- Length:
50
- Leave everything else as is, and click Next, Next, and Save & New.
- Define another field:
- Data Type: Email
- Field Label:
Email
- Leave everything else as is, and click Next, Next, and Save.