+ Start a Discussion
Mike MacLeodMike MacLeod 

Can't get my list to iterate

I'm so close to being done with this trigger, I can smell it. THe trigger works, but when it updates, it creates the correct number of Commission objects. Hoever, it just creates the correct number with itdentical  entriies. 

 

I know this is because somehow I'm not iterating correctly, but I can not figure out the error. HELP!

 

Here's the code: 

 

trigger GenerateCommission on Revenue_Event__c (after insert) {

set<account>DoctorID = new set<account>(
[Select ID from account where ID in (Select r.Account__c From Revenue_Event__c r WHERE Revenue_Event__c.id IN: Trigger.new)]);

if (DoctorID.size()>0){
list <Account> CommissionList = new list<Account>(
[SELECT Id,(Select Id, Practitioner_Account__c, Sales_Agent__c From Commission_Calculations__r WHERE Active__c = TRUE)
FROM Account WHERE id in: DoctorID ]);

if (CommissionList.size()>0){
List<commission__c> FinalCommissions = new list<Commission__c>();

For (Revenue_Event__c NewRevenueEvent : Trigger.new){
for(Account j: CommissionList){

for(Commission_Calculation__c cc : j.Commission_Calculations__r){              
Integer i=0;
              Commission__c NewCommission = new Commission__c();
              NewCommission.Revenue_Event__c = NewRevenueEvent.id;                      
              NewCommission.Sales_Agent__c = CommissionList[i].Commission_Calculations__r.get(i).Sales_Agent__c;
              NewCommission.Commission_Percentage__c = CommissionList[i].Commission_Calculations__r.get(i).id;
              FinalCommissions.add(NewCommission);    
  i++;                 


            }    

 
       }

Insert  FinalCommissions;
}
}
}

Best Answer chosen by Admin (Salesforce Developers) 
GlynAGlynA

Mike,

 

Is this what you're trying to do?

 

trigger GenerateCommission on Revenue_Event__c ( after insert )
{
    //  build a set of Accounts (doctors) related to each Revenue Event in this trigger
    Set<Id> set_AccountIDs = new Set<Id>();

    for ( Revenue_Event__c event : Trigger.new )
    {
        set_AccountIDs.add( event.Account__c );
    }

    //  query the Account records (doctors) related to the Revenue Events in this trigger
    Map<Id,Account> map_Doctors = new Map<Id,Account>
    (   [   SELECT  Id, (SELECT Id, Practitioner_Account__c, Sales_Agent__c FROM Commission_Calculations__r WHERE Active__c = true)
            FROM    Account
            WHERE   Id IN :set_AccountIDs
        ]
    );
    if ( map_Doctors.isEmpty() ) return;

    List<Commission__c> list_Commissions = new List<Commission__c>();

    //  for each Revenue Event,
    for ( Revenue_Event__c event : Trigger.new )
    {
        //  look up the associated Account (doctor)
        Account doctor = map_Doctors.get( event.Account__c );

        //  for each Commission Calculation related to the Account (doctor),
        for ( Commission_Calculation__c calculation : doctor.Commission_Calculations__r )
        {
            //  create a new Commission record
            list_Commissions.add
            (   new Commission__c
                (   Revenue_Event__c            = event.Id,
                    Sales_Agent__c              = calculation.Sales_Agent__c,
                    Commission_Percentage__c    = calculation.Id
                )
            );
        }
    }
    //  insert the new Commission records into the database
    insert list_Commissions;
}

 

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!

 

-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator

All Answers

GlynAGlynA

You are setting the Integer i = 0; everytime through the loop.  Take that line out of the loop and it should work.

 

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!

 

-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator

Mike MacLeodMike MacLeod

The problem is, I can remove the integer = i, but then the trigger doesn't create the record. Here's the section:

 

for(Commission_Calculation__c cc : j.Commission_Calculations__r){              
Integer i=0;
              Commission__c NewCommission = new Commission__c();
              NewCommission.Revenue_Event__c = NewRevenueEvent.id;                      
              NewCommission.Sales_Agent__c = CommissionList[i].Commission_Calculations__r.get(i).Sales_Agent__c;
              NewCommission.Commission_Percentage__c = CommissionList[i].Commission_Calculations__r.get(i).id;
              FinalCommissions.add(NewCommission);    
i++      ;        


            }    

 

I can't figure a way to reference the array correctly, so the sales agent and commission percentage pick the right records out of the query. Right now it goes through and writes the same fields over and over (but does so for the correct amount of records held in the query. I need to find a way to increment I or use some other method to define the integer i. If you have any thoughts, I would be grateful!

 

 

GlynAGlynA

Mike,

 

It should work if you put the declaration and initialization of i outside the loop, like this:

 

Integer i=0;
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[i].Commission_Calculations__r.get(i).Sales_Agent__c;
    NewCommission.Commission_Percentage__c = CommissionList[i].Commission_Calculations__r.get(i).id;
    FinalCommissions.add(NewCommission);    
    i++;
}

Try this and let me know if you have any problems.

 

-Glyn

Mike MacLeodMike MacLeod

Hi Glyn,

 

I tried, but get this error:

 

Apex trigger GenerateCommission caused an unexpected exception, contact your administrator: GenerateCommission: execution of AfterInsert caused by: System.ListException: List index out of bounds: 1: Trigger.GenerateCommission: line 25, column 1.

 

Any thoughts? 

Thank you for your help!

GlynAGlynA

Mike,

 

Is this what you're trying to do?

 

trigger GenerateCommission on Revenue_Event__c ( after insert )
{
    //  build a set of Accounts (doctors) related to each Revenue Event in this trigger
    Set<Id> set_AccountIDs = new Set<Id>();

    for ( Revenue_Event__c event : Trigger.new )
    {
        set_AccountIDs.add( event.Account__c );
    }

    //  query the Account records (doctors) related to the Revenue Events in this trigger
    Map<Id,Account> map_Doctors = new Map<Id,Account>
    (   [   SELECT  Id, (SELECT Id, Practitioner_Account__c, Sales_Agent__c FROM Commission_Calculations__r WHERE Active__c = true)
            FROM    Account
            WHERE   Id IN :set_AccountIDs
        ]
    );
    if ( map_Doctors.isEmpty() ) return;

    List<Commission__c> list_Commissions = new List<Commission__c>();

    //  for each Revenue Event,
    for ( Revenue_Event__c event : Trigger.new )
    {
        //  look up the associated Account (doctor)
        Account doctor = map_Doctors.get( event.Account__c );

        //  for each Commission Calculation related to the Account (doctor),
        for ( Commission_Calculation__c calculation : doctor.Commission_Calculations__r )
        {
            //  create a new Commission record
            list_Commissions.add
            (   new Commission__c
                (   Revenue_Event__c            = event.Id,
                    Sales_Agent__c              = calculation.Sales_Agent__c,
                    Commission_Percentage__c    = calculation.Id
                )
            );
        }
    }
    //  insert the new Commission records into the database
    insert list_Commissions;
}

 

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!

 

-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator

This was selected as the best answer
Mike MacLeodMike MacLeod

Looks like it! I'm getting one error:

 

for ( Commission_Calculation__c calculation : doctor.Commission_Calculations__r )
        {
            //  create a new Commission record
            list_Commissions.add
            (   Commission__c NewCommission = new Commission__c
                (   Revenue_Event__c            = event.Id;
                    Sales_Agent__c              = calculation.Sales_Agent__c;
                    Commission_Percentage__c    = calculation.Id;


The error is Error: Compile Error: expecting a right parentheses, found 'NewCommission' at line 33 column 30.

Not sure where the parenthesis go though. Thank you very much for the help Glyn!
GlynAGlynA

Yep, my mistake.  (I can't compile the code...)  Anyway, I've edited my reply above to fix the error.  Please try again!

 

-Glyn

Mike MacLeodMike MacLeod

ALMOST :) 

 

Error: Compile Error: expecting a right parentheses, found ';' at line 34 column 58

 

It's the next line down. BTW if you're looking for a bit of contract work, we should really talk. This is the first time I've ever written a trigger, which is why mine sucks. :)

GlynAGlynA

Another goof.  Fixed in the post.  Try again.

 

-Glyn

Mike MacLeodMike MacLeod

WOW, it works! You are the BEST Glyn!   If you're in Toronto, look me up man.  I totally owe you a beer!