Hello World with Apex


Posted on

Salesforce is extremely popular software. It is used by large and small companies alike, and its Salesforce1 Platform allows you as a developer to create applications in the cloud.

You write code on the Salesforce1 Platform in a language called Apex. It is similar to Java. Most of the tutorials for getting started with Apex use the Force.com IDE. I’m not a fan of IDEs.

In this tutorial, I am going to show you how to write an Apex Hello World app without using an IDE. Let’s begin.

Setup Salesforce.com

Create a salesforce.com developer account.

You’ll receive an email from Salesforce with a link to confirm. Click it.

On the next screen, set your password. Yeah, it’s an odd flow, but it’s Salesforce so what do you expect.

You now have a developer account.

Generate your security token. We will need this later. While logged into your developer account, click “Your Name > My Settings” at the top right portion of your developer account dashboard.

Then on the left side of the screen click “Personal > Reset My Security Token.” Click the “Reset Security Token” button. Salesforce emails you a security token. You will need this later.

Setup local environment

Install Sublime Text 3.

Install Mavens Mate.

Open up Sublime Text 3, and then open up Mavens Mate on your machine. There should be a Mavens Mate icon, at the top bar of your screen. Click it and then click “Plugins.”

On the next window, click “Install Plugin.” This installs the Mavens Mate plugin into Sublime Text 3.

Close and reopen Sublime Text 3.

Click “Mavens Mate > Settings > User” at the top Sublime Text 3 Toolbar.

This will open a file called mavensmate.sublime-settings. Paste the following in that file. Adjust to the path and directory you want to store your salesforce code in.

{
  "mm_workspace": "/Users/scottmotte/code/salesforce"
}

Save and close that file.

Create a project

With Sublime Text 3 still open, click Mavens Mate > Project > New Project.

On the next screen enter the following:

  • Project name: name it something you’d like
  • Username: enter your email address
  • Password: enter your password APPENDED with your salesforce security token. (see above for how to generate the security token)
  • Click the Advanced tab and unselect everything except ApexClass and CustomObject.

When you’re ready click the “Create Project” button.

This will create the project up on Salesforce.com and locally on your machine. That’s what we want. It’s the way Salesforce development works. Now when we develop locally, it will sync things up remotely.

Create a class

With Sublime Text 3 still open, click “Mavens Mate > Metadata > New Apex Class.” Then from the popup click “Default.” Finally, name your class “HelloWorld.”

Edit that file to look like the following:

global class HelloWorld {
  public String hello() {
    return 'Hello World!';
  }
}

Create a test class

Next, click “Mavens Mate > Metadata > New Apex Class.” Then from the popup click “Unit Test.” Finally, name this class “HelloWorldTest.”

Edit that file to look like the following.

@isTest
private class HelloWorldTest {
  @isTest static void hello() {
    HelloWorld hello_world = new HelloWorld();
    String result = hello_world.hello();
  }
}

Let’s run this test.

Click “Mavens Mate > Unit Testing > Open Apex Test Runner UI.”

On the next screen, make sure the HelloWorldTest is selected and click the “Run Tests” button.

You’re done

The test should pass. Congrats, you wrote your first Apex hello world code!


Programmer and tinkerer. Fledgling Devangelist @SendGrid. Follow me @scottmotte.

Scott Motte on Twitter

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>