Don’t be afraid of a little Apex


EDITED ON 8/6/14: Fixed some of the examples and added some content

In an earlier post I wrote about Workbench and how it can be utilized. Now I want to show you a way to use Apex inside workbench to do deletes or updates very quickly.

Many admins get scared and run away when they hear Apex. I am all for less code and more clicks (meaning use functionality instead of Apex when you can). But some Apex is easy and can help you quickly do things. Workbench allows you to execute Apex without a Visualforce page or calling a trigger.

Executing Apex within Workbench allows you to quickly update or delete data. This is one of the few things that Apex execution can help the average admin with. So I am going to show you how to do these two things and add a bit more to your arsenal.

When executing Apex in this fashion it is called Anonymous Apex. This is because with Anonymous Apex you do not need to deploy any code, you can simply run Apex immediately. Normally with Apex you need to write a class or classes, then a trigger, and then a test case. With Anonymous Apex you simply immediately execute your code. This is handy for quick data tasks, as you will see below. For those that have a database background you will really like Anonymous Apex.

NOTE: Please use Sandbox or Developer Edition when playing with any of these. You can easily delete or change data. VERY VERY VERY important that you always test things in Sandbox or Developer Edition first.

Mass deleting records using Apex:

Go to Workbench (if you need help on this first read up on my write-up about Workbench). Go to Queries–>SOQL Query. Choose your object, click on the ID field, and do any filtering you need. You want to choose records you will be deleting. Below is the example query I will work with.

SELECT Id FROM Account WHERE Type = ‘Big Bank’

Once you are good with the results of the query copy the query it and then head over to Utilities–>Apex Execute.  We will now write the Apex code to delete all the records returned from your query. For my example I am going to delete all accounts that have a type of Big Bank. Don’t be scared. This won’t be as hard as you think it will be.

List BigBanks = [SELECT Id FROM Account WHERE Type = ‘Big Bank’];
delete BigBanks;

That is it. The scariest part is just how easy it was to delete all those records. And you should be scared of that part, just not the Apex code. Use delete in Apex with caution simply because you can mass delete things so easily. This is why Sandbox or Developer Edition is important. Always test in one of those first.

To explain the code in short:

  • List<Account> simply means we are making a list of records. Think of it in those terms.
    • After List you must always put the object, which in this case is Account. This also needs to be surrounded by < >, so it is List
  • BigBanks is simply giving the list a name for reference. Name it whatever you want, just no spaces.
  • The next part is your SOQL query you copied. You can add more filters, if needed.
  • The last step simply says delete the name of your list (2nd bullet in this list).
  • Sometimes your query might return more than 10,000 records. A few ways you can deal with this but since we are going with simplicity the easiest way is to use LIMIT in your query.
    • At the end of your select statement you can add LIMIT 10000.

So not that hard, right?

Mass Updating Records

You can also mass update records using this same Apex Execute. In this example I will  show you how to change all contacts with a last name of Jones to Smith. Not sure why you would ever mass update everyone’s last name from Jones to Smith but it an example that you can hopefully understand.

Again go to Queries–>SOQL Query in Workbench.  Copy and paste the following query. Run it. Do you have anyone with the last name of Jones? If so you will get some results. You can change the filter, if you want, to something more relevant.

SELECT Id FROM Contact WHERE LastName = ‘Jones’

Once you are happy with the results of your query copy it.

Now like the delete example go to Utilities–>Apex Execute . Copy and paste the following code. Change the query, if you want.

List cnts = [SELECT Id FROM Contact WHERE LastName = ‘Jones’];
for(Contact c : cnts){
c.LastName = ‘Smith’;
update cnts;

Another simple one when you see it. This simply is going to update all contacts with a last name of Jones and change the last name to Smith.

Lets explain the different parts.

  • List is the same as the explanation in deletes. Making a list of all the contacts you will want to update.
  • cnts is the name of the list of contacts.
  • For basically loops through the list for you. Without the For it would not go through each record from the list.
  • Contact c : cnt is setting things up for the update. Contact c is saying we will be using the Contact object and c is name we will reference. c could have been anything, just name is c for short for Contact.
  • c.LastName is the field you will update. You can update any field that is updatable on the object. The c. is what says use the Contact object (see bullet above) and then you reference the field name, in this case LastName.
  • Lastly you simply say update listname; . So in this case the list name, set at the beginning, is cnts. So this updates all the records.

Again not so scary.

Little Apex code like this can really speed up some of your admin duties without being a full Apex developer. Don’t be scared and try these things out in a Developer Edition instance first to learn. Just remember that anything you do with Anonymous Apex is immediate and no simple way to rollback. So always do exports before you do this in production.

One thought on “Don’t be afraid of a little Apex

  1. Valentin Schmitt

    I think you missed after “List” in this part

    List BigBanks = [SELECT Id FROM Account WHERE Type = ‘Big Bank’];
    delete BigBanks;

    Thanks for your website by the way

Leave a Reply

Your email address will not be published. Required fields are marked *