首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >odoo-12 :如何解决写()方法问题

odoo-12 :如何解决写()方法问题
EN

Stack Overflow用户
提问于 2019-10-31 11:12:09
回答 1查看 615关注 0票数 1

在我的例子中,我继承了crm表单,并在从该字段选择用户时添加了一个mamy2many字段,这个名称估计分配给它,并保存了添加关注者和发送邮件的时间记录。现在的问题是,当我把看板卡从一个阶段更改到另一个阶段时,如果在表单估计字段中分配了一些用户,邮件也会发送给那些用户。

但我只想发送邮件时,我打开记录,并选择用户从估计字段,然后单击保存按钮,只有我想要沙子邮件。不是当我把卡从一个阶段改到另一个阶段时,不是把它从一个阶段改为另一个阶段的时候,不是把它从一个阶段改为另一个阶段.

如果你知道的话请告诉我。

代码语言:javascript
复制
@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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-31 14:08:47

尝试添加另一个条件,以便在estimation_id更改时发送邮件。

代码语言:javascript
复制
if u'estimation_id' in vals and rec.estimation_id:

编辑

以下代码将计算新添加的用户:

代码语言:javascript
复制
user_ids = {rec.id: [user_id.id for user_id in rec.estimation_id] for rec in self}
res = super(CrmLead, self).write(vals)
for rec in self:
    new_user_ids = [user.id for user in rec.estimation_id if user.id not in user_ids[rec.id]]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58642124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档