Beginner Apex Trigger Example

This post is intended for beginner developers or admins that are interested in learning more about the development side of the Salesforce platform. We will walk through the creation of a Salesforce Apex Trigger, highlight the fundamental elements of the logic and identify why the logic was built the way that we coded it here.

Let's consider for a moment that your Salesforce organization uses Salesforce Communities and has one enabled for your customers. Now in Salesforce Communities each individual has the option of deciding what pieces of their personal information is shared internally and externally. Let's also say that the business leaders within your organization want each employee to share their email address, phone number, city, state and country so that they are visible to all Community Users, both internal and external.

One way to handle this requirement is to ask the Users of your Salesforce Community to update the settings for their contact information manually. You can provide them with the instructions on how to update these settings and ask them to perform the steps the first time they login to the platform or within some timeframe. Then you probably have to harp on them until they confirm that the modifications have been made. Another way to handle this is to write a simple Salesforce Apex Trigger that automatically populates the settings you require and enforces these settings each time a User record is created or saved.

The first thing we need to consider is the object on which this trigger will fire and the timing of the trigger. The timing of the trigger we plan on building should be "before" and we will want it to fire on "insert" and "update." By firing during the before context we will be able to pass the updates to the fields in question directly as we process each record and we will not have to worry about processing any additional DML requests.

At this point we can create the Salesforce Apex Trigger in our sandbox or developer org. Navigate to Setup > Customize > Users > Triggers. On the resulting page click the "New" button where you can copy and paste the code from below:

trigger userBefore on User (before insert, before update) {
	
}

This first portion of code gives the trigger a name, specifies the object on which it operates, and defines the events that cause it to fire. In our example, this trigger is called userBefore, it operates on the User object, and runs before new Users are inserted into the database and before existing Users are updated to the database.

Next we need to loop through the records that are being inserted or updated.

for (User u : Trigger.new) { //for all records
	//do something
}

Trigger context variables such as Trigger.new are built into all triggers and provide access to the records that caused the trigger to fire. In this case, Trigger.new contains all the Users that are about to be inserted or updated. The for loop in our example is iterating over each User record provided by Trigger.new and we are assigning each record to the variable u.

Now we can do something with all of the records being processed by the Salesforce Apex Trigger. In this particular case we can populate the values of specific fields right as we are processing them. As such, we will provide a value for each field we want to populate within the for loop.

for (User u : Trigger.new) { //for all records
	u.UserPreferencesShowCityToExternalUsers = true;
	u.UserPreferencesShowCountryToExternalUsers = true;
	u.UserPreferencesShowEmailToExternalUsers = true;
	u.UserPreferencesShowStateToExternalUsers = true;
	u.UserPreferencesShowTitleToExternalUsers = true;
	u.UserPreferencesShowWorkPhoneToExternalUsers = true;
}

You may notice that there is no explicit update statement being used in the Trigger logic. Because we are passing the field values for each record within the for loop, the values will be committed to the record after the loop completes and the records are saved.

Bringing all of the code together yields the following trigger.

/*
	Created by: Greg Hacic
	Last Update: 26 September 2014 by Greg Hacic
	Questions?: greg@interactiveties.com
	
	Notes:
		- Enforces accessibility to User level details in Salesforce Community for org
		- Tested by userBeforeTest.class
*/
trigger userBefore on User (before insert, before update) {
	
	for (User u : Trigger.new) { //for each User declared as u within the entire batch of Users being processed
		u.UserPreferencesShowCityToExternalUsers = true; //set the UserPreferencesShowCityToExternalUsers as true
		u.UserPreferencesShowCountryToExternalUsers = true; //set the UserPreferencesShowCountryToExternalUsers as true
		u.UserPreferencesShowEmailToExternalUsers = true; //set the UserPreferencesShowEmailToExternalUsers as true
		u.UserPreferencesShowStateToExternalUsers = true; //set the UserPreferencesShowStateToExternalUsers as true
		u.UserPreferencesShowTitleToExternalUsers = true; //set the UserPreferencesShowTitleToExternalUsers as true
		u.UserPreferencesShowWorkPhoneToExternalUsers = true; //set the UserPreferencesShowWorkPhoneToExternalUsers as true
	}

}

I added some comments at the top where I like to keep track of the person that last updated the code, the person that created the code and some notes like where the test methods for the code can be found. The unit test for this Salesforce Apex Trigger can be found in the Beginner Apex Unit Test Example post.

Automated Exchange Rates in Salesforce.com

Reduce Repetitive Tasks, Eliminate Errors & Free Up Your Administrators.

Birthday Reminders for Salesforce.com

It might lead to a sale. Or it might make you feel good.