You need to sign in to do that
Don't have an account?
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;
}
}
}
Mike,
Is this what you're trying to do?
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
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
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!
Mike,
It should work if you put the declaration and initialization of i outside the loop, like this:
Try this and let me know if you have any problems.
-Glyn
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!
Mike,
Is this what you're trying to do?
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
Looks like it! I'm getting one error:
Yep, my mistake. (I can't compile the code...) Anyway, I've edited my reply above to fix the error. Please try again!
-Glyn
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. :)
Another goof. Fixed in the post. Try again.
-Glyn
WOW, it works! You are the BEST Glyn! If you're in Toronto, look me up man. I totally owe you a beer!