September 21, 2016 by LanceShi
1.1 An important message
Open Developer Console
There are several ways to write code in Salesforce. For now, let’s use developer console. It is a very good tool for beginners. There are several reasons:
- It is Salesforce built-in tool so there is no need for installation or configuration.
- It has good syntax highlighting. And it also helps you with code indent.
- If you have any errors in your code, it will tell you right away.
So, let’s start using it.
Open Developer Console via Your Name > Developer Console, as shown.
Write our first code
In the opening Developer Console, click ctrl + e. If you are using Mac, click cmd + e. This should open your Execute Anonymous Window. You can consider this as your playground for now. It should look like this:
Type in the below code:
System.debug('I am determined to start learning Salesforce development today'); System.debug('Today is ' + Date.today());
Then click Execute. Find the log in your logs tab. Double click to open it. In the filtering input, type in ‘DEBUG’. Make sure it is capitalised as the filter is case sensitive. The result should look something like below:
In the future, when I say look at the debug logs, it means open the debug log and filter it with ‘DEBUG’. I will not explain the details in the future.
A little explanation:
The code should be pretty straight-forward. But I will explain a little in details.
System.debug(), here System is the namespace. But you don’t need to worry about it for now. Just consider System.debug() means type the information out in the debug log.
Date.today() means today’s date. You don’t need to worry about the grammar for now.
Every statement in Apex need to end with ; This is the same as many other programming languages like Java and C#. In Apex, break lines doesn’t mean much unless it is in a quoted string. Only ; means it is an end to the current statement.
Write a piece of code to display ‘Hello, my name is Lance. This is my first day of learning Apex.’ Please replace Lance with your own name here. You should be able to see the result in your debug log.