Skip to Content
Menu
This question has been flagged
2 Replies
204 Views

Hi All,

I have created a boolean field in sale order. 

is_primary_quotation = fields.Boolean(string='Primary Quotation', default=False) 
and in crm model have a realtionship. 

sale_order_ids = fields.One2many('sale.order', 'opportunity_id', string='Sales Orders') 

and I add below addition logic 




File "D:\odoo-18.0+e.20250604(New)\odoo\fields.py", line 1232, in __get__ record.ensure_one() File "D:\odoo-18.0+e.20250604(New)\odoo\models.py", line 6255, in ensure_one raise ValueError("Expected singleton: %s" % self) ValueError: Expected singleton: sale.order(68, 59).

how to solve this error so that I'll geting MRR and NRR value is the quote is primay quote of the lead?

Avatar
Discard
Best Answer

Hi,


Try the following code.


@api.depends('sale_order_ids.non_recurring_total', 'sale_order_ids.is_primary_quotation')

def _compute_expected_revenue(self):

    for lead in self:

        primary_orders = lead.sale_order_ids.filtered(lambda o: o.is_primary_quotation)

        lead.expected_revenue = sum(order.non_recurring_total for order in primary_orders)



@api.depends('sale_order_ids.recurring_monthly', 'sale_order_ids.is_primary_quotation')

def _compute_recurring_revenue(self):

    for lead in self:

        primary_orders = lead.sale_order_ids.filtered(lambda o: o.is_primary_quotation)

        lead.recurring_revenue = sum(order.recurring_monthly for order in primary_orders)


Hope it helps

Avatar
Discard
Best Answer

sale_order_ids in your if conditions

if lead.sale_order_ids.is_primary_quotation

is not a single Sale Order, because 

  1. sale_order_ids = fields.One2many() - thus either no record, one record or many records
  2. you've probably never told sale_order_ids to be one specific record, i.e. by applying an actual search domain

See the domain attribute for One2many https://www.odoo.com/documentation/18.0/developer/reference/backend/orm.html#odoo.fields.One2many or compute the field.

Apart from that I don't think much can be said since your source doesn't really add up - to me at least. is_primary_quotation implies to me that there should be a single primary Sale Order only - but primary in regard to what? The Lead, a Customer, ...? You probably want to apply some logic that actually enforces is_primary_quotation to be possible to be set only once for any given sale.order linked to a crm.lead, i.e. by checking in create() and write() of sale.order whether any lead_id.sale_order_ids.filtered(lambda so: so.is_primary_quotation) is there already.

Avatar
Discard
Author

I want to update MRR and NRR for one quote, for that I create a boolean fields that will mark as primary quote.
If it is primary quote, then only it should update MRR and NRR in the respective lead.
I added the is_primary_quotation boolean field to identify and update the MRR and NRR in the lead.

My error is still not resolved bcz we can generate multiple quote from a lead.

Related Posts Replies Views Activity
0
May 24
855
1
Dec 18
3345
0
Apr 23
1788
1
Apr 20
3365
2
May 25
2965