Writing Apex code

,

Now that you have a feeling for the basics of Apex syntax, you can finally jump into creating some Apex code. This example uses the Force Platform IDE to create the code. You will learn about triggers in the next chapter, which focuses on Apex and its interactions with Force Platform data.

Using the Force Platform IDE

In Chapter 8: The Force Platform IDE, you learned about the Force Platform IDE. This development environment is based on the popular Eclipse framework.

You will be using the Force Platform IDE to create and save Apex classes and methods, culminating in a class that uses features of Apex to insure a particular type of data integrity.

But you should walk before you run, so the next few sections develop some very simple Apex code, just to get used to the process. You will use a specific feature of the Force Platform IDE that allows you to enter and run Apex code interactively.

This feature is found at the bottom of the development environment. You can see there is a window along the bottom of the main development area on the right, with four tabs showing by default. If you click on the tab labeled Execute Anonymous, you can expand the window to look similar to the figure below:

Figure 178. The ExecuteAnonymous window in the Force Platform IDE


Caution

ExecuteAnonymous calls submit Apex code to run on the Force Platform server. The code runs with the security settings of the current user - this is the one exception to the rule that Apex always runs with system privileges.


The Execute Anonymous window uses the Force Platform API call of the same name to submit Apex code to the Force Platform server and receive the results of that code.

The same functionality is available through the Setup menu. If you click on the System Log link, you bring up the dialog window shown in the figure below. This dialog has a text area for entering Apex code at the bottom of the dialog, submitted with the Execute Apex >> button, and the results shown at the top of the window.

Figure 179. The System Log window


A Simple Script

Start your Apex work with one of the simplest statements—the Apex equivalent of ’Hello World’.

1.
Start up the Force Platform IDE. If you did not start a project in Chapter 8: The Force Platform IDE, begin the development process by creating a Force Platform project. For more information about creating a Force Platform project in the Force Platform IDE, refer to that chapter to create one.

2.
Enlarge the lower pane of the environment so that you have more room to enter your code and view the results. The configuration shown in the figure of the IDE above is appropriate for the tasks ahead.

3.
Select the name of your current project as the Active Project.

4.
Select Apex_code as the log category. You will be using a method of the System class to echo values back to the results area.

5.
Enter the following code in the Source to execute window:

system.debug('Hello Apex!'),

,

Figure 180. Results from Hello Apex


6.
Click Execute Anonymous to produce the result shown in the figure above.

Well, at least you got back something. The message explains that the Apex code in the window executed successfully.

When you clicked Execute Anonymous, the Force Platform IDE sent the Apex code from the source window back to the Force Platform server where it was executed. The message was sent to the debug log, but you did not see it because your Results window is not set to display that level of detail.

7.
Set the Log level slider on the right to Fine.

8.
Click Execute Anonymous again. This time you get the desired message displayed to the right of a line that includes a timestamp marking the execution of the statement.

9.
Set the Log level slider to Finest and execute the code again.

10.
The final display of information is probably more than you need for these exercises so set the slider back to the Fine setting.

Now that you have conquered the task of entering and executing Apex code from the Force Platform IDE, you can modify the code to see a little more functionality at work.

Adding Repetition

You can expand this most basic line of Apex code by using a For loop to return multiple messages through the use of the system.debug method.

1.
Change the code in the source window to the following:

for (Integer I = 0; I < 5; I++){
 System.debug('Execution ' + I);
 }

This code is an example of a standard For loop. The variable I controls the iteration through the code, the condition of I < 5 determines when that iteration should stop, and the I++ indicates that the value of the variable increases by 1 on each iteration. The loop executes 5 times and displays text with a count of the individual executions of the system.debug call numbered from 0 to 4.

2.
Click Execute Anonymous to produce the display shown below.

Figure 181. Iterated results


Success once again.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.46.96