This question has been flagged

I am trying to send a mail to the portal user who've successfully submitted a request and I'm getting this error KeyError: False.

These are my controller and template codes. 


@http.route('/submit', method='post', type='http', auth='public', website=True, csrf=False)
def send_request(self, **post):

values = {
'name': post['subject'],
'maintenance_team_id': post['teams'],
'equipment_id': post['equipment'],
'description': post['details'],
'priority': post['stars'],

}

print(values)
# maintenance_request = self.env['maintenance.request'].search()
request.env['maintenance.request'].sudo().create(values)
search_ids = request.env['maintenance.request'].search([])
last_id = search_ids and max(search_ids)
print(last_id.id)
template = request.env.ref('website_maintenance.mail_template_maintenance_request')
print(template)
template.send_mail(last_id.id, force_send=True)
return redirect('/maintenance_request-thanks')


template


<odoo>
<data>
<record id="mail_template_maintenance_request" model="mail.template">
<field name="name">Maintenance: Reception Acknowledgment</field>
<!-- <field name="model_id" ref="website_maintenance"/>-->
<field name="subject">Maintenance request received</field>
<field name="email_to">robodoo2019@gmail.com</field>
<field name="body_html"><![CDATA[
<div>
<!-- Just trying to send an empty mail first -->
</div>]]>
</field>
</record>
</data>
</odoo>


What am I doing wrong here?

Avatar
Discard
Best Answer

Hi,

Just make sure that you are getting values in the last_id and template variable. Can see the print statements you have added, just check the log and ensure the values are getting in the variable.

search_ids = request.env['maintenance.request'].search([])
last_id = search_ids and max(search_ids)

With the above code if you are looking to get the record created in this line,

request.env['maintenance.request'].sudo().create(values)


You can easily get it like this,

last_id = request.env['maintenance.request'].sudo().create(values)

Thanks

Avatar
Discard
Author

Thank you for the answer, I can now send mail as admin(internal user), but cant do it as a portal user. And I'm getting the following error:[[ I dont use a model or a security csv file for this custom module. ]]

odoo.exceptions.AccessError: ('Sorry, you are not allowed to access this document. Only users with the following access level are currently allowed to do that:\n- User types/Internal User\n\t- Administration/Settings\n\n(Document model: mail.template) - (Operation: read, User: 8)', None)

Either use sudo to send email or give access rights for portal user.

Odoo security: https://www.youtube.com/watch?v=mzg3EGD_6Gw

Author

I've used sudo and got the desired output. Thank you for all the help :)