I have an <apex:pageBlockTable>
on a VF page that references an sObject list containing records from 2 objects.
- As soon as I reference a field that one object doesn't have, it throws:
Invalid field Last_Name__c for SObject Scholarship_Award__c
on the VF preview page.
- With the
Name
field alone, it displays records like this instead of by each record:
I can't find any similar issues on the web, but I know both have to do with the List<sObject>
since it works 100% with regular single object lists.
Apex
public without sharing class awardsClass {
//Initialize objects and create new record for each
//Recipient
public Recipient__c rec {
get {
if(rec == null)
rec = new Recipient__c();
return rec;
}
set;
}
//Scholarship Award
public Scholarship_Award__c sch {
get {
if(sch == null)
sch = new Scholarship_Award__c();
return sch;
}
set;
}
//List for allrecipients
public List<sObject> allrecipients {
get {
if(allrecipients == null)
allrecipients = new List<sObject>();
allrecipients.add(rec);
allrecipients.add(sch);
return allrecipients;
}
set;
}
//Set attachment object
public Attachment myDocument {
get {
if(myDocument == null)
myDocument = new Attachment();
return myDocument;
}
set;
}
//Empty constructor - DML not allowed in constructor
public awardsClass(){
}
//Add recipient row in pageBlockTable
public PageReference add_recipient(){
//insert scholarship award
//Set new instances of recipient and scholarship
Recipient__c anotherRecipient = new Recipient__c();
Scholarship_Award__c anotherScholarship = new Scholarship_Award__c();
//Recipient fields
anotherRecipient.Name = rec.Name;
anotherRecipient.Last_Name__c = rec.Last_Name__c;
anotherRecipient.School__c = sch.School__c;
anotherRecipient.Specialty__c = rec.Specialty__c;
anotherRecipient.Biography__c = rec.Biography__c;
//Scholarship fields
anotherScholarship.Award__c = sch.Award__c;
anotherScholarship.Year__c = sch.Year__c;
anotherScholarship.School__c = sch.School__c;
anotherScholarship.Recipient__c = rec.Id;
//All both recipient and scholarship record to allrecords list
allrecipients.add(anotherRecipient);
allrecipients.add(anotherScholarship);
return null;
}
//Save all records and upload document to recipient record
public PageReference saveAll(){
upsert allrecipients;
myDocument.parentId = rec.Id;
insert myDocument;
return null;
}
}
VF (Snippet to save your eyes. Have got inputFields before this)
<apex:pageBlock title="Your Added Scholarships">
<apex:pageBlockTable id="Recipients" value="{!allrecipients}" var="Recipient">
<apex:column headerValue="First Name">
<apex:outputText value="{!Recipient['Name']}"/>
</apex:column>
<apex:column headerValue="Last Name">
<apex:outputText value="{!Recipient['Last_Name__c']}"/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!saveAll}" value="Submit Scholarships"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>