Email support@vrajatechnologies.com Teams Teams Contact +91 9099928101 Whatsapp +91 73596 28466 Linkedin Linkdin
  1. sum() Β β€” Add Values in Recordsets

βœ… What isΒ sum()?

The sum() function returns the total of numeric values in an iterable.

πŸ“Œ Realistic Example

Suppose you want to calculate the total amount from a list of sale orders.

total_amount = sum(self.mapped(‘amount_total’))

  • self is a recordset of sale.order

  • mapped(‘amount_total’) gives a list of totals

  • sum() adds all the totals together

βž• More Example:

total_qty = sum(self.order_line.mapped(‘product_uom_qty’))

This gives the total quantity of all products in the order lines.

2. len() β€” Count Records or Items

βœ… What isΒ sum()?

The len() function returns the number of items in a recordset or list.

πŸ“Œ Realistic Odoo Example

Suppose you want to know how many order lines a sale order has:

line_count = len(self.order_line)

This returns the number of lines in the current order.

βž• More Example:

Count how many customers have at least one confirmed sale order:

partners = self.env[‘res.partner’].search([])

confirmed_partners = partners.filtered(lambda p: p.sale_order_ids.filtered_domain([(‘state’, ‘=’, ‘sale’)]))

count = len(confirmed_partners)

βœ… Summary Table

FunctionPurposeExample Use Case
sum()Add numeric valuesTotal order amount, total quantity
len()Count number of items in record/listCount lines, records, filtered partners