Working With Apex

,

As with the other components of the Force Platform, you need to learn some of the basics about how to interact with Apex code.

How Do You Create Apex Code?

Apex code can be created with any tool that edits text files, but the standard methods of creating and editing Apex code are the Setup menu and the Force Platform IDE.

You can interact with Apex Classes through the Setup Develop Apex Classes choices to get to the page shown below.

Figure 176. Accessing Apex Classes through Setup


This page lists the classes currently available and gives you the opportunity to create new ones. You can edit, delete and assign security, described below, through this interface.

When you edit a class, you are placed into a code editing window, as shown below. This edit window uses color to highlight different attributes in the code, such as variables and keywords. This window is similar to the window used to edit Visualforce pages in Developer Mode.

Figure 177. Editing an Apex class in Setup


If you are developing a trigger with Apex, you start from the detail page for the object that triggers the code. The Triggers section of the page lists existing triggers, along with a New button to create new triggers. Both creating a new trigger and editing an existing trigger or creating a new trigger puts you into the edit window shown above.

The Force Platform IDE is a plug-in to the industry standard Eclipse framework. Many developers are already familiar with the Eclipse, and the Force Platform IDE, as you saw in Chapter 8: The Force Platform IDE, is a highly productive environment for developing and testing Apex code. You can create and edit both Apex classes and triggers in the Force Platform IDE. The examples in the remainder of this chapter and the next use the Force Platform IDE as the development environment for hands-on examples.

Apex Code and Security

Apex code exists on the Force Platform. As such, no one can run your Apex code, whether implicitly, with triggers, or explicitly without first being authenticated users for your organization.

As you learned in Chapter 7: Protecting Your Data, the Force Platform has a rich security framework, consisting of component-based security, implemented through profiles, and record-based security, implemented through record ownership and sharing. All Apex code executes as system user, with all access to all data and other components, with one exception, which you will learn about later. A system user is not limited by any profiles, so component-based security limitations expressed through profiles are, by default, ignored.

Because of this, a user must have the powerful Modify All Data permission before they can be granted the Author Apex permission, required to develop Apex code.

However, you can limit the ability to use a class to one or more security profiles, so you can prevent groups of users from using the functionality of those classes in this manner. Although triggers use Apex code implicitly, a user cannot cause a trigger to fire if they do not have access to the operations that would cause the trigger to fire.

You can also add the keywords with sharing in your definition of an Apex class. These keywords indicate that the sharing rules for the user who has called the Apex class should be respected.

Apex Code and Web Services

Apex communicates with the outside world through the mechanism of web services. You can expose any method in an Apex class as a web service through the addition of the keyword webservice at the beginning of the method declaration. You can also expose variables within the class with the same keyword.

Once you have created methods designated as web services, you can generate Web Service Definition Language (WSDL) for them. The WSDL can be consumed by applications to access the Force Platform methods.

Caution

Apex methods are run as a system user, so you should take great care to avoid potentially exposing your data through a web service call to an Apex method. However, a web service call to the Force Platform can only be executed by an authenticated user, as described above.


You can also create a Apex class stub from a WSDL specification for an external web service, and then call the methods in that class. Please be aware that there are some limitations in the use of this feature, such as a built-in timeout of 10 seconds for a response.

For more information on Apex code and web services, consult the Apex Language Reference manual.

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

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