You need to sign in to do that
Don't have an account?
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 -
- Number of Installments
- First Installment Date
- 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.
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]
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.
Thanks.
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
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;
}