Trigger Scenario 8 : The following Trigger will fires when we try to create the account with same name i.e. Preventing the users to create Duplicate Accounts Object : Account Trigger: Before Insert, Before Update MIND IT ! Here (Way-1), I have used SOQL inside for loop which will affect governor limit and it is not best practice. The most important being that it is not bulkified. We should always avoid SOQL inside loop. So please see second way-2 below for best practices. WAY-1 Trigger Code : AccountDuplicateTrigger.apxt 1 2 3 4 5 6 7 8 9 10 trigger AccountDuplicateTrigger on Account (before insert, before update) { for (Account a:Trigger. new ) { List<Account> acc=[select ID from account where Name=:a.Name and Rating=:a.rating]; if (acc.size()> 0 ) ...
Scenario: Write a trigger on Account to prevent record deletion if Annual Revenue is greater than 50000. If Annual Revenue is more than 50000 it should throw error. AccountRevenueTrigger : Trigger AccountRevenueTriggerHandler: Class
Best Practices in Apex Triggers 1. One Trigger for one object 2. Naming Convention 3. Handler class for your trigger 4. No DML & SOQL inside for loops 5. Use custom label to activate/deactivate your trigger in production
Comments
Post a Comment