Sunday, 15 March 2015

Best Practice for writing Triggers

        Hello everyone, Today I'm gonna give you some tips for writing triggers. First of all, What does it mean of Best Practice? It means that it's the ideal way to write triggers.

So let's get started with Best Practice for writing Triggers.
.
Since the day I wrote my first trigger,  I have written thousands of triggers. In this blog I'll discuss what should be taking care of while writing triggers and what could we avoid in that.

- Triggers are always executed when some DML event occurred like insert, update, delete, upsert, undelete etc.
-  It would be good practice in writing triggers that we can put all the trigger logic into some other class and call that class from trigger.
- So our trigger will contain only 2-3 lines of code which calls the method of that particular class which has trigger logic in it.

This would be more clear if I give you one example here.

- Assume that I created one custom field named testName__c of string datatype in standard object Account. Now I need that field to be populated whenever new account is created and that field value should be updated from account name. So the trigger for fulfilling that requirements is following.


Trigger Code :


Now, This trigger is very short and simple. Sometime you need to write a trigger which contains very complex logic and more lines of code. In that case, It's not best practice to write a trigger logic in trigger.

Following is code for the same trigger after moving its logic into class.


Trigger Code :



Class Code :


We saw that how to write trigger logic into class. But there is something more important that needs to be taken care of while writing triggers. And that is :


- DML
- Nested For Loop
- SOQL query


As we know that salesforce has some governor limits, That means, we can't use more than 100 soql queries and many more limitations like that. For avoiding some errors and taking care of this governor limits, we must have to take some precaution while writing trigger code.


1) DML statement is very important in trigger. Most of the triggers are written for bulkified operation. So, while writing trigger, we must have to take care of that we don't use DML operation in for loop or it can't be executed more than one time.

Following is the example. We consider the example which is given above. But instead of before, we'll use after insert. So we can use DML statement. It's just for understanding.

Trigger Code :


Class Code :


Now, here we used "update" operation in for loop which is highly unaccepted in trigger practice. This will definitely give you error if the list of account would be more than 100. What's the best practice then for using DML statement. Following code is improved code. Please look at it.


Trigger Code :


Class Code :


You can see what I did here. I made one empty list of the data-type list then just simply add all the accounts in that list which needs to be updated. And then simply applied DML operation for that particular list out of for loop.

2) Nested For loop is also not a good solution for trigger. 
- Although some developers use nested for loop in trigger. But I'll suggest not to do so. 
- If you do not find any solution for the problem and the only way to solve it is by using nested for loop, only then and then you can make a exception by using nested for loops. Otherwise find other ways to solve it. 
- I'm not showing any example for this but I hope you get my concept.

3) SOQL queries are very important while using trigger. 
- Queries can be used in most of the triggers. But as I mentioned earlier that salesforce has some of its limitations. SOQL query is one of them.
- We can't use more than 100 soql queries for one organization. So that's the best practice to use minimum soql queries. 
- But we get into trouble when we wrote only one query but it's been executed more than 100 times, that'll create problem for us. 
- You can guess, how is it possible that one query can be executed more than 100 times! 

Yes, you guessed right, In for loop. If we use soql query in for loop then it'll definitely give us error. 
For avoiding this kind of problem, create one list out of for loop and then fill that list up by using that query and use that list in for loop. That's the best practice.


Trigger Code :

Here we can see that query is being used within for loop so everytime it comes for new record, a query will be executed. For avoiding this, Check the following code.


Trigger Code :

At last, I'll give you summarized topics on which you have to look after while writing triggers.


- There should only be one trigger for each object.
- Use a consistent naming convention including the object name (e.g., AccountTrigger)
- Avoid complex logic in triggers. To simplify testing and resuse, triggers should delegate to Apex classes which contain the actual execution logic.
- Trigers should be "bulkified" and be able to process up to 200 records for each call.
- Use Collections in SOQL "WHERE" clauses to retrieve all records back in single query
- Bulkify any "helper" classes and/or methods.
- Execute DML statements using collections instead of individual records per DML statement.

I hope this helped you. Next time, I'll come up with more interesting and helpful topic for you guys. Till then, Have a nice coding. :)

2 comments:

  1. Introduction of cloud servers have created a greatest impact over data interpretation. Also they made a history in cloud computing. Handling data made much easier and more comfortable like never before. Your content tells the same about the emerging technology. Thanks for sharing this information in here.

    Salesforce training in chennai | Salesforce developer training in chennai

    ReplyDelete
  2. Cloud servers are the best in safe guarding one's information thorugh online. Without this dedicated methodology many companies would have not existed at all. The same though has been furnished above. Thanks for sharing this worth while content in here. Keep writing article like this.

    Salesforce Training | Salesforce training institutes in chennai | Salesforce crm training in chennai

    ReplyDelete