Apex Trigger Code Need Improvement
Current, MC_Cycle_Plan_Product_vod sobject have two Apex Triggers: 1.VOD_MC_CP_PRODUCT_BEFORE_INSERT have 6 SOQL queries.
2.VOD_MC_CP_PRODUCT_BEFORE_INSERT_UPDATE have 3 SOQL queries.
These two apex triggers directly write SOQL queries in trigger class, so it become a flawed programming pattern.
At single transaction Salesforce can support Total number of records processed as a result of DML statements is 10,000. and Apex trigger batch size is 200, It means if you execute DML statements records number is over 200,it will be executed as each chunk of 200 records,so it will causes a trigger to fire multiple times for chunks of 200 records.so if I add 10,000 records, tirgger will be fired 50 times.so the same SOQL queries will also be queried 50 times. This trigger will encountered uncatchable governor limits error too many SOQL quries 201 error at 34th trigger batch.
I analyzed this sobject apex trigger,6 queries of 3 queries doesn't need business data to query.just need current login user Id,current login user's Profile Id, current login user's language.
1.SELECT Id, PermissionsModifyAllData FROM Profile WHERE Id =: Userinfo.getProfileId()
2.SELECT MCCP_Admin_vod__c FROM User WHERE User.Id =: Userinfo.getUserId()
3. SELECT Text_vod__c FROM Message_vod__c WHERE (Name = 'MCCP_LOCK_MSG' AND Category_vod__c = 'Multichannel' AND Active_vod__c = TRUE AND Language_vod__c =: UserInfo.getLanguage()
current login user's id,profile id,language is constant,so this means these 3 queries's query result is also constant, it doesn't need each trigger batch to query them again.It will just query them only one time at first trigger batch will be just fine.and you can add trigger handler class to bypass each trigger batch to query same SOQL query each time and can reuse same logic to other trigger handler class.I notice sobject MC_Cycle_Plan_Channel_vod__c,MC_Cycle_Plan_Target_vod__c apex trigger queried same 3 result constant SOQL queries.
Apex Governor Limits | Salesforce Developer Limits and Allocations Quick Reference | Salesforce Developers
Please sign in to leave a comment.