Entries in apex (70)


Debugging Apex Made Easier in Winter '16

With the release of Winter '16, Salesforce has taken a big step forward in making it easier to debug Apex code. One of the hard things we have all had to deal with is that debugging Apex is very much a "print line" exercise up to now. 

The Apex Debugger in Eclipse is now being released for general availability. It behaves very similar to debuggers from other languages for those of us who started out in Java, C++, C# etc. I've used it in my sandbox and I am able to set a breakpoint, start a debugging session and determine root causes.

Features & Uses


  • Set breakpoints in Apex classes and triggers
  • View variables, including sObject types, collections, and Apex System types.
  • View the call stack, including triggers activated by Apex Data Manipulation Language (DML), method-to-method calls, and variables
  • Interact with global classes, exceptions, and triggers from your installed managed packages. When you inspect objects that have managed types that aren’t visible to you, only global variables are displayed in the variable inspection pane.
  • Complete standard debugging actions, including step into, over, and out, and run to breakpoint
  • Output your results to the Console window




What counts toward Salesforce CPU limits?

Once in a while you we all hit the following Apex CPU time limit exception. Example

System.LimitException: Apex CPU time limit exceeded


Most of the time when I see this and dig into it I'll find things like FOR loops inside of FOR loops (i.e. dream within a dream) or tons of workflows/processes being run before the code that threw the exception was even hit.

To start with let's just cover what counts against this CPU timer and why it is there. The CPU timer is there because Salesforce is a multitenant system. It has to serve many customers on the same servers so the governor limits are there to make sure one customer or block of code is not using up all the resources.

Counts Doesn't Count
Any Apex Code HTTP Callouts
Workflow Execution Database operations, e.g. DML, SOQL
Library functions exposed through Apex SOSL
Any time spent evaluating formulas for validation/workflows


As I said before, one of the biggest trouble spots I often run into is poorly written logic in formulas that eat up time.

One cool thing is that you can open the Developer Console and click on a specific execution. Click on one of the log entries and then switch the perspective to Analysis.

Salesforce Developer Console Switch Perspective

Next you will get to see some nifty tables, charts and graphs that will help you to really see how much of your CPU time is being used and being used by what.

Click to expand (Salesforce Dev Console Execution Timeline)


One great resource to have in your back pocket is if you don't need the code to execute at a given instance run it in a batch. i.e. Database.Batchable



APEX: Retrieve a Custom Object Field's History

Let's say you have the field "Address" in a custom object labeled "Customer" and you're looking to obtain that particular field's history and tracking details along with the dates on which changes were made. Today, I'll share a query that will help you do just that:


SELECT ParentId, OldValue, NewValue, Field, CreatedById, CreatedDate FROM Customer_History where parentId = :CustomerId and Field ='Address__c'

What Salesforce Objects Support Apex Triggers?

If you’re running Unlimited, Developer, Enterprise, or editions of, you can use Apex Triggers to execute before or after a database insert, update, delete, merge, upsert undelete, or any combination thereof. allows for Triggers for many standard/system and custom objects. However, not all System Objects support Apex Trigger. Below is a list containing commonly used System objects:




Import Your Data into Salesforce



The right way...

Most new Salesforce users have existing data in legacy systems. An important milestone when first using Salesforce is to properly import your data. The initial data migration to Salesforce is a one-time effort—so be sure to get it right the first time! Below are four steps to import your data into Salesforce:

1. Pick The Proper Migration Tool: Salesforce CRM provides two tools for importing data.

  • Salesforce Import Wizard: this tool is aimed for the “general user”. The import Wizard allows you to import up to 50,000 records at a time. It also does an initial scan of duplicates.
  • Salesforce Apex Data Loader; however, is designed for the heavy/technical user. This tool not only provides you with the ability to mass Upload, Update, and Delete of records into your Salesforce org but also to schedule imports/export of data. Keep in mind that this tool is only available to Enterprise and Unlimited Editions of Salesforce.
  • Use Cloudingo: Cloudingo has the ability import large batches of records into Salesforce without creating duplicates. You can filter on many different standard/custom fields.


2. Prepare for The Import: the Salesforce Import Wizard is designed to work with CSV files in                      Microsoft Excel.

  • Collect and organize your data into a single CSV Excel file.
  • Review the Excel file and make sure all data fields are properly mapped to corresponding Salesforce fields.
  • Assign the right owners of the records. If an owner is not assigned, Salesforce will assign the person importing as the owner.


3- Test and Validate your Data:

  • Before committing to importing all of your data, you might consider doing an initial test of importing 10 records
  • Create a report and make sure the data is mapped correctly and that it matches the original data in the Excel file.
  • Make necessary changes to your records and test again; if all goes well, proceed to importing your records


4- Analyze and Clean your Data: it is highly critical that data is clean and duplicate-free. There are many Data Quality tools in the Salesforce Appexchange; my favorite one is Cloudingo (mentioned in Step 1). Cloudingo not only scans and dedupes your entire database (including Leads, Accounts, and Contacts), but also gives you the ability to decide what determines the “winner/master record” and then merge the losers into it. For example, do you want the winner to be the oldest record, the newest, or the most populated one? Don't take my word for it though, they even offer a free trial that will let you see how the product works and shows you a report of your duplicate records, too.


Executing Anonymous Apex Inside Eclipse

One of the most underutilized programming techniques in is the ability to execute a snippet of apex code "anonymously". I know, it has a funny ring to it. Anonymous code is basically just a block of code that doesn't get stored inside This is nice, because often times you just want to try part of a method out or execute a SOQL statement without going through the hassle of adding it to a trigger or a class. 

This is a great technique to quickly evaluate or debug a statement of code on-the-fly. There are a couple of ways you can do this.


  1. Open up the Developer Console inside Salesforce
  2.  The executeAnonymousWeb services API call
  3. Inside Eclipse/ IDE

I typically do this inside Eclipse so I thought I would quickly highlight how to do that and include a screenshot as a guide. When you have the IDE plugin installed in Eclipse, all you have to do is navigate to the Execute Anonymous tab. If you don't see the tab, make sure you've got the perspective open (opens by default when you create a new project). 

Inside the tab, you'll see a dropdown for the active project, a log category dropdown (apex code, apex profiling, callout, database, validation, workflow) and a log level slider.

Below those settings, you'll see a "Source to execute" textbox. Here is where you will actually type your code snippet. 

Now all you have to do is run it by clicking "Execute Anonymous". If there are any syntax errors, you'll see those below in the results window. If the code compiles, it will run and you'll see the output below. Don't get discouraged if the output looks a little verbose and hard to read. You can always adjust the slider over to the left and show less information. 

Execute Anonymous Apex in Eclipse (Click to Magnify)For the specifics on what type of code you can execute anonymously, check out the documentation here.







Connect Salesforce to Other Apps

Kapow Software released its latest cloud integration recently which lets users connect Salesforce - or any app - with any web-based data or business provider. Kapow Katalyst, as its called, allows for seamless sharing and automation across various platforms. And it all works without any coding. 

As Salesforce becomes more and more critical to the handling of various (and growing in number) business processes the need to integrate Salesforce with outside systems becomes necessary. Kapow has the right idea. 

Stefan Andreasen, CEO of Kapow Software, explains the need for this update well:


“We hear from customers that as Salesforce pushes deeper into their enterprise, the need for real-time, self-service integration between Salesforce and external web apps and websites is becoming a must-have, not simply a nice-to-have."


Beyond integration, the other major service the update offers is automation. The Kapow Katalyst Apex Generator allows for automation between any app and any other web-based application. This feature allows Salesforce, or any other application to become the center and focus of many different business processes. 

The best part of all this is that no coding is required. These applications bypass the Apex needed to connect any outside data or business processes to Salesforce. Kapow is simply streamlining the process. 

Overall, I'm pretty excited about this update. The need to automate and integrate various platforms is the key to productivity and doing it without code is pretty handy.


Data Quality: Case in Point

The motivation for this blog post is a case in point example, so I'm going to go ahead and give you the story first and I think you will understand immediately why I'm so frustrated. For the purpose of this exercise, I'm leaving out all names and identifying traits of the people involved. I'm sure that most of you have experienced a similar situation, so feel free to chip in with comments.

I received an email two days ago from the sales director at my company. He went into detail about a conflict that came to his attention early in the week with two sales representatives, who I'm going to call Tom and Jerry. Tom made a phone call to a customer that he had made initial contact with two months ago and had entered into our Salesforce database. He was confused when the customer told him that he had just talked to a rep at our company named Jerry, and had actually decided to go ahead and buy one of our products.

At this point, Tom was both frustrated and irritated. He had embarrassed himself in front of a customer and lost out on his commission. He immediately went to the sales director with his complaint, who immediately emailed me. So I was left with a problem: how had Tom and Jerry both been assigned the same customer by the Salesforce system?

I found my answer fairly quickly. Every weekend, our Salesforce account does a mass import from our internal database containing all of our customer information. At some point, the customer in question had his phone number changed. When the system did it's weekly import, instead of recognizing that the number had changed, it went ahead and created a completely new account under the duplicate information with a different phone number. Tom was assigned 'Original Customer' and Jerry was assigned 'New Customer' and I ended up with a two-salesperson pileup.

I know that I've shared a lot of my thoughts on data quality with you before, but I don't think it can be emphasized enough. Without maintaining strict protocols for data import as well as entry, conflicts like this can arise fairly easily.


Steps Toward Cleaner Databases

One of the most consistent problems I face in my work is getting members of our sales team to fully and accurately fill out the required fields on This lack of attention to detail has left entries into our database in a patchy condition at best. Some accounts are fully filled out, but nothing is a bigger bane on the efficiency of our sales team than a partially completely lead form.

However, by taking a two simple steps as a Salesforce Admin, you can start to really cut down on the amount of incomplete data that is entered into the system.

1. Be sure that you have set up all fields appropriately for the lead form. If you plan on transferring the information from the lead form over to the account page, be sure that the fields on both pages match up.

2. Next, set up validation rules. By creating efficient validation rules, you can make sure that the user enters in the required data before they are allowed to save the record or change the status of the lead.

Important things to consider: When you are creating the validation rules for the lead forms, think carefully about which fields you want to be required. For example, if the salesperson makes initial contact over the phone, it makes sense to require that the telephone number field is filled in. You have to be careful though, because validation rules prevent the user from saving the information without all of the required fields filled in. What information is it likely that the sales rep can acquire over the phone? What is unlikely?

Hope this helps! If you aren't already, Follow Me On Twitter!