MyHelloWorld.apex

Image:cookbook.jpg

Force.com Cookbook Sample Code
Chapter 4: Best Practices for Writing Apex - Getting Started with Apex

To download all the code samples, access the Cookbook. - Image:Key_icon.gif

This Apex class is part of a simple scenario in which Apex is used to update a custom account field named Hello with the text "World" every time a new account is created.

To use this code in your own organization, you'll first need to define a custom text field named Hello on the Account object.

public class MyHelloWorld {

	// This method updates the Hello field on a list
	// of accounts

	public static void addHelloWorld(Account[] accs){
		for (Account a:accs){
			if (a.Hello__c != 'World')
				a.Hello__c = 'World';
		}
	}
}	

Sample code provided by salesforce.com. All rights reserved.