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
Stephen RossStephen Ross 

How do I stop apex:repeat from returning duplicate lines in an email alert template??

I know close to ZERO about Apex code so trying to edit the existing Visualforce/apex code in our email template is crazy hard, but what I'm trying to do it limit the duplication of return data to one unique line per product.

Here is the code within our email template:

<table border="0" >
                 <tr >
                     <th>Quantity</th><th>Product Name</th>
                  </tr>
    <apex:repeat var="opp" value="{!relatedTo.OpportunityLineItems}">
       <tr>
            <td>{!ROUND(opp.Quantity,0)}</td>
            <td>{!opp.PriceBookEntry.name}</td><br/>
       </tr>
    </apex:repeat> 
    </table>

We implemented a new CPQ tool that now shows each line item x the number of years on the contract, rather than one line item per the contract.  Meaning, instead of this portion of our email alert showing (using a 3 year term):

QTY  Product Name
1        Product 1
5        Product 2
1        Prodcut 3

It now shows:

QTY   Product Name
1        Product 1
1        Product 1
1        Product 1
5        Product 2
5        Product 2
5        Product 2
1        Prodcut 3
1        Prodcut 3
1        Prodcut 3

Anything anyone can offer an Apex newbie would be greatly appreciated!
Raj VakatiRaj Vakati
Hi Ross,
Please Modify your Apex controller to return the Aggregate group by Product Name and use the wrapper class to return the list of records. 
If you can share the apex class I can rewrite it 
Thanks ,
Raj
Stephen RossStephen Ross
Hi Rajamohan,

I know very little about APEX code but this is the entire email template code:

<messaging:emailTemplate recipientType="Contact"
    relatedToType="Opportunity"
    subject="Closed/Won Opportunity Alert - {!relatedTo.name}" >
   
<messaging:htmlEmailBody >       
    <html>
        <body>
         <STYLE type="text/css">
               TH {font-size: 12px; font-face: arial;background: #CCCCCC; border-width: 1;  text-align: center }
               TD  {font-size: 12px; font-face: verdana }
               TABLE {border: solid #CCCCCC; border-width: 1}
               TR {border: solid #CCCCCC; border-width: 1}
         </STYLE>
                  <font face="arial" size="2">
        The following opportunity has been Closed/Won:
        <br/>
        <br/><b>Opportunity Name:</b> {!relatedTo.name} (<td><a href="https://sciquest.my.salesforce.com/{!relatedTo.id}">View</a></td>)
        <br/>
        <br/><b>Opportunity Owner:</b> {!relatedTo.owner.name}
        <br/><b>Account Name:  </b>  {!relatedTo.Account.name}
        <br/><b>Opportunity Type:</b> {!relatedTo.Opportunity_Type__c}
        <br/><b>Opportunity Tier:</b> {!relatedTo.Tier__c}
        <br/><b>Solution Engineer(s):</b> {!relatedTo.Sales_Engineer__c}
        <br/><b>Pre-Sales Support:</b> {!relatedTo.Services_Support__c}
        <br/><b>Value Consultant(s):</b> {!relatedTo.Value_Consultant_s__c}
        <br/>
        <br/><b>Industry: </b> {!relatedTo.Account.Industry}
        <br/><b>Industry Sub Classification: </b> {!relatedTo.Account.Industry_Sub_Classification__c}
        <br/><b>Competitors:</b> {!relatedTo.Competitors__c}
        <br/>
               <br/>
                <br/><b>Year 1 Contract Value:</b> -- $
            <apex:outputText value="{0, number, ###,###,###,##0}">
              <apex:param value="{!relatedTo.Year_1_Contract_Value__c}"/>
            </apex:outputText>
        <br/><b>Year 1 License Value:</b> --- $
            <apex:outputText value="{0, number, ###,###,###,##0}">
              <apex:param value="{!relatedTo.Year_1_License_Value_CPQ__c}"/>
            </apex:outputText>
        <br/><b>Renewal Amount:</b> --------- $
            <apex:outputText value="{0, number, ###,###,###,##0}">
              <apex:param value="{!relatedTo.Other__c}"/>
            </apex:outputText>
        <br/><b>Services Value:</b> ------------ $
            <apex:outputText value="{0, number, ###,###,###,##0}">
              <apex:param value="{!relatedTo.Services_Value__c}"/>
            </apex:outputText>
        <br/><b>Total Contract Value:</b> ---- $
            <apex:outputText value="{0, number, ###,###,###,##0}">
              <apex:param value="{!relatedTo.Total_Contract_Value_CPQ__c}"/>
            </apex:outputText>
        <br/><b>Fixed Bid Travel Amt:</b> --- $
            <apex:outputText value="{0, number, ###,###,###,##0}">
              <apex:param value="{!relatedTo.Fixed_Bid_Travel_Amount_if_any__c}"/>
            </apex:outputText>
        <br/>
        <br/>
    <table border="0" >
                 <tr >
                     <th>Quantity</th><th>Product Name</th>
                  </tr>
    <apex:repeat var="opp" value="{!relatedTo.OpportunityLineItems}">
       <tr>
            <td>{!ROUND(opp.Quantity,0)}</td>
            <td>{!opp.PriceBookEntry.name}</td><br/>
       </tr>
    </apex:repeat> 
    </table>
        
    <apex:repeat var="Pdata" value="{!relatedTo.Partners_Data__r}">
   <br/><b>  Partner Role :</b>  {!Pdata.Role__c}
   <br/><b>  Partner Account:</b> {!Pdata.Partner_Account__r.Name}
   <br/>
   <br/>
   </apex:repeat>
   
<br/>   
         <br/>
   
                 <table border="0" >
                 <tr >
                     <th>Name</th><th>Services Net</th><th>Discount</th><th>ASO Additional Info</th>
                  </tr>
         
   
                <apex:repeat var="svcs" value="{!relatedTo.Services__r}">
                <tr>
   
                    <td>{!svcs.Name}</td>
                    <td>{!svcs.Services_Net__c}</td>
                    <td>{!svcs.Discount__c}</td>
                    <td>{!svcs.ASO_Services_Training_Products__c}</td>
                </tr>
                </apex:repeat>             
       </table>
       <br/>
       Professional Services Information:
      <br/><b>Login Contact Name:</b> {!relatedTo.Login_Contact_Name__c}
        <br/><b>Login Contact Email Lookup:</b> {!relatedTo.Login_Contact_Email_Lookup__c}
       <br/>
   
      
 </font>
      
        </body>
    </html>
</messaging:htmlEmailBody>
   
<messaging:plainTextEmailBody >
The following opportunity has been Closed/Won:
 
Opportunity: {!relatedTo.name}
Link: https://sciquest.my.salesforce.com/{!relatedTo.id}
Opportunity Owner: {!relatedTo.owner.name}
Account Name: {!relatedTo.Account.name}
Opportunity Type: {!relatedTo.Opportunity_Type__c}
Opportunity Tier: {!relatedTo.Tier__c}
Solution Engineer(s): {!relatedTo.Sales_Engineer__c}
Pre-Sales Support: {!relatedTo.Services_Support__c}
Value Consultant(s): {!relatedTo.Value_Consultant_s__c}
Competitors: {!relatedTo.Competitors__c}
Year 1 Contract Value:  --- ${!ROUND(relatedTo.YR1CV_CPQ__c,0)}
Year 1 License Value:  ---- ${!ROUND(relatedTo.Year_1_License_Value_CPQ__c,0)}
Renewal Amount:  ---------- ${!ROUND(relatedTo.Other__c,0)}
Services Value:  ---------- ${!ROUND(relatedTo.Services_Value__c,0)}
Total Contract Value:  ---- ${!ROUND(relatedTo.Total_Contract_Value_CPQ__c,0)}
Fixed Bid Travel Amt:  ---- ${!ROUND(relatedTo.Fixed_Bid_Travel_Amount_if_any__c,0)}
       
Qty ---- Product Name
<apex:repeat var="opp" value="{!relatedTo.OpportunityLineItems}">
{!ROUND(opp.Quantity,0)} ---- {!opp.PriceBookEntry.name}
</apex:repeat>
</messaging:plainTextEmailBody>      
       
</messaging:emailTemplate>


I'm not sure if that answers your question but that's about all I know to do at the moment.  Sorry!

Thanks,
Stephen