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
UrvikUrvik 

Trigger to create multiple records

I have 2 objects (A & B) related using Lookup relationship and I have a trigger on after insert on object A which creates record on object B. - Upto this stage everything is working fine. My issue is - I have the following fields in object A -

 

  1. Number of Installments
  2. First Installment Date
  3. Total Amount Due

If a user enters Number of Installments as 4, First Installment Date 5/15 and Total Amount Due $4000.00

Then

I would like to divide Total Amount Due by Number of Installments ($4000/4=$1000) and create 4 records of $1000 each in the object B. Also, the Payment Due Date in object B should set to 5/15, 6/15, 7/15 and 8/15.

 

Any help would be appreciated. Thanks.

imutsavimutsav
In the trigger first check if the no. of Installment is >0 if yes then
amt = total Amount Due / no. of installment;
NOW write a loop
Integer i = 1;
noOfDays = 0;
List<B> bLists = new List<B>();
while(i<=no. of installment) {
B bRec = new B();
bRec.amount = amt;
bRec.DueDate = dueDate + no.OfDays;
relate the B to A;
bLists.add(bRec);
i++;
noOfDays= noOfDays+15;
}
insert bLists;

Thanks
Utsav

[Do mark this answer as solution and give KUDOS if it helped you resolve the issue]
UrvikUrvik

I am new to triggers and classes so could you please tell me where should I accomodate the code. I am not good at writing loops. Below is my basic trigger which creates a record in object B (Payment) - object A is Violations.

 

trigger CreatePayment on Violation__c (after insert) {

List<Payment__c> pm = new List<Payment__c>();

    for (Violation__c newVio: Trigger.New)
      if (newVio.Number_of_Installment_s__c > 0){
               
            pm.add (new Payment__c(
                Payor_Name__c = 'James',
                Total_Due__c = newVio.Fine_Amount__c,
                Payment_Method__c = 'Check',
                RecordTypeId = '012Z00000008jBP',
                Violation__c = newVio.Id));
    }
    insert pm;
    }

Thanks.

itsawsitsaws

Hi Urvik,

As per my understanding ,I have modified the code as per your requirment,Please have alook on this.

trigger CreatePayment on Violation__c (after insert)
{

List<Payment__c> pm = new List<Payment__c>();
Integer i = 1;
decimal xx;
for(Violation__c newVio : Trigger.New)
{
Integer amount_Val = math.mod(newVio.total_Amount_Due, newVio.no_of_installment);
xx = newVio.First_instalmanet_date ;
for(integer i=0;i<=reminder;i++)
{
Payment__c objpament = new Payment__c();
objpament.amount = amount_Val;
bRec.DueDate = xx ;
xx = newVio.First_instalmanet_date + 1
pm.add(objpament);
}
}
if(!pm.Isempty())
insert pm;

}

 

Regards,

Itswas

sushant sussushant sus

i sample code will help u

 

trigger updateobject2 on object1(before insert)
map<string,object1> map1=new map<string,object1>
integer i=1;
for(object1 ob1:trigger.new)
map1.put(ob1.object2__r.name,ob1);
}

list<object2> ob2=newlist<object2>[select name from object2 where name in:MAP1.KEYSET()];
 for(object 2 obje2: ob2)
 {   
    object1 objec1=map1.get(obje2.name);
   while(i<= integer.valueof(objec1.no_of_installment))
   {  
     
     obje2.due_ammount=objec1.totalamount)/objec1.no_of_installment);
     obje2.payment_due_date=objec1.paymentduedate.addMonths(i);
    
     objec2.add(objec2);
   
   
   
   }
   i++
 
   insert objec2;
 
 }