You need to sign in to do that
Don't have an account?
Srinivas223
Implementing Best Practices in Triggers
I know writing Queries inside FOR loop is not a best practice. But not getting an idea to avoid it. I appreciate if anyone could explain me how I can avoid writing it here.
Thank you.
trigger IMRORevisionTrigger on iMROrevision__c (before insert, before update) {
list<string> mvssArray = new list<string>();
if(trigger.isUpdate && (trigger.isafter || trigger.isbefore)){
list<iMROrevision__c> LstImroRev = [select aircraft__c,NumofInvoices__c from iMROrevision__c where id in : trigger.new];
system.debug('-----' + LstImroRev);
for(iMROrevision__c lstimr : LstImroRev){
list<aircraft__c> lstAir = [select id,
(select id, MVSS_Contact__c from Aircraft_Relationships__r),ServiceCenter__c from aircraft__c where id =: lstimr.aircraft__c];
for(aircraft__c ac : lstAir ){
system.debug('the aircraft id----' + ac.id);
for(Aircraft_Relationship__c acRel : ac.Aircraft_Relationships__r){
system.debug('the mvss con 0000' + acRel.MVSS_Contact__c );
if(acRel.MVSS_Contact__c != null)
mvssArray.add(acRel.MVSS_Contact__c);
}
system.debug('the size 8888 ' + mvssArray.size());
}
}
}
Thank you.
trigger IMRORevisionTrigger on iMROrevision__c (before insert, before update) {
list<string> mvssArray = new list<string>();
if(trigger.isUpdate && (trigger.isafter || trigger.isbefore)){
list<iMROrevision__c> LstImroRev = [select aircraft__c,NumofInvoices__c from iMROrevision__c where id in : trigger.new];
system.debug('-----' + LstImroRev);
for(iMROrevision__c lstimr : LstImroRev){
list<aircraft__c> lstAir = [select id,
(select id, MVSS_Contact__c from Aircraft_Relationships__r),ServiceCenter__c from aircraft__c where id =: lstimr.aircraft__c];
for(aircraft__c ac : lstAir ){
system.debug('the aircraft id----' + ac.id);
for(Aircraft_Relationship__c acRel : ac.Aircraft_Relationships__r){
system.debug('the mvss con 0000' + acRel.MVSS_Contact__c );
if(acRel.MVSS_Contact__c != null)
mvssArray.add(acRel.MVSS_Contact__c);
}
system.debug('the size 8888 ' + mvssArray.size());
}
}
}
Please check following code once :
Hope this helps you!
Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
All Answers
2) Logic-less Triggers
3) Context-Specific Handler Methods
4) Bulkify your Code
5) Avoid SOQL Queries or DML statements inside FOR Loops
6) Using Collections, Streamlining Queries, and Efficient For Loops
7) Querying Large Data Sets
8) Use @future Appropriately
9) Avoid Hardcoding IDs
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_bestpract.htm
https://developer.salesforce.com/page/Apex_Code_Best_Practices
https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices
Please check following code once :
Hope this helps you!
Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com