Hi Community,
In my case i inherited the crm form and add one mamy2many field this name [Estimation Assign to] when i select the users from this field and save the record that time selected users are added in Add Followers and also send the mail. Now problem is that when i change the kanban card from one stage to another stage that time if in form estimation field assigned some users the mail are also send those users.
but i want to send mail only when i'm open the record and select the users from estimation field and then click on save button only that i want to sand mail. not when i change the kanban card from one stage to another stage.
if you know it please let me know.
See the code .py File
class CrmLead(models.Model):
_inherit = 'crm.lead'
_order = "create_date desc"
crm_entries_id = fields.Many2one('set.estimation')
estimation_id = fields.Many2many('res.users', default=lambda self:self.env.user, domain=lambda self: [("groups_id", "=", self.env.ref( "estimation.group_user_hide" ).id)], index=True, track_visibility='onchange', string='Estimation Asign To')
# Add Followers From Dropdown
@api.model
def create(self, vals):
lead_res = super(CrmLead, self).create(vals)
for rec in lead_res:
if rec.estimation_id:
partner_ids = []
for est_rec in rec.estimation_id:
if est_rec.partner_id and est_rec.partner_id.email:
user_name = self.env.user.name_get()[0][1]
partner_ids.append(est_rec.partner_id.id)
template_obj = self.env['mail.mail']
template_data = {
'subject': 'New Estimation Asign : ',
'body_html': "Hello,</br><h5>" + user_name + " invited you to follow Lead/Opportunity document : " + rec.name + "</h5>",
'email_from': self.env['mail.message']._get_default_from(),
'email_to': est_rec.partner_id.email
}
template_id = template_obj.create(template_data)
template_obj.send(template_id)
if partner_ids:
rec.message_subscribe(partner_ids, None)
return lead_res
@api.multi
def write(self, vals):
res = super(CrmLead, self).write(vals)
for rec in self:
if rec.estimation_id:
partner_ids = []
for est_rec in rec.estimation_id:
if est_rec.partner_id and est_rec.partner_id.email:
user_name = self.env.user.name_get()[0][1]
partner_ids.append(est_rec.partner_id.id)
template_obj = self.env['mail.mail']
template_data = {
'subject': 'New Estimation Asign : ',
'body_html': "Hello,</br><h5>" + user_name + " invited you to follow Lead/Opportunity document : " + rec.name + "</h5>",
'email_from': self.env['mail.message']._get_default_from(),
'email_to': est_rec.partner_id.email
}
template_id = template_obj.create(template_data)
print('===================To sent ==================', est_rec.partner_id.email)
template_obj.send(template_id)
rec.message_subscribe(partner_ids, None)
#message_unsubscribe
message_partner_ids = rec.message_partner_ids.ids
est_ids = [est_rec.partner_id.id for est_rec in rec.estimation_id] + [self.env.ref('base.partner_root').id]
unsub_partners = set(message_partner_ids) - set(est_ids)
if list(unsub_partners):
rec.message_unsubscribe(list(unsub_partners))
else:
print("+++++============= Else Part =============+++++")
return res