Skip to main content
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;

    }

}

 
3 个回答
  1. 2020年6月9日 10:53
    @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
0/9000