Skip to main content

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

  1. In your Trailhead Playground, click the setup gear Setup and select Developer Console.
  2. In the Developer Console, click Debug | Open Execute Anonymous Window.
  3. 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);
  4. Replace Enter_your_name_here with your name. Don’t delete the surrounding single quotes.
  5. Select Open Log.
  6. Click Execute. The execution log opens, displaying the result of running your code.
  7. Select Debug Only. The Details column displays the contents of the debug statements in your code.
  8. 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.

  1. Return to your Trailhead Playground in your browser.
  2. Click the setup gear Setup and select Setup.
  3. Click Object Manager.
  4. Click Create | Custom Object.
  5. 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
  6. 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.
  7. 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.

  1. If it isn’t already selected, select the Candidate object.
  2. Click Tab Style and choose any image.
  3. Click Next, Next, Save.

Create Custom Fields

Create custom fields for candidate first name, last name, and email.

  1. In the Candidate object, click Fields & Relationships, and then click New.
  2. Select Text, and then click Next.
  3. Define the field as follows:
    • Field Label: First Name
    • Length: 50
  4. Leave everything else as is, and click Next, Next, and Save & New.
  5. Define another field:
    • Data Type: Text
    • Field Label: Last Name
    • Length: 50
  6. Leave everything else as is, and click Next, Next, and Save & New.
  7. Define another field:
    • Data Type: Email
    • Field Label: Email
  8. Leave everything else as is, and click Next, Next, and Save.
Share your Trailhead feedback over on Salesforce Help.

We'd love to hear about your experience with Trailhead - you can now access the new feedback form anytime from the Salesforce Help site.

Learn More Continue to Share Feedback