+ Start a Discussion
Patricia LimPatricia Lim 

How to combine 2 apex classes

I am trying to combine both custom controller and extension in a single Apex class so I don't have to use an extension and can use 2 objects in one VF page.

I need to be able to add records from both `participant__c` and `referral__c` objects. Where participant records are inserted together from the record list.

First class for using 2 objects
public class ecrjcController {
    public Referral__c ref {get;set;}
    public Participant__c par {get;set;}
    
    public ecrjcController(){
        
        ref = new Referral__c();
	}
    
    public void saveRecords(){
        insert ref;
    }
}

2nd Apex Class for apex:pageBlockTable
 
public with sharing class ecrjcClass{
    
    public Participant__c record = new Participant__c();
    public Referral__c ref {get;set;}
    public List<Participant__c> allrecords {get;set;}

    public ecrjcClass(ApexPages.StandardController controller){
        ref = new Referral__c();
        record = (Participant__c)controller.getRecord();
        allrecords = new List<Participant__c>();
    }

    //Add a record row
    public pageReference Add_Row(){

        Participant__c anotherRecord = new Participant__c();   
        anotherRecord.Name = record.Name;
        anotherRecord.Last_Name__c = record.Last_Name__c;
        anotherRecord.Age__c = record.Age__c;
        anotherRecord.Victim_Offender__c = record.Victim_Offender__c;
        anotherRecord.Referral__c = record.Referral__c;
        allrecords.add(record);

        return null;
    }
	
    //Save and insert records in list
    public pageReference save(){
	
        insert ref;
        insert allrecords;
        return null;
            
    }
}
    //Delete button on each record on apex table
Unfortunately using a standard controller with an extension does not work as it is returning an error when I reference an object from the extension