function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MmehtabMmehtab 

Need help with bulkifying the following trigger!

Total newbie to apex. I seem to able to test this trigger fine, but it still runs into governer limits while deploying. Please suggest what can be done to improve the code. 

trigger CheckDeptAllocations on Expense_Report__c (before update) {

    for( Expense_Transaction__c ExpTrans:[SELECT Id, Expense_Amount__c, Allocated_By_Department__c,Amount_Equals_Department_Allocations__c,Expense_Report__c FROM Expense_Transaction__c
                                          where Amount_Equals_Department_Allocations__c=False and Allocated_By_Department__c>0 and Expense_Report__c in: Trigger.new]){
                                          
    If( ExpTrans.Id !=NULL && ExpTrans.Expense_Report__c!= NULL){
    for (Expense_Report__c e: Trigger.New){
        // Check where report status is 'Extracted for Payment'
          if(e.Report_Status__c=='Extracted for Payment'&& ExpTrans.Allocated_by_Department__c>0 && ExpTrans.Amount_Equals_Department_Allocations__c==False) {
                                e.addError('Please make sure all Department Allocations equal the Expense Amount on transactions.');
                return;
           
    }
        
    }
}
}
}
SantoshChitalkarSantoshChitalkar
I can not see anything wrong with this code. Which eception or Limit exception you are getting ??
Patcs_1Patcs_1
Hi

I guess you should add the codition inside the second for loop, like below.

trigger CheckDeptAllocations on Expense_Report__c (before update) {

    for( Expense_Transaction__c ExpTrans:[SELECT Id, Expense_Amount__c, Allocated_By_Department__c,Amount_Equals_Department_Allocations__c,Expense_Report__c FROM Expense_Transaction__c
                                          where Amount_Equals_Department_Allocations__c=False and Allocated_By_Department__c>0 and Expense_Report__c in: Trigger.new]){
                                          
        If( ExpTrans.Id !=NULL && ExpTrans.Expense_Report__c!= NULL){
            for (Expense_Report__c e: Trigger.New){
                if(e.Id == ExpTrans.Expense_Report__c)
                {    
                    // Check where report status is 'Extracted for Payment'
                        if(e.Report_Status__c=='Extracted for Payment'&& ExpTrans.Allocated_by_Department__c>0 && ExpTrans.Amount_Equals_Department_Allocations__c==False) {
                                e.addError('Please make sure all Department Allocations equal the Expense Amount on transactions.');
                        return;
                        }  
                }
        
            }
    }
}
}

If the above is not working, you can use like below,

trigger CheckDeptAllocations on Expense_Report__c (before update) {

    for( Expense_Transaction__c ExpTrans:[SELECT Id, Expense_Amount__c, Allocated_By_Department__c,Amount_Equals_Department_Allocations__c,Expense_Report__c FROM Expense_Transaction__c
                                          where Amount_Equals_Department_Allocations__c=False and Allocated_By_Department__c>0 and Expense_Report__c in: Trigger.new]){
                                          
        If( ExpTrans.Id !=NULL && ExpTrans.Expense_Report__c!= NULL){
           if(Trigger.newmap.containskey(ExpTrans.Expense_Report__c))
           {
              if(Trigger.newmap.get(ExpTrans.Expense_Report__c).Report_Status__c=='Extracted for Payment'&& ExpTrans.Allocated_by_Department__c>0 && ExpTrans.Amount_Equals_Department_Allocations__c==False)
              {
                         e.addError('Please make sure all Department Allocations equal the Expense Amount on transactions.');
                        return;
              }
            }
    }
}
}

Please let us know if any of the solutions works.

Thanks
doravmondoravmon
I have this similar problems before,  you just need to change a little here:
*****Don't use 'select....from...' in a for(x:y) loop
so what I will do is save the id first, then query, then for loop again ~~
Hope this modify will solve your problems~~^_^

trigger CheckDeptAllocations on Expense_Report__c (before update) {
Set<Id> ids = new Set<Id>();
for(Expense_Transaction__c e : Trigger.new)
{
    ids.add(e.Id);
}

Expense_Transaction__c[] queryList = [SELECT Id, Expense_Amount__c, Allocated_By_Department__c,Amount_Equals_Department_Allocations__c,Expense_Report__c FROM Expense_Transaction__c
                                          where Amount_Equals_Department_Allocations__c=False and Allocated_By_Department__c>0 and Expense_Report__c in: ids]
    for( Expense_Transaction__c ExpTrans : queryList)
{
                                          
    If( ExpTrans.Id !=NULL && ExpTrans.Expense_Report__c!= NULL){
    for (Expense_Report__c e: Trigger.New){
        // Check where report status is 'Extracted for Payment'
          if(e.Report_Status__c=='Extracted for Payment'&& ExpTrans.Allocated_by_Department__c>0 && ExpTrans.Amount_Equals_Department_Allocations__c==False) {
                                e.addError('Please make sure all Department Allocations equal the Expense Amount on transactions.');
                return;
           
    }
        
    }
}
}
}