Skip to Content
Menu
This question has been flagged

I want to notify all Maintenance Team members when a new request is created inside that particular team.

At the moment I have this functionality handled by an automated action like this:
https://i.imgur.com/ZbDstba.png

Python code:

body_html = """
...
""" + record.name + """
...
"""

num_of_members = len(record.maintenance_team_id.member_ids)

if num_of_members:
members_emails = []

for i in range(num_of_members):
members_emails.append(record.maintenance_team_id.member_ids[i].email)

email_to = ",".join(members_emails)

mail_pool = env['mail.mail']
values={}
values.update({'subject': 'New maintenance request - ' + record.company_id.name})
values.update({'email_to': email_to})
values.update({'body_html': body_html})
msg_id = mail_pool.create(values)

if msg_id:
mail_pool.send([msg_id])

But now I would like to convert this solution to a custom module. What is the correct way to do this? 

Should I inherit `maintenance.request`, override the create method and send my email somehow (how exactly?) with my hardcoded email body? 

class MaintenanceRequest(models.Model):
_inherit = 'maintenance.request'

@api.model
def create(self, vals):
req = super(MaintenanceRequest, self).create(vals)

body_html = """
...
""" + req.name + """
...
"""
# ...
if msg_id:
mail_pool.send([msg_id])

return req

Or there is a way to hook myself to a premade function for sending notification and just tell it to run also for the team members?  I don't want to add all of them as followers (because they would get spammed with unnecessary updates about the request) - only to notify them about the new request and then they can follow it if they needed to.

Avatar
Discard
Related Posts Replies Views Activity
2
Nov 22
4365
1
Jun 22
6771
1
Sep 21
2044
0
Aug 21
95
0
Jul 21
172