1

I am trying to save a visualforce email template that has both plain text and html elements-- I just tried saving a plain text version to confirm the error is not being caused by an HTML nesting/labeling issue and got the same error with just my plain-text email:

Error: Invalid field Opportunity for SObject Opportunity

Here is the (scrubbed) Visualforce plainText:

<messaging:emailTemplate recipientType="Contact"
  relatedToType="Opportunity"
  subject="Closed Won! with {!relatedTo.Account}">
<messaging:plainTextEmailBody>
  Congrats Team!
  We have just signed {!relatedTo.Account} to a new Agreement.

  {!relatedTo.Owner}, please confirm existing opportunities are properly related to this parent opportunity. 

  Effective Start Date: {!relatedTo.Effective_Date__c}

  Sales Lead: {!relatedTo.Sales_Owner_Field_Updater__c}


  Client: {!relatedTo.Account}
  Opportunity Name: {!relatedTo.Name}      
  Opportunity Type: {!relatedTo.Type}

  Below is a list of Related Opportunities to the Opportunity

  Opp Link | Amount | Stage | Opp Type
  <apex:repeat var="oppx" value="{!relatedTo.Opportunity}">
  https://na12.salesforce.com/{!oppx.id} | {!oppx.Amount} | {!oppx.Stage} | {!oppx.Primary_Media_Opportunity__c}

  </apex:repeat>


  Opportunity details: https://na12.salesforce.com/{!relatedTo.Id}
  Account Details: https://na12.salesforce.com/{!relatedTo.AccountId}


  </messaging:plainTextEmailBody>

</messaging:emailTemplate>

Not sure why this is Error'ing out on me, I am not seeing an 'Opportunity' field being referenced anywhere at the Merge Field level, is this being caused by my markup trying to source related Opportunity records?

1 Answer 1

2

Your relatedToType in your template is set to "Opportunity" which means that the binding variable relatedTo will reference the Opportunity record. Then at the following line:

<apex:repeat var="oppx" value="{!relatedTo.Opportunity}">

you're basically trying to reference a field called "Opportunity" on the Opportunity record [Opportunity.Opportunity]. There isn't such a field.

If you're trying to get a related list of opportunities then I assume you have a custom field used as a lookup to the Opportunity object itself in order to create that hierarchy. You will need to find the child relationship name for your field (let's assume in this case the child relationship name is called "Opportunities"). Then you need to also append the "__r" suffix since it's a custom field. The end result should be something like this:

<apex:repeat var="oppx" value="{!relatedTo.Opportunities__r}">

6
  • Excellent answer! However, the related Opportunities form a related list on this Opportunity record type that the alert/email template would be sent out for. In other words, this Opportunity has multiple Opportunities associated/related to it, does it still make sense to reference the Lookup field?
    – GPP
    Jan 12, 2016 at 2:00
  • 1
    I'm not sure I understand your question. For opportunities which have multiple related opportunities (in the related list) this will create links for each one of the related records. If there are no related records, the apex:repeat won't run and that section in the template will be blank. Jan 12, 2016 at 2:19
  • 1
    That was exactly what I was asking, and your solution resolved my issue. Thanks so much!! :)
    – GPP
    Jan 12, 2016 at 3:06
  • btw, I had a question outside the scope of the one in OP: I want to use this VisualForce Email Template for Email Alerting; what "recipientType=" value should I use if I want the recipients to be defined in the 'Email Alert' Action?
    – GPP
    Jan 12, 2016 at 5:51
  • 1
    Please post this as a separate question on the site as per the guideliness. Jan 12, 2016 at 6:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.