+ Start a Discussion
TestingSFUserTestingSFUser 

How to created multiple Child Records with different dates when Parent record is created/updated

Parent record: Invoice (Fields: Amount__c, No_of_terms__c, Invoice_Date__c)
Child Record: Monthly_Payment__c (Fields: Payment_amount__c, Payment_date__c, Invoice__c)
====================================
When ever Invoice(Parent) record is created/ update with..
Amount__c = $1000;
No_of_Terms__c = 9;
invoice_date__c = today();
-----------
then i want to create Monthly Payments records as many as no_of_terms...
and
-----
for upto....
monthly payment records = no_of_terms - 1
Payment_amount__c = 111.11 (we calculate upto 2 decimals)
((should be equel to (1000/8) = 111.11))
Payment_date = today()+30 (for 1st monthly payment record)
(today()+60...for 2nd monthly payment record)
(today()+90...for 3rd monthly payment record....)like that
----------------------

for last monthly payment record (i.e. 9th monthly payment record)
-----
Payment_amount__c = (1000-(111.11*8)) = 111.12
payment_date__c = today()+270
==============================
==============================
PriyaPriya (Salesforce Developers) 
Hey,

Write a trigger code to achieve this. See the below example to create the child record when any parent is created :- 
 
trigger CreateBuyingInfluence on Opportunity_Positioning__c (after insert)

 {
    List<Buying_Influence__c> Childs = new List<Buying_Influence__c>();

    for(Opportunity_Positioning__c a : trigger.new)
    {
       Buying_Influence__c Child = new Buying_Influence__c ();
       Child.SCOP__c = a.id;
       Child.Name = 'testName'; 

       Childs.add(Child);      
    }

    insert Childs;
}

If this helps, kindlly mark it as best answer.

Thanks!