Quickstart: Using the Force.com IDE
- Create a project.
- Create an Apex class and trigger that populates the custom field with text.
- Add a unit test to automatically test the method.
1. Create a Project
The following steps create an Eclipse project and connect it to the home organization (the organization associated with the project):
- Select .
- Enter the following information and click Next:
For details on these settings, see Force.com Project Properties.
Field Value Project Name Enter a name for your project. Username Enter the username you use to log in to the organization associated with this project (“home organization”). The username must have the “Modify All Data” permission. Password Enter the password for the specified username. Security Token If you are using a security token, enter the value here. Environment Choose the appropriate environment for your connection (Developer Edition or Sandbox). Do not change endpoint Leave this option unchecked. Timeout (sec) Set to a value between 3 and 600 seconds. Proxy Settings If you connect through a proxy, click the Proxy Settings link to open the Network Connections dialog. - On the Project Contents page, choose Apex and Visualforce.
- Click Finish to create the project, connect it to the associated organization, and copy components from the home organization into the project in the appropriate folders.
Now that you have created a project, when you edit and save items in the project, the edits are saved to the server. (If the items fail to compile, they are not saved.)
2. Create Apex Components
The following steps create an Apex class and trigger to pre-populate a field on the Accounts tab.
Create a class that populates the Hello field on the Accounts tab with the word “World”:
- In Eclipse, right-click on the project you created in the Package Explorer, and select .
- Enter MyHelloWorld as the name for the project, leave the other settings as is, and click Finish.
- The source for the new MyHelloWorld.cls class is displayed. Replace the auto-generated text with the following:
Save your changes.
- If the IDE asks if you want to save the changes to the server, click Yes.
Next, create a trigger that calls MyHelloWorld.cls whenever a new record is created.
- Right-click on your project in the Package Explorer and select .
- Enter helloWorldAccountTrigger as the name of the trigger.
- Click the Object drop-down list and select Account.
- In the Apex Trigger Operations section, check the before insert checkbox.
- Click Finish.
- The source for the new helloWorldAccountTrigger.trigger file is displayed. Replace the auto-generated text with the following:
Save your changes.
To see your new Apex class and trigger working, log in to your Salesforce organization in a browser and create a new account. You should see the Description field pre-populated with the value “Hello World”.
At this point, you can make changes to the class or trigger in the project. When you save, the changes are automatically saved to the associated organization, assuming no conflicts exist. If you make changes from the organization itself, you must synchronize those changes to the project. For details, see Server Synchronization.
3. Add Tests
Unit tests are class methods that verify whether a particular piece of code is working properly. Unit test methods take no arguments, commit no data to the database, and are flagged with the testMethod keyword in the method definition. A rich set of unit tests gives you confidence that your code works correctly and can help you catch bugs when a code change suddenly causes a test to fail.
The steps below create a simple unit test for the Hello World program:
- Open MyHelloWorld.cls and add the test methods
below:
- Create another Apex
class with the name MyHelloWorld.cls. Replace
the auto-generated text with the following test
class:
- Save your changes.
-
To execute Apex unit tests:
- Select (
). - Select the Test tab.
- Add tests to your run configuration.
- To add a test class or test method, click one of the Search buttons. To select a method, first select the class that contains that method.
- To create a run of test suites:
- Select Use suites.
- Select one or more test suites.
- Click Apply.
- To execute the selected test run configuration, click Run.
- Select (
For detailed information on testing, see Testing Code with the Force.com IDE.