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
Mike MacLeodMike MacLeod 

Need some help with a trigger for loop

Hi Everyone!

 

I am writing an apex trigger. The trigger is designed to fire when a revenue event is created. The trigger is suposed to perform a query on Accounts to find an item called Commission Calculation. If the Commission Calculation is active, the trigger gets the Sales Agent's ID and the Commission Calculation ID, which it uses to populate fields in the Commission record. 

 

My problem is I can't see to find a way to get the For loop to operate correctly. Below is the trigger in all its glory. If anyone can help it would be deeply appreciated!

 

trigger GenerateCommission on Revenue_Event__c (after insert) {

For (Revenue_Event__c NewRevenueEvent : Trigger.new){

If (NewRevenueEvent != null)
{
list <Account> CommissionList = [SELECT Id,
(SELECT id, Sales_Agent__C FROM Commission_Calculations__r WHERE Active__c = TRUE)
FROM Account];

Integer i = CommissionList.size();

list <Commission__c> NewCommission = new list <Commission__c>();

for (Account j : NewCommissionList) {
NewCommission[0].Revenue_Event__c = NewRevenueEvent.id;
NewCommission[0].Sales_Agent__c = CommissionList[0].Commission_Calculations__r.get(0).Sales_Agent__c;
NewCommission[0].Commission_Percentage__c = CommissionList[0].Commission_Calculations__r.get(0).id;
}
// Insert NewCommission[0];
}

}
}

Best Answer chosen by Admin (Salesforce Developers) 
HaroldHarold

There's a few things wrong here.
1. Your query for account has no where clause so it's going to return all accounts and
their corresponding Commission_Calculations__c objects
2. You query for account is located inside your trigger loop. This is won't pass sf bulk
testing.  You should loop through the trigger and get all account id's you need and then query then and loop through maps or sets.

3. You will also need to remove the update from the loop.
4. Your inner loop needs to be something like

for(Account j: CommissionList){
for(Commission_Calculations__c cc : j.Commission_Calculations__r){
//code here
}
} // this is assuming that your relationship between account and commission_calculations is a
// parent child relationship.

 

Let me know if this helped.

All Answers

digamber.prasaddigamber.prasad

Hi,

 

Before giving any suggestion, I would like to know the relationship between Revenue_Event__c,  Account, Commission_Calculations__c and Commission__c.

HaroldHarold

There's a few things wrong here.
1. Your query for account has no where clause so it's going to return all accounts and
their corresponding Commission_Calculations__c objects
2. You query for account is located inside your trigger loop. This is won't pass sf bulk
testing.  You should loop through the trigger and get all account id's you need and then query then and loop through maps or sets.

3. You will also need to remove the update from the loop.
4. Your inner loop needs to be something like

for(Account j: CommissionList){
for(Commission_Calculations__c cc : j.Commission_Calculations__r){
//code here
}
} // this is assuming that your relationship between account and commission_calculations is a
// parent child relationship.

 

Let me know if this helped.

This was selected as the best answer
Mike MacLeodMike MacLeod

Hi and thanks for responding!

 

Commission Calculation is a detail record of account. 

 

Revenue Event is also a detail record on Account 

 

Commission is a detail of Sales Agent

 

Does that help?

Mike MacLeodMike MacLeod

Thank you very much for your reply! I will do as you suggest and let you know. If you lived in Toronto I'd bake you a pie. :)

Mike MacLeodMike MacLeod

I've cleaned up the code considerably, the only problem is, it doesn't actually show the commissions! I can tell the trigger is firing but nothing happens! 

 

Here's the much cleaned up trigger:

 

trigger GenerateCommission on Revenue_Event__c (after insert) {

 

list <Account> CommissionList = new list<Account>([SELECT Id,

(SELECT id, Sales_Agent__C FROM Commission_Calculations__r WHERE Active__c = TRUE)

FROM Account where Id IN :Trigger.newMap.keySet() ]);

 

List<commission_Calculation__c> FinalCommissions = new list<Commission_Calculation__c>{};

 

For (Revenue_Event__c NewRevenueEvent : Trigger.new){

 

for(Account j: CommissionList){

for(Commission_Calculation__c cc : j.Commission_Calculations__r){              

              Commission__c NewCommission = new Commission__c();

              NewCommission.Revenue_Event__c = NewRevenueEvent.id;                      

              NewCommission.Sales_Agent__c = CommissionList[0].Commission_Calculations__r.get(0).Sales_Agent__c;

              NewCommission.Commission_Percentage__c = CommissionList[0].Commission_Calculations__r.get(0).id;

              FinalCommissions.add(cc);           

            }    

 

 

       }

Insert  FinalCommissions;

}

 

 

Any help would be deeply appreciated!!

HaroldCHaroldC

list <Account> CommissionList = new list<Account>([SELECT Id,

(SELECT id, Sales_Agent__C FROM Commission_Calculations__r WHERE Active__c = TRUE)

FROM Account where Id IN :Trigger.newMap.keySet() ]);

 

I think the above code is wrong.  From you code it looks like it running on the after insert of the the Revenue_Event__c object which means the trigger.new.keyset() is the id's of the revenue_Event object.  You'll need to loop through something like this

 

set<id> setAcct = new set<id>();

 

for(Revenue_Event__c re: trigger.newmap.values()){

   setAcct.add(re.Accountid__c); // or what ever the field name in your revenue event object that contains your account id.

}

 

then in your select statement for account use id =: setAcct.