Tuesday, June 4, 2013

Five things to do while writing an apex code to prevent pulling your hair out later.

A new intern joined the team excited to learn about Salesforce. She had a little Java background and thought that Salesforce was very easy compared to Java. In her eagerness, she wrote a 20 lines of code to prove her skills and grasp on Salesforce. The problem was, the code could be written in just 3 lines.

When you are coding on Salesforce, you have to keep in mind the following things:

i) If you can save some lines of code, save it, you can use those lines later. 
If you can write a code in 5 lines, don't waste a 6th one. That one line you saved could be the difference between spending a weekend in office optimizing the code or going to a party at the weekend.

ii) Write like the code will run forever.
Yes, your Sales team has taken in writing that there will only be three records in the system. Yes, you have written a good code for that. But when you are dealing with Salesforce, remember that data is going to increase over a period of time. Today you may be processing a 100 records but there will be a time when you might have to process 1000 or 100000. That period might be beyond your warranty support but it is going to come. You cannot write a prophecy about the day your code broke. Bulkify your code to handle as much data as you can. Put a check if the data is beyond your limit and print a decent, good error message for the future developer to understand what is wrong.

iii) Dread the governor limits
You may hate them, you may love them but you cannot ignore them. Salesforce governor limits are the harsh reality of this world, just like it will rain when you forget your raincoat at home. The governor limit are going to show their face just when you are about to close your day. Write a code to avoid the limits. Have you heard of a developer who wrote such an efficient code that no Governor limit was broken? Neither have I. But you can try to be as close as possible to the legend.

iv) SOQL/DML should not be allowed to enter a FOR loop
This rule is Salesforce 101. No DML or SOQL statement should be inside for loop, even if you are running the loop for only two records.

for(Contact c: ContactList){
//THIS IS A NO NO NO
account a = [Select id,name from Account where id=:c.accountid];
c.name=c.name+a.name;
//THIS IS A EVEN BIGGER NO
update c;  
}

The code written above is a recipe for disaster. Instead the same code can be optimized as:

for(Contact c: ContactList){
accountSet.add(c.accountid);
}
//Whuuu huuu not inside any loop
Map<id,Account> accountMap = new Map<id,Account>([Select id,name from Account where id=: accountSet]);
for(Contact c: ContactList){
//Like they say in treasure hunt movies, a map comes in handy.
c.name=c.name+ accountMap.get(c.accountid).name;
}
update ContactList;

v) Use Collections Set, Map wherever possible
If in your code you have to loop inside a loop, make sure to use a map instead of a list for the inner loop. When we use a map, it is easier to fetch data and will save you a lot of time. Using map also helps speed up the code.

for(Contact c: ContactList){
for(account a: accountList){
if(c.accountid==a.id){
//do something awesome
}
}
}

This code is good enough but can be optimized even more like this

for(Contact c: ContactList){
if(c.accountid==accountMap.get(c.accountid)){
//do something awesome
}
}


These are five simple things can will make your life easier. Being a Salesforce developer is a great honor, use it wisely. Use it fairly.