Using An Apex Map with a List of sObjects

The other day I found this handy. It's a slight twist on something I found in the developer documentation. Ultimately I wanted to quickly compare some Account IDs and see if any from list A were in list B and if so, perform an action on the Accounts in list B. I decided that a map would be good for this task.

First, a quick review of what a Map is. A map is essential a key/value pair list. Here is the textbook definition:

A map is a collection of key-value pairs where each unique key maps to a single value. Keys and values can be any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types. - Salesforce Developer Documentation

A Map is perfect for this particluar task because Salesforce object IDs are unique just like the key in a Map. There is only ever going to be one.  

Below is a snippet of an "auto" populated map of the Accounts I was interested in.


List<Account> listAccountsWithSupport = [select Id,Name from Account where HasPaidSupport__c =  true];
Map<Id, Account> mapAccountsWithSupport = new Map<Id, Account>(listAccountsWithSupport);

I can now quickly compare the Accounts I have in List A with this second set of Accounts stored in a map. Both A & B could actually be maps but the method I'm working with in the legacy code already returns a List for the Accounts in pile A.

for(Account a: listAccountA){

//check to see if the current account in list A matches any in our Map (which is pile B)

  if(mapAccountsWithSupport.containsKey(a.Id)) {

      a.HasPaidSupport__c = true; //perform the desired action

/*we can also perform actions on any of the accounts in the Map if we want by using a.Id to find a specifc Account. Below will return the actual Account object based on the  key "id" if it finds one. At this point you could perform an action if you wanted. */

     Account tmpAccount = m.get(a.Id);



update listAccountA; //make sure to call any updates outside the loop :)



Three Common Apex Coding Topics

If you're an admin of a big Salesforce org, then you probably had some users complain about unfriendly errors. If your Salesforce org is in any way customized then those errors are likely related to performance issues in the custom Apex code. Although custom Apex code can be powerful, it can sometimes hinder the overall performance of your Salesforce org. Today, I'm going to introduce three changes you can make in Apex Code that will improve the performance of your Salesforce:


Enhance Validation and Workflow Rules:

One example of the errors your users might get is one that includes FIELD_CUSTOM_VALIDATION_EXCEPTION. This error usually means that you have a scheduled batch that inserts/updates records without fulfilling a Validation Rule(s). Here are a few areas you can improve performance:

  • If you have a batch class, make the class calls a DML operation and that opt_allOrNone is set to False. This will allow the rest of the operation to still succeed even if the record fails.
  • Workflow Rules are most helpful in preventing duplicate records. If you have a lot of Workflow Rules in your org, you can cut down unnecessary processes by deactivating the Workflow and improving the Trigger to do the Workflow Rule'sjob.
  • Review Validation Rules with management and make sure they are necessary to fulfill business logic. Having too many validation rules and required fields can cause unit tests to fail, which will then prevent you from deploying a change set.

Increase Field Length:

If, for example, you have a Trigger that populates values larger than the field length, then you'll most likely get a STRING_TOO_LONG error message. One thing you can do here is to increase the Field Length; it is better to have a large value than getting an error message.


Rectify API Limits:

If your org has a lot of integrations with third party apps and that those integrations are used frequently, your users might then see a "REQUEST_LIMIT_EXCEEDED" error. There's no ideal solution to reducing API usage, but one thing you can do is to evaluate the current API usage in your org (SetupAdministration SetupCompany ProfileCompany Information) and figure out a way to reduce and disperse the number of API calls.


It is always a good idea to periodically review the current custom Apex Code you have in your org with management and stakeholders. Making these changes will not only cut down the number of errors your users encounter, but will help improve the performance and the overall user-experience in your Salesforce.

Jan032014 Tip: Freeze Users

When administering Salesforce, you can at times run into scenarios where a particular users’s account should no longer have access. An example of that is when you have reason to believe that a user’s account has been compromised or someone has just left the company and you want to prevent them from logging in. Normally you would just disable the user; however, if the user is part of a custom heirachary field - there are more steps invovled. A nice solution in the meantime us to just use the "Freeze" function. 

To freeze a user in your org, go to Your NameSetupManage UsersUsers. Find and click the user and then click “Freeze” to freeze the account. 

salesforce freeze user
After the user is frozen - the Freeze button changes to an Unfreeze button which performs (well, you get the idea).
Note: When you freeze a user, it does not free up a user license. In order to actually free up a licnense you have to perform an actual deactivation of the user.

Generate a Report Right From the Record Page

One of the major perks of using is its robust Reporting tool. Although the Reporting tool is quite capable, one drawback of it is that it's somehow separated from where you'd mostly spend time working through the data. If you're looking at a particular Lead record and wanted to create a report, you'd have to navigate to the Reports tab and create a report there. Today, I'll be showing you a quick way to create a report from the click of a button right from the record page. For my tutorial, I'll create a button an place it on the Leads page- I will then have the ability to generate a report on All Open Leads; right from that button.

Click to read more ...


Custom Objects in Salesforce

In Salesforce, you're able create a custom object to store a particular set of data where users can create and manage that data all under a tab . Today, I'm going to show you how to create a custom object in Salesforce and place its data under a tab.


Building the Structure of the Custom Object :

In this tutorial, I'll be creating a "Reservations" custom object to be able to create and track customer reservations.

  • To get started, go to SetupApp SetupCreateObjects. Then click "New Custom Object."

  • Next, fill in the name for the object and click "Save."

  • Under Optional Features, make sure to allow for Reports, Activities, and Tracking Field History.
  • Next, I'm going to allow for Adding Notes and Attachments. Tick the checkbox to "Launch new Custom Tab Wizard" to customize the look and feel of the tab.

Creating the Tab for the Custom Object:

Now that I've built the structure of the custom object "Reservations," I can create the Tab where the object will be stored.

  • Pick the appropriate style of the tab and click "Next'.

  • Next, add the profiles you want to see and/or use the "Reservations" objects, then click "Next".


  • Then, determine which Salesforce apps you want this object to be available and click "Save".

  • Finally, under Custom Fields and Relationships, create the appropriate custom fields for the object. I'm going to add a new custom field "Reservation Date and Time"
  • Now, you can see the "Reservations" tab at the top where you can create new reservations.


Standardize Countries and States in Salesforce

Often times importing new data into your Salesforce org will result in having inconsistent data. This inconsistency is particularly found in Countries and States/Provinces, such as the values (US, Usa, us, usa) where they should be updated to United States, instead. Today, I'm going to give a tutorial on how to use Salesforce's Mass Update Addresses; to standardize your countries and states.

Standardizing Countries:

Initially, you want to update the countries first then the states. To get started:

  • Go to your name> Setup> and search for "Mass Update Addresses" or go to Administration Setup> Data Management> Mass Update Addresses
  • Select "Country", then click Next.

  • From the "Available Values" column, select the countries you want standardized (this view can display a maximum of 100 countries, if you can't find the countries you want updated, search for them in the above search box) and add them to the "Selected Values". Then, enter in the new name you want applied to the selected countries and click Next.

  • After that, you'll be shown a list of where records of those countries are found. Click "Replace" to replace all those countries with the new one.

Standardizing States:

Now that you've updated the countries in your org, standardize the states in those countries:

  • In Step 1, select "State/Province" and enter in the country in which the states belong to and click Next. 

  • Pick the states you want updated and enter in the new name and click Next. Then, review where those states are found in your data and click Replace to replace them with the new name.

Tips on Mass Updating Addresses in Salesforce:

  • Always standardize the countries before the states.
  • Keep in mind that once you've updated the values of countries and states, all associated filter conditions will be affected and that includes: assignment rules and web-to-lead, which then should be updated with the new values.

Salesforce Lead Queues

In Salesforce, Queues allow for management and organization of records in Leads, Cases, and custom objects. Lead Queues are particularly helpful because they allow you to funnel a group Leads into a queue based on a particular criteria whereby users in that queue can claim ownership of those Leads. For example, you can create a Lead Queue to distribute newly created Leads or those captured from web by region/territory (West Coast, North Pacific...etc) Today, I'll be giving an introduction on how to create a Leads Que.

Creating Regions/Territories:

First, we're going to create a queue for each region.

  • To get started, go to Setup> Administration Setup> Manage Users> Queues and click New.
  • Name the Queue and optionally, you can assign an email address specific for the queue; users in that queue will receive updates on actions.
  • Next, pick the object you want to create the queue in.



  • Next, assign users to the queue. Alternatively, you can assign a Group of users to a queue. Then click Save.

Assigning Rules for Lead Queues

Now that you've built the queue, it's time to create rules to automatically assign users to the appropriate region/territory queue. Go to Setup> Customize> Leads> Assigning Rules and click "New". In Assignment Rules, you can prioritize the order of rules.Assigning Rules will run on an ascending order.

  • First, set the order of the rule.

  • Enter the criteria on which you want the Assignment Rule to apply
  • If you have multiple rules and depending on the logic, you can modify the logic of the rules right under the list.

  • Finally, enter the name of the queue you want the records with the above criteria to go to. click Save.


And now, users assigned to the queue can have a list view of all records in that queue where they can assign ownership accordingly. Only those users and users of higher hierarchy will have access to that list view.


Salesforce: Create Your Own App Marketplace

Last month, Salesforce announced Private AppExchange-- a feature that lets managers create a private app marketplace with a specific selection of apps totally private to their organization.

Click to read more ...


Salesforce Page Layout

In SalesforcePage Layouts allow for customization of UI pages for users in your organization. And often times throughout, you have pages of objects that might contain items that are not used by many users. Today, I'm going to explain how you can customize a Salesforce page layout with the fields, related lists, and custom links that suit your organization's use.

In the example below, I'll change the layout of the page Contracts. Note that you can "Edit" the current page layout of that object, but it's best to create a new Contract Page Layout and then assign it the default one.

  • To get started, go to Your NameSetupCustomizeContractPage Layout> Click on New.

  • Fill in the appropriate information and click "Save".

  • Next, drag and drop the fields, buttons, and related lists you want on the Contract Page Layout.

  • Then, click "Save".

Assigning Page Layouts

Now that you've created the page layout, you can assign it to the appropriate user Profiles in your Salesforce org.

  • Go back to the "Contract Page Layout" and click "Page Layout Assignment".

  • Pick the profile and assign the appropriate Page Layout. Then, click "Save".