Meaningful Error Messages #94

This one is a hoot, Error: java.sql.SQLException: ORA-00001: unique constraint (CORE.AKAPEX_CLASS) violated ORA-06512: at “SNEEZY.CAPEX”, line 505 ORA-06512: at line 1 : {call cApex.update_class(?,?,?,?,?,?,?,?,?,?,?,?)})} Sneezy.Capex, what a cute name. The number of question marks in the error message scares me a bit so I may take an early lunch and try to forget about it. Solution pending.

Handling System.QueryException

When attempting to fetch a single record from the database it is easy to run into the above exception. I’ve seen two schools of thought in dealing with this issue – mostly in the forums – and have been hoping to find a more official standpoint.

Read more

How to avoid Governor Limits [Part 1 of N]

This topic is waaaay too big to cover in one post, so for now I’m going to concentrate on avoiding this particular exception: ‘Too many SOQL queries’. I will also touch on the ‘Too many DML rows’ exception, and expand on it’s solutions in another post. Bulkifying your triggers is another topic I will cover at some later date, but the methodologies mentioned here should go some way towards helping you avoid some of the Governor Limits there too.

Read more

‘With Sharing’ Keyword

Recently I came across a rather confusing situation with my Force.com application. I had whittled down my profiles; implemented a marvelous role hierarchy; and a number of sharing rules, and although my application was functionally restricted i.e. object-level access, field-level access etc., the data didn’t seem to be restricted by the sharing model i.e. I could see data created by user A when logged in as user B, even though I had explicitly disabled this behaviour.

The application I’m developing may not be the same or even similar to yours, especially since I don’t use any standard pages. I suspect that if I had use more of the standard functionality I may have guessed at the solution a bit earlier.

Read more

SOQL ‘Distinct’ Keyword

It doesn’t exist. Sad but true.

In SQL there is a clause that ensures a duplicate free result set is returned by any query you issue e.g. You want to retrieve a duplicate free list of first names from the table clients. The column holds the following values: Alice, Bob, Alice. In Oracle SQL you would use the following query (and it would return the names Alice and Bob each listed once).

SELECT DISTINCT firstname
FROM clients;

Unfortunately SOQL doesn’t have this capability although there is an Apex trick that can be employed as a work-around.

Read more

Force.com Anonymous Blocks

Anonymous Blocks. Where have you been all my life? Right under my nose apparently. I am embarrassed to say I’ve only discovered this little jewel of the Force.com platform recently, and although I’ve managed without it, I’m pretty sure that it’ll be a regular feature in my development experience.

What are they you ask? It’s simply the ability to execute a bit of Apex code on the fly. This is awesome^6 at least. Often during the development process you need to clean your data. Either you’ve changed your logic and that data is affecting functionality, or perhaps you need to remove data en masse. This is simple with Anonymous Blocks, just write up a short script that will restore your data to an ‘initial’ state and save it for future use.

Read more

Salesforce Savepoints

Transaction control is an important part of any system that interacts with a database and Salesforce has neat ways of implementing said control.

Anyone that’s worked with SQL databases will be familiar with savepoints and rolling back, and Salesforce has implemented similar constructs. For those who haven’t heard of these terms wikipedia describes them as

savepoint is a way of implementing subtransactions (also known as nested transactions) within a relational database management system by indicating a point within a transaction that can be “rolled back to” without affecting any work done in the transaction before the savepoint was created.

rollback is an operation which returns the database to some previous state.

As with most things, the importance of these two features is best demonstrated using examples.

Read more

Throwing Custom Apex Exceptions

Throwing custom exceptions is something that most Java developers expect to be able to do when working with Apex code, and indeed it is possible. It isn’t very well documented, but I managed to stumble across this solution after some experimentation. Usually, I would expect an exception to be thrown using syntax at least similar to: However this syntax will give the follow error: Type cannot be constructed: Exception Instead you have to create a new exception class with extends the Apex Exception class: And then throw that new exception: That’s just the beginning of course, you could create some pretty fancypants error handling in your custom exceptions. The world is your oyster, now all your need is some Tabasco and lime.

Dynamic Custom Labels

I know what you’re thinking, ‘this is cRAzY talk’. But do not doubt my friend, Salesforce has a very powerful translation framework.

Besides being able to change the language used by the Salesforce standard areas with the flick of a picklist, you can include translatable Custom Labels in most of your custom application. To do this you will need to request that the ‘Translation Workbench’ feature be enable for you Salesforce development environment, after which you will find new areas in your ‘Setup’ menu.

Post enablement you can then create a Custom Labels and include them where necessary within your code. This can be done within a VisualForce page like so

Read more