+ Start a Discussion
Jamie EvansJamie Evans 

bug? page refresh creates new child records

Hi, 

I have a problem that I'm finding hard to figure out. Any help on the issue would be most welcome. 

I have a custom object called Bonus, this has a lookup relationship with another custom object called Payments. 

Using a VF page and standard controller with extension, I have a button on the Bonus page layout that will create 4 payment records for a single Bonus record when pressed. 

This all works fine, however if I refresh the page immidiately after creating the Payment records, I get 4 more. If I close the page, open it again and then refresh I do not get any duplicates. 

Has anyone ever experienced this before? ...or have any suggestions for how I can troubleshoot?

Many thanks!
Ankit AroraAnkit Arora
How you are inserting the records? When you press the button a VFP is called and then via action you are inserting the records, that is one reason I predcit might cause a problem. Try this once (even if it is not a case), open a VFP and provide another button which user has to hit to insert the records. If it resolves the problem then do let me know.
Jamie EvansJamie Evans
Hi Ankit, 

Thanks for your message. 

The button is already on a VFP, this calls an apex method via action: 
<apex:commandButton value="Create Payments" action="{!CreatePayments}"/>

The createPayment() method then creates a list, set's the fields for the payment's and finally inserts the list. 




Gigi.OchoaGigi.Ochoa
Hi Jamie,

Can we see your code?

Jamie EvansJamie Evans
Hi Gigi, 

Thanks for your message - yes sure thing: 

NOTE: Some code has been removed to make it easier to read, see ** section below. 

Thanks again.


public with sharing class sipExt { 
   
    private ApexPages.StandardController standardController;     
    public sipExt(ApexPages.StandardController standardController)
    {
        this.standardController = standardController;
    }
   
    public PageReference CreatePayments()
    {       
        Id recordId = standardController.getId();
        bonus__C bonusRecord = (bonus__C) standardController.getRecord();
        system.debug(bonusRecord);
       
        list<Bonus_Payments__c> PAYList = new List <Bonus_Payments__c>();
       
        //Create Payment SA1 P1
        Bonus_Payments__c PAY1 = new Bonus_Payments__c();
        PAY1.Bonus_Record__c = bonusRecord.Id;
        PAY1.Sales_Associate__c = bonusRecord.sa1_user_lookup__c;
        PAY1.Date_Payment_Due__c = bonusRecord.Date_Payment_1_Due__c;
        PAY1.amount__c = bonusRecord.SA1_P1_Amount__c;
        PAY1.adjusted_amount__c = bonusRecord.SA1_P1_Adj__c;
        PAY1.Division__c = bonusRecord.SA1_division__c;
        PAY1.Region__c = bonusRecord.SA1_region__c;
        PAY1.Payment__c = '1';
        PAY1.Opportunity_Record__c = bonusRecord.opportunity__c;
        Pay1.Type__c = 'SIP';
       
         // Only create payment record if a sales associate exists
         if (PAY1.Sales_Associate__c != null)
         {
          PAYList.add(PAY1);
         }
       
 ** THERE ARE 3 MORE PAYMENTS IDENTICAL TO ABOVE, PAY2, PAY3, PAY4. REMVOED TO MAKE THIS EASIER TO READ **


        insert PAYList;
  return null; 
}
}
Sumitkumar_ShingaviSumitkumar_Shingavi
1. Look at <apex:page> tag and see if "action" property is set in there which is calling some method from your controller. There might be indirect call to method done for creating record and that's the reason why records getting created .
2. Secondly, also check if any JS code calling any methods on "onload" event.
3. After #1 and #2 is confirmed then you also need to add a line at end of "CreatePayments()" method to re-initialize "PAYList" list variable. So, do it after "insert PAYList;" line immediately.

One of the should solve your provblem.

PS: if this answers your question then hit Like and mark it as solution!