You need to sign in to do that
Don't have an account?
Daniel Probert
Trigger to Loop a specific number of times
Hi Community,
I'm new to apex and trigger but have created a simple trigger against a custom object Loan__c that after insert will create the repayment schedule in the custom related object Loan_Repayment__c
trigger generateloanrepayments on Loan__c (after insert) { List <Loan_Repayments__c> prefToInsert = new List <Loan_Repayments__c> (); for (Loan__c c : Trigger.new) { Loan_Repayments__c p = new Loan_Repayments__c (); p.Loan__c = c.Id; p.Expected_Repayment_Date__c = c.Hidden_First_Payment_Date__c + 30; p.CurrencyISOCode = c.CurrencyISOCode; prefToInsert.add(p); } try { insert prefToInsert; } catch (system.Dmlexception e) { system.debug (e); } }
This works well however what I now need to do is have this loop and create the entire repayment schedule based on the value that is stored in Loan__c.Number_of_Repayments__c.
So when Loan__c.Number_Of_Repayments__c = 5 there are 5 records creates.
Hope this makes sense and someone can help me.
Cheers
Dan
for (integer i=0 i< numOfRepayments i++){
// create your records
}
thanks where do I put that though i changed my code to this an I get an error:
Error: Compile Error: expecting a semi-colon, found 'i' at line 8 column 21
appreciate your quick response..
I am assuming the date needs to progress as well. How about:
for (Loan__c c : Trigger.new) { Loan_Repayments__c p = new Loan_Repayments__c (); p.Loan__c = c.Id; p.CurrencyISOCode = c.CurrencyISOCode; For (Integer i = 1, i <= Loan__c.Number_Of_Repayments__c,i++) { p.Expected_Repayment_Date__c = c.Hidden_First_Payment_Date__c + (30 * i); prefToInsert.add(p); } }
If I am wrong about the date thing, just keep your original Repayment Date line as and where it was (outside the new 'for' loop) and delete mine.
Regards
MellowRen
Man you guys were quick at posting!
In your current solution you're missing some commas in the 'for' loop declaration—look at the example I wrote and you'll see what I mean.
thanks for the idea hadn't got to that i was going to add that once I got the basic looping working.
i get this error with the code that you've given me and can't see the duplicate value:
Error: Compile Error: Duplicate variable: i (attempt to re-create the variable with type: Integer) at line 11 column 21
any ideas?
replace , with ;
Hey - You seem to have lost your outer for loop (the trigger.new one). You still need that.
Arrgg, its late and I am confusing languages. You need semi-colons ( ; ) in the for loop, not commas. Sorry. That will fix your duplicate declaration problem.
EDIT: Man rov is fast! :-)
List <Loan_Repayments__c> prefToInsert = new List <Loan_Repayments__c> ();
integer numOfRepayments = Loan__c.Number_of_Repayments__c;
for (integer i=0; i< numOfRepayments; i++){
Loan_Repayments__c p = new Loan_Repayments__c ();
p.Loan__c = c.Id;
p.Expected_Repayment_Date__c = c.Hidden_First_Payment_Date__c + 30;
p.CurrencyISOCode = c.CurrencyISOCode;
prefToInsert.add(p);
}
try {
insert prefToInsert;
} catch (system.Dmlexception e) {
system.debug (e);
}
}
hi guys thanks for the response, i've had a long weekend so just getting back to this now.
using this code i can now save the trigger without error:
the problem i now have is when I save a new Loan record which should trigger this to create x number of repayment records I receive this error:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger generateloanrepayments caused an unexpected exception, contact your administrator: generateloanrepayments: execution of AfterInsert caused by: System.ListException: Before Insert or Upsert list must not have two identically equal elements: Trigger.generateloanrepayments: line 13, column 1
this seems to be an issue within:
but i can't for the life of me work out what might be causing it :(
if i remove the statement then i get no error but also no records added.
any guidance?
thanks and kudos to all of you for helping me get this far.
dan
Ooops. Fair enough. We're trying to insert the same record multiple times. Will write a modifed version in a tick.
no worries all I've just worked it out with a mix of everything that was sent through this is my final code that works perfectly.
the issue was the i had 2 seperate sections for the initial loop then a new loop for the date increment - this i got working but found only 1 record was created with the date incremented 5 times. 5 = number of repayments.
it was then that i realised what the issue must be so rebuilt like above and it works perfectly.
once again kudos to you all wouldn't have worked it out without you.
Try this:
I think that should work. Let us know if there are any further errors.
Regards
MellowRen
hey mellow thanks for sending that through what's the benefit of using the method below?
Mine looks almost identical apart from that so just want to understand why that would be beneficial to me?
Yes, you got it. Funny how our code is basically identical.
> once again kudos to you all wouldn't have worked it out without you.
There is actually a kudos mechanism on this forum (the "star" button under peoples names) if you truly mean this :-)
No difference. We simul-posted (I wrote my reply as you wrote down yours). Your version and mine are functionally the same.
I personally have a style choice of declaring variables near the top of code, I find it easier for debugging/modifying later. Since I hadn't looked at this for a few days (your long weekend), I just reverted to my style whilst typing a solution for you.