Navigation
Connect With Me
Twitter
Tuesday
May222012

How do I escape an apostrophe (single quote) in my SOQL statement?

This is one of those things you don't need to do all that often but when you do, you can't seem to figure out how to make it work. I ran into this earlier today. I had a SOQL statement that kept returing a "MALFORMED_QUERY". 

I started to debug it and realized that it was happening when I was looking for accounts where the name had an apostrophe (single quote) in it.

A quick example would be something like:

L'Oreal

If you want to find all the accounts where the name starts with L'Oreal you would need to escape the apostrophe (single quote). In the Force.com Explorer, all you need to do it put a \ before the apostrophe like so:

Select Id, Name, (Select Id, Name From Contacts) From Account where name like '%L\'Oreal%'

Now, if you are building the SOQL dynamically and you are not sure when/where the single quote could appear, Salesforce has though of it. There is a nice and handy escapeSingleQuotes string method. It takes a string as an argument and returns a string with all the single quote instances escaped. This saved a lot time and guess work.

 

 

Thursday
May102012

Bring on the Java Developers!

We all know that Salesforce is growing; enough to have attracted more than 100,000 customers! But the news is: Salesforce is reaching out to coders in the aim of becoming a host of apps outside the CRM. The executive leading this major shift is Byron Sebastian, who came to Salesforce after the acquisition of Heroku in 2010. This decision came after the staggering rise of subscriptions to 40% to its hosted apps, reported in its latest fiscal year.

This is not the first time Salesforce delved into enterprise Java, a few years ago, Salesfroce teamed up with VMware with VmForce. Another evidence of Salesforce attempting to buy into developers, is its upcoming Cloudforce conference that’s going to be held in London. In that conference, Salesforce will host sessions to help train developers to build Salesfroce’s Chatter apps and other mobile apps. “ I’m really on a mission to have a strong voice for developers on the Salesforce platform,” said Sebastian in a recent interview.

 

Thursday
May102012

Offset Pagination Coming to SOQL in Summer '12

I can't begin to tell you how excited I am about this addition! This is a major performance improvement for those of us trying to go through large result sets returned with a SOQL statement. By giving us the new OFFSET, we can now specify the starting row offset as part of the query results. 

Basically, this is a nice and painless way for us Force.com developers for paging through large result sets and quickly jumping to a particular subset row. As an example, let's say you want to return a list of products to a user in a table and show them 50 at a time with a back/next option to page through the results. When the user skips to page Two in the result set, we want to start with the 51st record in the query result set. Previously we had to jump through some hoops to accomplish that in Ape/SOQL. Now, with OFFSET it's pretty darn easy.

SELECT Name

   FROM VendorProducts__c

WHERE ModelYear__c > '2001'

ORDER BY Name

   LIMIT 50

OFFSET 50

This accomplishes what we were trying to do by showing us a resulting set of 50 records that begins with the 51st record in that query result set. LOVE IT!

Monday
May072012

Select all records created before/after ___ with SOQL

I get this question from time to time. Often times we want to look at all records that were created or last updated before/after a certain date. Here is a quick example of how to select all the contacts created after a certain date:

SELECT Id, FirstName, LastName from Contact where CreatedDate >= 2012-05-07T00:00:00Z

Yes, you have to write the date in that format. And no, don't put the date in quotes.

Tuesday
May012012

Executing Anonymous Apex Inside Eclipse

One of the most underutilized programming techniques in Force.com is the ability to execute a snippet of 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 Salesforce.com. 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/Force.com 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 Force.com 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 Force.com perspective open (opens by default when you create a new Force.com 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 Salesforce.com documentation here.