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
zAkszAks 

Create records on installement object based on no of installment in transaction object.

Hi,

 How to Create records on Installement object based on no of installment in transaction object which holds the amount divided by no of installments..




Thanks:
Best Answer chosen by zAks
VamsiVamsi
Hi,

You need to write a trigger on transaction object inorder to create Installement  object records based on number count specified in field on transaction object that contains amount divided by no of installments..

Below is a sample code which creates the multiple contacts based on the count specified in field "NumberofLocations__c" on account object 

trigger concreation on Account (after insert,after update) 
{
list<contact> listContact = new list<contact>();
map<id,decimal> mapAcc=new map<id,decimal>();
    for(Account acc:trigger.new)
    {
    mapAcc.put(acc.id,acc.NumberofLocations__c);
    }
if(mapAcc.size()>0 && mapAcc!=null)
    {
    for(Id accId:mapAcc.keyset())
        {
        for(integer i=0;i<mapAcc.get(accId);i++)
            {
            contact newContact= new contact();
            newContact.accountid=accId;
            newContact.lastname='contact'+i;
            listContact.add(newContact);
            }
        }
    }
if(listContact.size()>0 && listContact!=null)
insert listContact;
}

Please mark as best answer, if the above helps ...!!!

All Answers

VamsiVamsi
Hi,

You need to write a trigger on transaction object inorder to create Installement  object records based on number count specified in field on transaction object that contains amount divided by no of installments..

Below is a sample code which creates the multiple contacts based on the count specified in field "NumberofLocations__c" on account object 

trigger concreation on Account (after insert,after update) 
{
list<contact> listContact = new list<contact>();
map<id,decimal> mapAcc=new map<id,decimal>();
    for(Account acc:trigger.new)
    {
    mapAcc.put(acc.id,acc.NumberofLocations__c);
    }
if(mapAcc.size()>0 && mapAcc!=null)
    {
    for(Id accId:mapAcc.keyset())
        {
        for(integer i=0;i<mapAcc.get(accId);i++)
            {
            contact newContact= new contact();
            newContact.accountid=accId;
            newContact.lastname='contact'+i;
            listContact.add(newContact);
            }
        }
    }
if(listContact.size()>0 && listContact!=null)
insert listContact;
}

Please mark as best answer, if the above helps ...!!!
This was selected as the best answer
zAkszAks
Thanks...

Its working  fine fr Me ....

 
zAkszAks
And how to calculate the each Installment Amount on installment object from transaction object when amount and no of installments are entered..



Thanks :
VamsiVamsi
I guess you need the Installement records to be created based on the number of installments entered then amount / no of transcations value would be the amount per each transcation right ??? then populate this value to each of the new Installement records.
zAkszAks
 AmountEntered in transaction / No of installments ,and answers of this should b updated in the Amount Field of Records created on  installment object...

Thanks:
VamsiVamsi
Then populate that answer value in new record amount field. Or else please post the code that you have written
zAkszAks
public with sharing class pay1 {


    public Payment__c thePayment {get; set;}
    public Installments__c myInstallment {get;set;}
    /*public string selectValue {get; set;}
   
    public contact__c con {get;set;}
  
    public List < SelectOption > statusOptions {get;set;}
    public boolean Visibility {get;set;}*/
    final ApexPages.StandardController stdController;    

    public Transaction__c transactions;


    public pay1 (ApexPages.StandardController con) {
        stdController = con;
        //string paymentQuery = 'SELECT cardDetails__c,chequeDate__c,chequeNumber__c,Conpay__c,cvv__c,ExpiryDate__c,password__c,Payment_Date__c,Payment_Number__c,pay__c,PostType__c,TransPay__c,Type__c,UserName__c FROM Payment__c';
        //paymentList = database.query(paymentQuery);
        //transactions = (Transaction__c) con.getRecord();
        
        //thePayment = [select id from Payment__c where Transaction__c = :transactions.id][0];
        //System.debug(con.getRecord());
    }
    
    public Payment__c getThePayment() {
        //stdController.save();
        return thePayment;
    }
    public PageReference save() {
        Transaction__c t = (Transaction__c) stdController.getRecord();
        insert t;
        Payment__c pay = new Payment__c();
        pay.Transaction__c = t.Id;
        pay.Payment_Number__c = '234324';
        insert pay;
        PageReference ref = new PageReference('/' + t.Id);
        ref.setRedirect(true);
        return ref;
    }
    @RemoteAction    
    public static String insertPayment(String donationMode,String charityTrans,String donationAmount,String pledgeAmount,String installment,String checkNumber, String checkDate, String cardDetails, String expDate, String cvv, String username, String password, String contact) {
        String[] cDate = checkDate.split('/');
        Date d = Date.newInstance(Integer.valueOf(cDate[2]), Integer.valueOf(cDate[1]), Integer.valueOf(cDate[0]));
        String[] eDate = checkDate.split('/');
        Date e = Date.newInstance(Integer.valueOf(eDate[2]), Integer.valueOf(eDate[1]), Integer.valueOf(eDate[0]));
        
        Transaction__c trans = new Transaction__c();
        trans.DonationMode__c = donationMode;
        
        List<CharityCenter__c> cc = [select id from CharityCenter__c where name = :charityTrans limit 1];
        trans.CharityTrans__c = cc[0].Id;
        
        List<Contact__c> cd = [select id from Contact__c where name = :Contact limit 1];
        trans.Contact__c= cd[0].Id;
             
        trans.DonationAmount__c = Double.valueOf(donationAmount);
        trans.PledgeAmount__c = Double.valueOf(pledgeAmount);
        trans.Installments__c = Double.valueOf(installment);
        insert trans;
        List<Contact> con = [select id from contact where name = :contact limit 1];
        if (donationMode.equalsIgnoreCase('OneTimeDonation')) {
            
            Payment__c p = new Payment__c();
            p.Transaction__c = trans.Id;
            p.cardDetails__c = Double.valueOf(cardDetails);
            p.chequeDate__c = d;
            p.chequeNumber__c = Double.valueOf(checkNumber);
            p.cvv__c = Double.valueOf(cvv);
            p.ExpiryDate__c = e;
            p.password__c = password;
            p.UserName__c = username;
            if (con != null && con.size() > 0) {
                p.Conpay__c = con[0].id;
            }
            
            try {
                insert p;
            } catch (Exception ex) {
                System.debug(ex.getMessage());
            }
        } else if (donationMode.equalsIgnoreCase('Pledge')) {
            System.debug('Pledge');
            
            Payment__c p = new Payment__c();
            List<Installments__c> payList = new List<Installments__c>();
            Integer noOfInstallments = Integer.valueOf(installment);
            Double eachPledgeAmount = noOfInstallments > 0 ? Double.valueOf(pledgeAmount) / noOfInstallments : 0;
            
            
            /*if (con != null && con.size() > 0) {
                p.Conpay__c = con[0].id;
            }*/
            
            for (Integer i = 0; i < noOfInstallments; i++) {
                Installments__c ip = new Installments__c();
                ip.Name = 'Ins - ' + (i + 1);
                ip.Transaction__c = trans.Id;
                //ip.cardDetails__c = Double.valueOf(cardDetails);
                //ip.chequeDate__c = d;
                //ip.chequeNumber__c = Double.valueOf(checkNumber);
                //ip.cvv__c = Double.valueOf(cvv);
                //ip.ExpiryDate__c = e;
                //ip.password__c = password;
                //ip.UserName__c = username;
                
                ip.Amount__c = eachPledgeAmount;
                payList.add(ip);
            }
            
            
            
            
            System.debug(payList);
            try {
                insert payList;
            } catch (Exception ex) {
                System.debug(ex.getMessage());
            }
        }
        
        return '/' + trans.Id;
        //Transaction__c transactions = (Transaction__c) con.getRecord();
        //p.Transaction__c = transactions.Id;
    }
    
   
}



this is my code ...



Thanks: