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
BlairBBlairB 

Adding multiple custom object records on one visualforce page

Hello,

I have a custom object (Warehouse Visit) that is the child record of Contact. I'd like to click a button on the Warehouse Visit tab where multiple records can be created on one Visualforce page. Basically, I'd like staff to be able to log multiple visits on one page rather than add them one by phone.

On the VF page, I'd like a link that says "add" to allow staff to add multiple rows for each record (3-4 fields). At the end of each row, I want a Remove or X link to remove that row if it was created in error. Then, on save, I'd like it to redirect back to the Warehouse Visit tab. 

I haven't started building the VF page yet, but I'd like to enter about 4 fields, including a lookup to the Contact, a couple of date/time fields, and maybe a checkbox or picklist. 

I have started with this Apex class code, based on a previous button I had help developing, but I'm getting an error. Can anyone help fix this code?

public class WarehouseVisitController {
    public List<Warehouse_Visit__c> wvRecord{get; set;}
    public Integer wvIndex {get; set;}

    public WarehouseVisitController  (){
        wvRecord = new List<Warehouse_Visit__c>();
        wvRecord.add(new Warehouse_Visit__c);
    } #this is where the error is happening
    public void addWarehouseVisit () {
        wvRecord.add(new Warehouse_Visit__c);
    }
    public void removeWarehouseVisit() {
        wvIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('wvIndex'));
        wvRecord.remove(wvIndex);
    }
    public PageReference save() {
        upsert wvRecord;
        PageReference page = new PageReference('/');
        return page;
    }
    public PageReference cancel() {
        PageReference page = new PageReference('/' );
        return page;
    }
}

 
ShirishaShirisha (Salesforce Developers) 
Hi,

Greetings!

As per my understanding your requirement is to create the multiple records at a time using the Visualforce page.

If yes,you can check the below blog which can help you to write the logic or start with the visualforce page and then you can make changes to your code according to your business requirement.

https://www.salesforcetutorial.com/visualforce-page-add-multiple-records/

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
BlairBBlairB
Hi,
I appreciate that info, but it's not exactly what I need, as I want to add custom object records that are the child records of the Contact object. I would really love help working from my existing code and moving forward. Thanks.
GauravGargGauravGarg
@BalirB,

You have to follow few steps:
  • Create a Wrapper class says "ChildContactWrapper"
  • Add fields need to be displayed over the UI. 
  • Add List<ChildContactWrapper> as {get; set;} 
  • Method: add a new record,
    • To initiate new ChildContactWrapper record every-time "add" button clicks and place in a List of the wrapper. 
    • The function would add Contact-id of the parent record.
  • Method: delete record
    • To delete the last record in the ChildContactWrapper list when "remove" button clicks. 
  • Create Another function for "Save", to create child records based on the entry made. 

I hope this solves your problem. 

Thanks,
Gaurav
Skype: gaurav62990