1

I need to use fields from 2 different custom objects in a VF page apex:pageBlockTable. How can I use a List to put 2 objects in a single list?

public List<Recipient__c> recList{
    get {
      if(recList== null)
         recList = new List<Recipient__c>
      return recList; 
   }
    set;
}


public List<Scholarship_Award__c> schList{
    get {
      if(schList== null)
         schList= new List<Scholarship_Award__c>
      return schList; 
   }
    set;
}

//sObject list for all objects

public sObject[] getAllRecords() {
  sObject[] results = new sObject[0];
  results.addAll(recList);
  results.addAll(schList);
  return results;
}
//Empty constructor - DML not allowed in constructor
public awardsClass(){
    
}

//Add recipient row in pageBlockTable
public PageReference add_recipient(){
    
    //insert scholarship award

    
    Recipient__c anotherRecipient = new Recipient__c();
    anotherRecipient.Name = rec.Name;
    anotherRecipient.Last_Name__c = rec.Last_Name__c;
    anotherRecipient.School__c = rec.School__c;
    anotherRecipient.Specialty__c = rec.Specialty__c;
    anotherRecipient.Biography__c = rec.Biography__c;   
    
    sch.Recipient__c = rec.Id;
    upsert sch;
    results.add(anotherRecipient);
    return null;
}

Thank you

3
  • 1
    You may need to add more information here to get a clear answer. Your purpose might be achievable just by traversing schema relationships, or you may need to work with a wrapper class.
    – David Reed
    Oct 9, 2020 at 19:31
  • 1
    Try casting to an SObject or Object and adding to a List of the respective type (i,e. List<Object> or List<SObject>). This similar post may help if I understand what you are asking here salesforce.stackexchange.com/questions/175113/… Oct 9, 2020 at 19:42
  • Created lists for both objects and added it to an <sObject> list successfully. But it is returning the error Unknown property sObject.Name() Oct 9, 2020 at 20:05

1 Answer 1

2

If they need to be in the same variable, use sObject instead:

public List<sObject> allrecords {
    get {
      if(allrecords == null)
         allrecords = new List<sObject>();
      return allrecords; 
   }
    set;
}

If you need two separate variables, then you can just use two variables.


To combine the list based on updates, use a getter method:

public sObject[] getAllRecords() {
  sObject[] results = new sObject[0];
  results.addAll(recList);
  results.addAll(schList);
  return results;
}
6
  • Great thanks so much. Do I define both objects in sObject ? Oct 9, 2020 at 19:42
  • Please see updated code Oct 9, 2020 at 20:27
  • @patriciajlim This list would only update once. You need to use a getter method instead.
    – sfdcfox
    Oct 9, 2020 at 21:29
  • Would this replace List<sObject>? I've updated my code, but I don't think I understand properly. Oct 11, 2020 at 16:24
  • @patriciajlim Yes, you'd use getAllRecords instead of the property.
    – sfdcfox
    Oct 11, 2020 at 18:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .