Develop for scale

Every developer in Salesforce should know it – we have governor limits. And it means sometimes the code has to be written in different way than you are used to from other platforms. You should be aware of what bulkified is, how to use Batch classes and why you might need Stateful and a few other things.

On my last project, which I had to overtake from other company, I saw that even these basic concepts weren’t followed. But we also run into problems which aren’t so obvious.

Go with standard, please

I didn’t understand the whole project from the beginning. They use communities, but only as a way of authentication and developed every single page themselves in Visualforce. The strange part was, that these VF pages mimic the standard functionality of native templates, but provided just part of those standard features.

So they have a Chatter group but they can only publish post, comment on them and like them. They cannot delete, have problems with posting images or formatted text, completely forget about polls and much more.

They can search the whole system, but only in name fields and in exact wording as SOQL doesn’t really support anything more advance (hey, we have SOSL as well, you know?)

When they create a new custom field it takes a while to add it to pages, views and other places you want to see it.

From what I saw everything could be solved with standard features, the layout can be easily changed (we even have a Trailhead for it) and if they didn’t want to use the standard boring list views they could get something nicer from Dreamhouse App, for example.

Never mind, as a bonus they got tons of problems.

Transient keyword

The biggest problem, which starts to appear everywhere, is View State.

Maximum view state size limit (135KB) exceeded. Actual view state size for this page was …

It isn’t obvious at the beginning. You write the code, do the bulkification, test everything and then deploy to production and really start using it. And as you use it the amount of data grows. And as it grows also the number of rows your SOQL statements returns gets bigger (and you might hit the 50000 returned rows limit) but more importantly the size of your VF variables grows.

And once your variables go over 135KB you are done. And it is pretty easy, just imagine you query for all contacts and want to know their names, companies and email addresses. That’s 135 character per record pretty easily, so when you have more than 1000 records you will hit the limit.

What can you do about it? Use the Transient keyword.

By default Salesforce wants to save some processing time on their server (and also improve the user experience), so it save the state of data it sends to the client, so when you do any operation on the page it can take the data from the view state and act on them. But they also want to limit the size of this overhead, so we have the 135KB limit.

The only way out of this problem is to mark the variables as Transient. It means they won’t be send to the client and you won’t be able to use them in your code later on. You need to do their initialization every time you need them, which might be as easy as create a procedure to set them, which you call from constuctor and all other places where you need this variable as well.

You can check the view state to see which variables are the biggest one and start with them – just enable Development mode in your user record, open Development console and view the page. See An Introduction to Visualforce View State article for more details.

It also show the importance of representative test data, would we have them we might get this problem during development and not in real life.

Next time you develop VF page think about this problem. I will do it as well.

Leave a Reply