1

I have an <apex:pageBlockTable> on a VF page that references an sObject list containing records from 2 objects.

  1. 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.

enter image description here

  1. With the Name field alone, it displays records like this instead of by each record:

enter image description here

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>

1 Answer 1

1

As indicated in the error the issue is because field Last_Name__c does not exist on the Scholarship_Award__c object.

You can fix this in one of the below ways

  1. Create a field Last_Name__c that copies only last name to this field from Name field. You can do this via a simple flow or a trigger. Prefer this if you need the Last Name field for other logic or reporting requirements.

  2. The other technique is if you only need it to display and not input create a Wrapper object and return that instead of SObject. This is recommended if you need the Last Name displayed only for this page.

    public with sharing class Wrapper {
    
        public String LastName {get;set};
    
        public Integer Year{get;set};
    }
    

And then use this in the getters. You will construct the Wrapper objects from actual Sobject.

 public List<Wrapper> allrecipients {
    get {
        if(allrecipients == null)
            allrecipients = new List<Wrapper>();
            Wrapper wrapObject = new Wrapper();
            Wrapper.add(wrapObject);
        return allrecipients;
    }
    set;
}
2
  • Thanks but the last name field is only for the recipient object... as I want data from two different objects being displayed in the pageBlockTable Unfortunately I will need my script to insert records as well, so wrapper won’t work. Oct 20, 2020 at 22:21
  • with wrapper object you can use those properties on page block than using actual fields. This could be empty string for other object which does not have the field Oct 20, 2020 at 23:47

You must log in to answer this question.

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