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
GrrrrrrrrrrrrrGrrrrrrrrrrrrr 

Trigger help

Hello!

 

  I am writing a trigger (or trying to ...) that will create X amount of child records on a parent. 

Parent - Custom Object: Finance__c

Child   - Custom Object: Finance_Payment__c

 

On the parent is a field called Number_of_payments__c

 

I want the trigger to (after parent insert) create the same number of children records as indicated in the field Number_of_payments__c.

The only field on the child record that needs to be populated on the initial insert is the payment_number__c (which starts at 1 and ends at :Number_of_payments).

 

Can someone help me fill in the blanks (and correct mistakes). 

I am very new to triggers.  Thanks!

 

trigger Payments on Finance_Payment__c (after insert){
    // collect parent id ?
id[] finIds = new Id[0];
// collect number of payments data
Finance__c[] parent;
parent = [SELECT Number_of_payments__c FROM Finance__c WHERE Finance__C IN (ParentID?)];
// iterate through to create child records.
for (Finance_Payment__c f : tigger.new){
// create records using number of payments
insert ?;
}

}

 

Best Answer chosen by Admin (Salesforce Developers) 
AmulAmul

Number_of_payments__c is available on Finance__c. Then you need to write a trigger on Finance__c object like as

trigger tiggerOnFinanceObject on Finance__c (after insert) {

list<Finance_Payment__c> lstinsertFinancePayment=new list<Finance_Payment__c>();

for(Finance__c objFinance:Trigger.New){

if(objFinance.Number_of_payments__c!=null){
integer iRowCount=objFinance.Number_of_payments__c;
for(integer icount=1;icount++;;icount<=iRowCount){
Finance_Payment__c objFinancePay=new Finance_Payment__c();
objFinancePay.Financ__c=objFinance.id;
lstinsertFinancePayment.add(objFinancePay);
}
}
}

if(lstinsertFinancePayment.size()>0){
insert lstinsertFinancePayment;
}


}

 

 

//hope above code will help you.

 

All Answers

AmulAmul

Number_of_payments__c is available on Finance__c. Then you need to write a trigger on Finance__c object like as

trigger tiggerOnFinanceObject on Finance__c (after insert) {

list<Finance_Payment__c> lstinsertFinancePayment=new list<Finance_Payment__c>();

for(Finance__c objFinance:Trigger.New){

if(objFinance.Number_of_payments__c!=null){
integer iRowCount=objFinance.Number_of_payments__c;
for(integer icount=1;icount++;;icount<=iRowCount){
Finance_Payment__c objFinancePay=new Finance_Payment__c();
objFinancePay.Financ__c=objFinance.id;
lstinsertFinancePayment.add(objFinancePay);
}
}
}

if(lstinsertFinancePayment.size()>0){
insert lstinsertFinancePayment;
}


}

 

 

//hope above code will help you.

 

This was selected as the best answer
GrrrrrrrrrrrrrGrrrrrrrrrrrrr

That is perfect!  I made a few mods to match some other field names that I have added since, modifed the loop (it was out of order) and PERFECT.

 

Thanks