0

I have two custom Object name Loan and EMI. Now whenever new Loan is created with Fields Loan_Amount , No_of_EMI and others fields , multiple rows(=number of emi accounts) should be created in EMI object with Calculating the Value Remaining Amount. For each EMI records, remaining amount is different. I am getting below while doing this.

Execution of BeforeInsert caused by: System.ListException: Before Insert or Upsert list must not have two identically equal elements

trigger LaonEMI on Loan__c (before insert) {
    List<EMI__c> recordsToInsert = new List<EMI__c>();
    Double EMICount = 0;
    Double FinalAmount = 0;
    Double EMIAmount = 0;
    EMI__c la = new EMI__c(); 
    for (Loan__c l : Trigger.new) { 
            //la.Loan_ID__c = l.name;
            la.EMI_Amount__c = l.EMI_Amount__c  ;           
            la.EMI_Paid__c =  true;
            EMICount = l.Number_of_EMI__c;
            FinalAmount = l.Final_Amount__c ;
            EMIAmount = l.EMI_Amount__c;   
    }
     Decimal RemaningAmount = 0;
    for(integer i = 0 ; i<EMICount ; i++)
    {
        if( i == 0)
        {
            RemaningAmount = FinalAmount ;
        }
        else
        {
            RemaningAmount = (RemaningAmount - EMIAmount);            
        }
        la.Remaining_Amount__c  = RemaningAmount;
        recordsToInsert.add(la);
        system.debug(recordsToInsert);

    }
    insert recordsToInsert;

   // 


}
0

Your main problem is here:

recordsToInsert.add(la);

Since you only created one record in memory, adding it to the list more than once results in this error. I'm not 100% certain what you're trying to do, but it appears you'll be closer simply by shuffling some code around and adding a line or two:

trigger LaonEMI on Loan__c (before insert) {
    List<EMI__c> recordsToInsert = new List<EMI__c>();
    for (Loan__c l : Trigger.new) { 
        Double EMICount = 0;
        Double FinalAmount = 0;
        Double EMIAmount = 0;
        Decimal RemaningAmount = 0;
        EMI__c la = new EMI__c(); 
        //la.Loan_ID__c = l.name;
        la.EMI_Amount__c = l.EMI_Amount__c  ;           
        la.EMI_Paid__c =  true;
        EMICount = l.Number_of_EMI__c;
        FinalAmount = l.Final_Amount__c ;
        EMIAmount = l.EMI_Amount__c;   
        for(integer i = 0 ; i<EMICount ; i++)
        {
            if( i == 0)
            {
                RemaningAmount = FinalAmount ;
            }
            else
            {
                RemaningAmount = (RemaningAmount - EMIAmount);            
            }
            la.Remaining_Amount__c  = RemaningAmount;
            // Create a copy
            recordsToInsert.add(la.clone(false));
        }
    }
    insert recordsToInsert;
}

You may need to actually change some more logic than this, but it should get you started.

  • I am trying to insert records in object EMI. Number of records should equal to the Number of EMI. For Example, if we apply Loan 100000 and tenure is 12 months then Number of EMI is 12. In this case, 12 records should be inserted in EMI with different remaining amount. For First Records, Remaining amount is 110000(in case of 10% interest). For second record, It should Remaining Amount - EMIAmount. And so on for rest records. I am new to Salesforce – Alam Jan 17 at 14:22
  • @Alam Okay, but I can only help you with what I already know. Given this code as a starting point, what's your problem? Are you getting an error? Unexpected behavior? You may even want to ask a new question if this answer resolved your current question (the ListException error). We're not a "back-and-forth" type system like some discussion boards, but a real Q&A system. You might want to take the tour if you haven't already. – sfdcfox Jan 17 at 14:28
  • After implementing this line "recordsToInsert.add(la.clone(false));" . Now i am not getting any error. Insertion happend in Loan Object but still no insertion in EMI Object. – Alam Jan 17 at 14:31
  • @Alam Are you sure? If there wasn't an error, there should be records there. I did notice you had a line commented out for the Loan_ID__c field, so they're probably just not linked correctly. Try doing a query on the records. By the way, you'll need to change the trigger to "after insert" in order to get an Id for the Loan__c record. – sfdcfox Jan 17 at 14:44
  • Yes, I am sure. Getting no error but still no insertion happened. I configured with ID after changing to "After Insert". Thanks for prompt response. – Alam Jan 17 at 14:53
-1

Try this...

trigger LaonEMI on Loan__c (after insert) {
    List<EMI__c> recordsToInsert = new List<EMI__c>();  
    for (Loan__c l : Trigger.new) { 
        Double FinalAmount = 0;
        Double EMIAmount = 0;
        Decimal RemaningAmount = 0;
        for(integer i = 0 ; i<l.Number_of_EMI__c ; i++)
        {
            EMI__c la = new EMI__c(); 
            la.Loan_ID__c = l.ID;
            la.EMI_Amount__c = l.EMI_Amount__c  ;           
            la.EMI_Paid__c =  true;
            FinalAmount = l.Final_Amount__c ;
            EMIAmount = l.EMI_Amount__c;   
            if( i == 0)
            {
                RemaningAmount = FinalAmount ;
            }
            else
            {
                RemaningAmount = (RemaningAmount - EMIAmount);            
            }
            la.Remaining_Amount__c  = RemaningAmount;
            recordsToInsert.add(la);
        }
    }
    insert recordsToInsert;
}
  • I already tried having nested loop but didn't get success. No error is coming but Insertion not happened. I am doing this in Lightning view. Is there any access need to be given to insert data through trigger? I am login as a System Administrator. – Alam Jan 17 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.