Se rendre au contenu
Menu
Cette question a été signalée
3 Réponses
1877 Vues

Hello everybody,


I would like to know how to automatically change the name of an opportunity in CRM.

I would like this opportunity to look like REN/01022024/RANDOM_NUMBERS_OR_LETTERS


I think we need to go through automation but I don't see how to do it

Avatar
Ignorer
Meilleure réponse

It can also be done with an Automated Action.  

You have to create new Sequence numbering and then some simple Python code in the Automated Action.

Sequence Numbering:  


The Python code in the Automated Action will be something like this:

for record in records:
  ref = env['ir.sequence'].next_by_code('crm.lead.sequence')
  record['name'] = ref

This example provides more details: https://odootricks.tips/set-sequence-numbers-for-sales-orders/

Avatar
Ignorer
Meilleure réponse

Hi,

Please utilise the provided code below to update the opportunity name.

from odoo import models, fields, api

import random

from datetime import datetime


class CRMOpportunity(models.Model):

    _inherit = 'crm.lead'


    name = fields.Char(string="Opportunity Name", compute="_compute_custom_opportunity_name", store=True)


    @api.depends('date_deadline')

    def _compute_custom_opportunity_name(self):

        for opportunity in self:

            # Format date as "DDMMYYYY"

            formatted_date = datetime.strptime(opportunity.date_deadline, '%Y-%m-%d').strftime('%d%m%Y')

            

            # Generate random letters/numbers (you may adjust this based on your requirements)

            random_part = ''.join(random.choices('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', k=6))


            # Combine the elements

            opportunity.name = f'REN/{formatted_date}/{random_part}'

Regards

Avatar
Ignorer
Meilleure réponse

Hello Galien, 

For this you need to generate the sequence. you can follow this community answer : https://www.odoo.com/forum/help-1/create-a-sequence-number-internal-reference-for-project-task-110287


Thanks

Avatar
Ignorer
Publications associées Réponses Vues Activité
1
avr. 23
8335
1
déc. 24
1214
2
mars 24
1694
0
oct. 21
1835
3
janv. 20
3295