function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Melissa Parker 17Melissa Parker 17 

Invalid field for SObject when building a table of data in a Visualforce page

I am attempting to create a Visualforce Email template that basically emails the contact and shows its related list of Assignments__c in the template.

I am following the document here: https://developer.salesforce.com/page/VisualForceEmailTemplates_sample

However I am receiving an error: Error: Invalid field Contact for SObject Assignments__c

<messaging:emailTemplate subject="Tomorrow's Inspections and Homeowner Closing Appointments" 
recipientType="Contact" 
relatedToType="Assignments__c">

<messaging:htmlEmailBody >
 <html>
            <body>

            <p>Dear {!recipient.name},</p>
            <p>Attached is your inspection schedule for tomorrow. Please review it to make sure there is enough time allowed between inspections. 
            Call Rick Werts (863) 207-6550 with any changes that are necessary to make sure we are making every appointment on time. 
            Thank you for all of your hard work out there.

            Thank you,

            Bill Todd
            V.P. of Sales
</p>
            <table border="0" >
                <tr>
<th>Policyholder Name</th><th>Risk Street</th>
                    <th>Risk City</th><th>Risk State</th>
                    <th>Risk Zip Code</th><th>Inspection Date and time</th>
                    <th>Homeowner Closing Appt</th>
                </tr>
                <apex:repeat var="cx" value="{!relatedTo.Contact.Assignments__c}">
                <tr>
                    <td><a href = 
                        "https://cs50.salesforce.com/{!cx.id}">{!cx.Assignments.Name}
</a></td>
                    <td>{!cx.Assignments.Risk_Street_Address__c}</td>
                    <td>{!cx.Assignments.Risk_State_Address__c}</td>
                    <td>{!cx.Assignments.Risk_City__c}</td>
                    <td>{!cx.Assignments.Risk_Zip_Code_Address__c}</td>
                    <td>{!cx.Assignments.Inspection_Date_and_Time__c}</td>
                    <td>{!cx.Assignments.Homeowner_Closing_Appt__c}</td>
                </tr>
                </apex:repeat> 
            </table>
            <p/>
            <center>
                <apex:outputLink value="http://www.salesforce.com">
                    For more detailed information login to Salesforce.com
                </apex:outputLink>
            </center>
            </body>
        </html>
    </messaging:htmlEmailBody>
</messaging:emailTemplate>
Mahesh DMahesh D
Hi Melissa,

Try this:

<apex:repeat var="cx" value="{!relatedTo.Contact.Assignments__c}">

to 

<apex:repeat var="cx" value="{!relatedTo.Contact__r.Assignments__c}">

If there is any issue then let us know the field name of Contact on Assignment__c object.

Regards,
Mahesh
Melissa Parker 17Melissa Parker 17
I attempted, but didnt work. Now I receive an error of "Error: Invalid field Contact__r for SObject Assignments__c"

The contact lookup field on the Assignment__c object is called: 
User-added image

Thanks for all your help!!!
BHinnersBHinners
If you look at the doc here:
https://developer.salesforce.com/page/VisualForceEmailTemplates_sample
It shows using the plural form of the child related list for an email set up like so:
1<messaging:emailTemplate recipientType="Contact"
2    relatedToType="Account"
The related list on Accounts that is being added to the email is Cases:
<apex:repeat var="cx" value="{!relatedTo.Cases}">
In this example, relatedToType gives us access to the parent object for "Contact" so that we can access a different child object, "Case", on that same parent.

Since you are using a related list Assignments__r (*note that the related list uses the defined plural form and has __r suffix rather than the __c suffix*) on the Contact object, you should be able to just set value={!Contact.Assignments__r}.

Another good doc reference is found here:
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_custom_mapping.htm
Melissa Parker 17Melissa Parker 17
Thanks, I changed the value to display !Contact.Assignments__r, however, I am now receiving this error:

Error: Unknown property 'core.email.template.EmailTemplateComponentController.Contact'

User-added image
Mahesh DMahesh D
You can try this:
 
<apex:repeat var="cx" value="{!relatedTo.Inspector_Lookup__r.Assignments__c}">

Also you have to know the child relationship name for this Lookup.

Regards,
Mahesh
Melissa Parker 17Melissa Parker 17
No cigar! I am now receiving this error: Error: Invalid field Assignments for SObject Assignments__c

I'm not sure what you mean that I will need to know the child relationship for this lookup. From the Assignments object there is a lookup to the contact object called Inspector_lookup__c. Thats all there is to it. I have posted the entire code again with the update. Thanks again for the help.

<messaging:emailTemplate subject="Tomorrow's Inspections and Homeowner Closing Appointments" 
recipientType="Contact" 
relatedToType="Assignments__c">

<messaging:htmlEmailBody >
 <html>
            <body>

            <p>Dear {!recipient.name},</p>
            <p>Below is your inspection schedule and for tomorrow. Please review it to make sure there is enough time allowed between inspections. 
            Call Rick Werts (863) 207-6550 with any changes that are necessary to make sure we are making every appointment on time. 
            Thank you for all of your hard work out there.

            Thank you,

            Bill Todd
            V.P. of Sales

</p>
            <table border="0" >
                <tr>
<th>Policyholder Name</th><th>Risk Street</th>
                    <th>Risk City</th><th>Risk State</th>
                    <th>Risk Zip Code</th><th>Inspection Date and time</th>
                    <th>Homeowner Closing Appt</th>
                </tr>
                <apex:repeat var="cx" value="{!relatedTo.Inspector_Lookup__r.Assignments__r}">
                <tr>
                    <td><a href = 
                        "https://cs50.salesforce.com/{!cx.id}">{!cx.Assignments.Name}
</a></td>
                    <td>{!cx.Assignments.Risk_Street_Address__c}</td>
                    <td>{!cx.Assignments.Risk_State_Address__c}</td>
                    <td>{!cx.Assignments.Risk_City__c}</td>
                    <td>{!cx.Assignments.Risk_Zip_Code_Address__c}</td>
                    <td>{!cx.Assignments.Inspection_Date_and_Time__c}</td>
                    <td>{!cx.Assignments.Homeowner_Closing_Appt__c}</td>
                </tr>
                </apex:repeat> 
</table>
            <p/>
            <center>
                <apex:outputLink value="http://www.salesforce.com">
                    For more detailed information login to Salesforce.com
                </apex:outputLink>
            </center>
            </body>
        </html>
    </messaging:htmlEmailBody>
</messaging:emailTemplate>
BHinnersBHinners
Sorry, I am not following the object relationships you have defined.  Which object is parent, and which is child?  Or is this a many-to-many relationship?

You can find the relationship names for objects in Setup.
Melissa Parker 17Melissa Parker 17
Contacts is Parent, Assignments is child. Thats it.

On the Conact, there is an Assignment List.
Mahesh DMahesh D
Please take a Printscreen of your lookup field along with the Child Relationship Name.
Melissa Parker 17Melissa Parker 17

I already did but here it is again:


User-added image

User-added image

Mahesh DMahesh D
Hi Melissa,

You can try this:

 
<apex:repeat var="cx" value="{!relatedTo.Inspector_Lookup__r.Assignments__r}">

Regards,
Mahesh
BHinnersBHinners
Are you firing the email off the Assignment object?  Or off Contact?  That determines which object should be specified in the RelatedToType and how you will access related data.