diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py
index 0d64ebf943c..06798b750dd 100644
--- a/addons/crm/models/crm_lead.py
+++ b/addons/crm/models/crm_lead.py
@@ -1285,15 +1285,18 @@ class Lead(models.Model):
help_title = _('Create a new lead')
else:
help_title = _('Create an opportunity to start playing with your pipeline.')
- alias_record = self.env['mail.alias'].search([
- ('alias_name', '!=', False),
- ('alias_name', '!=', ''),
- ('alias_model_id.model', '=', 'crm.lead'),
- ('alias_parent_model_id.model', '=', 'crm.team'),
- ('alias_force_thread_id', '=', False)
- ], limit=1)
-
- if alias_record.alias_domain and alias_record.alias_name:
+ alias_domain = [
+ ('company_id', '=', self.env.company.id),
+ ('alias_id.alias_name', '!=', False),
+ ('alias_id.alias_name', '!=', ''),
+ ('alias_id.alias_model_id.model', '=', 'crm.lead'),
+ ]
+ # sort by use_leads, then by our membership of the team
+ alias_records = self.env['crm.team'].search(alias_domain).sorted(
+ lambda r: (r.use_leads, self.env.user in r.member_ids), reverse=True
+ )
+ alias_record = alias_records[0] if alias_records else None
+ if alias_record and alias_record.alias_domain and alias_record.alias_name:
sub_title = Markup(_('Use the New button, or send an email to %(email_link)s to test the email gateway.')) % {
'email_link': Markup("%s") % (alias_record.display_name, alias_record.display_name),
}
diff --git a/addons/crm/tests/test_crm_lead.py b/addons/crm/tests/test_crm_lead.py
index 4f3d406b193..3e975ffb5bc 100644
--- a/addons/crm/tests/test_crm_lead.py
+++ b/addons/crm/tests/test_crm_lead.py
@@ -654,6 +654,70 @@ class TestCRMLead(TestCrmCommon):
# self.assertFalse(new_team.alias_id.alias_name)
# self.assertFalse(new_team.alias_name)
+ @users('user_sales_manager')
+ def test_crm_team_alias_helper(self):
+ """Test that the help message is the right one if we are on multiple team with different settings."""
+ # archive other teams
+ self.env['crm.team'].search([]).active = False
+
+ self._activate_multi_company()
+ team_other_comp = self.team_company2
+
+ user_team_leads, team_leads, user_team_opport, team_opport = self.env['crm.team'].create([{
+ 'name': 'UserTeamLeads',
+ 'use_leads': True,
+ 'member_ids': [(6, 0, [self.env.user.id])],
+ }, {
+ 'name': 'TeamLeads',
+ 'use_leads': True,
+ 'member_ids': [],
+ }, {
+ 'name': 'UserTeamOpportunities',
+ 'use_leads': False,
+ 'member_ids': [(6, 0, [self.env.user.id])],
+ }, {
+ 'name': 'TeamOpportunities',
+ 'use_leads': False,
+ 'member_ids': [],
+ }])
+
+ self.env['crm.lead'].create([{
+ 'name': 'LeadOurTeam',
+ 'team_id': user_team_leads.id,
+ 'type': 'lead',
+ }, {
+ 'name': 'LeadTeam',
+ 'team_id': team_leads.id,
+ 'type': 'lead',
+ }, {
+ 'name': 'OpportunityOurTeam',
+ 'team_id': user_team_opport.id,
+ 'type': 'opportunity',
+ }, {
+ 'name': 'OpportunityTeam',
+ 'team_id': team_opport.id,
+ 'type': 'opportunity',
+ }])
+ self.env['crm.lead'].with_user(self.user_sales_manager_mc).create({
+ 'name': 'LeadOtherComp',
+ 'team_id': team_other_comp.id,
+ 'type': 'lead',
+ 'company_id': self.company_2.id,
+ })
+
+ # archive our team one by one and check that we have the correct help message
+ teams = [user_team_leads, team_leads, user_team_opport, team_opport, team_other_comp]
+ for team in teams:
+ team.alias_id.sudo().write({'alias_name': team.name})
+
+ for team in teams:
+ with self.subTest(team=team):
+ if team != team_other_comp:
+ self.assertIn(f"{team.display_name}", self.env['crm.lead'].sudo().get_empty_list_help(""))
+ else:
+ self.assertNotIn(f"{team.display_name}", self.env['crm.lead'].sudo().get_empty_list_help(""))
+ team.active = False
+
@mute_logger('odoo.addons.mail.models.mail_thread')
def test_mailgateway(self):
new_lead = self.format_and_process(