0

This is the error that I'm faced today, and have no idea where it comes from!

Error occurred trying to load the template for preview: List has no rows for assignment to SObject. Please try editing your markup to correct the problem.

I'm trying to create an Email Template for Order Confirmation email, to just ordered items.

These are all in sandbox.

In Email Template section, button [ Send Test and Verify Merge Fields ] does not show the test Orders that I created.

Edit: I managed to fix the error, but I still can't choose any test orders, it won't show them in look up window.

These are the steps I took (I use lightning experience):

Setup / Email / Classic Email Templates:

  • added folder "Order Email Templates"

  • new template / type: visualforce

template content:

<messaging:emailTemplate subject="sub b" recipientType="Contact" relatedToType="Order">
<messaging:plainTextEmailBody >

<c:OrderContentCmp ></c:OrderContentCmp>

</messaging:plainTextEmailBody>
</messaging:emailTemplate>

codes:

visualforce component named "OrderContentCmp"

<apex:component controller="OrderContentController" access="global">

    <h3>Deer {!record.BillToContact.Name},</h3>
    <p>Tank u for buying stuff, and yes this means if we don't review our email content, we're doomed.</p>
    <hr/>

    <h3>Order Details</h3>
    <hr/>
    <p>Order # {!record.OrderNumber}</p>
    <p>Placed on {!record.CreatedDate}</p>

    <!-- ITEMS -->

</apex:component>

controller

public with sharing class OrderContentController {

    private String mOrderId;
    public Order record { get; private set; }

    public List<OrderItem> items { get; private set; }

    public OrderContentController() {
        mOrderId = ApexPages.currentPage().getParameters().get('id');
        record = [SELECT Id,OrderNumber,BillToContact.Name,Account.Name FROM Order WHERE Id=:mOrderId];
        items = [SELECT Id,Product2.Name,Quantity,UnitPrice,TotalPrice FROM OrderItem WHERE OrderId=:mOrderId];
    }

}
1

A better solution is:

<c:OrderContentCmp orderId="{!relatedTo.Id}"/>

and the component itself

<apex:component controller="OrderContentController" access="global">
  <apex:attribute name="orderId" assignto="{!recordId}" type="ID" 
     description="..." required="true"/>
  .... markup
  ... markup for items uses this notation (or datatable equiv) 
    <apex:repeat value="{!record.OrderItems}" var="oi">
        ...
    </apex:repeat>
</apex:component/>

and the VF component controller

public with sharing class OrderContentController {
   public ID recordId {get; set;}
   public Order record {
     get {
         return recordId == null
           ? new Order() // avoid exceptions in Preview
           : [select Id, ... , 
               (select id, ... FROM OrderItems)
              FROM Order where Id = :recordId][0];
         }
     private set;

}

The component should be passed all the information it needs via the argument list as defined by apex:attribute and not rely on how / where the component is embedded

The reason for your error is that in Preview, there's no value for the Page Parameter Id and your controller bombs out. The solution I provide above deals with that issue

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.