Skip to Content
Menu
This question has been flagged
2 Replies
8332 Views

Hello I want to use for loop in email template so I created this template with for loop but for some reason, I'm getting an error

Failed to render template

UndefinedError: 'row' is undefined

for some reason, it doesn't recognize sale order lines.

        <record id="example_email_template" model="mail.template">
          <field name="name">Example e-mail template</field>
          <field name="email_from">${object.company_id and object.company_id.email or ''}</field>
          <field name="subject">Congratz ${object.name}</field>
          <field name="email_to">${object.email|safe}</field>
          <field name="lang">${object.lang}</field>
          <field name="model_id" ref="model_sale_order"/>
          <field name="auto_delete" eval="True"/>
      <field name="body_html">
            <![CDATA[
          <p>Dear ${(object.name)},<br/><br/>
          Good job, you've just created your first e-mail template!<br/></p>
              Regards,<br/>
              ${(object.company_id.name)}
                   <table>

                <tbody>

                <tr>

                <th style="width:135px">Product description</th>

                <th style="width: 85px;">Qty </td>

                </tr>

                % for row in object.order_line:

                <tr>

                <td> ${row.product_id.default_code}</td>

                <td style="text-align: center;">${row.product_uom_qty or ''}</td>


                </tr>

                %endfor

                <tbody>

                <table>
        ]]>
      </field>
       </record>
Avatar
Discard
Best Answer

Hi Grf,

I've just tested your e-mail template and altered it a little bit to make it more simplified. This code works fine for me:

<p>Dear ${(object.partner_id.name)},<br><br>
Good job, you've just created your first e-mail template!<br></p>

% for so_line in object.order_line:
  <li>
    Product: ${so_line.product_id.name} - QTY: $(so_line.product_uom_qty}
  </li>
% endfor

Regards,<br>
${(object.company_id.name)}&nbsp;<p></p><table></table>


Are you sure that you're doing this in the code editor and not just in plain text mode by the way? Otherwise Odoo cannot parse it:


Result of my code:


Regards,
Yenthe

Avatar
Discard
Best Answer

This is for someone ending up here from search engines. for loop with <table> tags do not work in Odoo email templates. Use <div> instead.

    <div style="display:table;">

Check this out https://github.com/odoo/odoo/issues/2166#issuecomment-162009387

Avatar
Discard

Thx. I get the same result.

Related Posts Replies Views Activity
1
Feb 24
921
1
Jul 23
1646
3
Dec 23
17567
1
Feb 22
4406
0
Aug 21
1528