Skip to Content
Menú
This question has been flagged
3 Respostes
1872 Vistes

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
Descartar
Best Answer

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
Descartar
Best Answer

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
Descartar
Best Answer

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
Descartar
Related Posts Respostes Vistes Activitat
1
d’abr. 23
8330
1
de des. 24
1211
2
de març 24
1681
0
d’oct. 21
1830
3
de gen. 20
3295