Odoo: Create function creates multiple subscriptions, but I only want one subscription to be created

136 views Asked by At

My Code is creating multiple subscriptions right now and it is confusing and not expected. I am trying to create a subscription from a sales order, while odoo does provide that automatically in my cas I am creating a subscription from a sales order which containes a product kit/bundle with a service product. Odoo does not automatically create a subscription in this case for the service product.

With the automated action below I am trying to create a subscription based on the condition above (product is a kit and has a service product), but my code creates multiple subscription and I want only for every service product. I have already checked if the amount of service products is equivalent to the amount of subscriptions, but it isnt. The number of subscriptions far exceed the amount of service product. Where is my mistake and how do I fix it.

Also i am trying to add the service product to my subscription but I dont know how (tried write function but wont work).

The automated action settings are in the pic below: Automated action settings

for record in records:
  vals = {
    'company_id': record.company_id.id,
    'partner_id': record.partner_id.id,
    'pricelist_id': record.pricelist_id.id,
    'template_id': record.order_line.product_id.bom_ids.bom_line_ids.product_id.subscription_template_id.id,
    'name': 'Name'
  }

sub = env['sale.subscription'].create(vals)

for record in records:
  for prod in record.order_line.product_id.bom_ids.bom_line_ids.product_id:
      
    if prod.recurring_invoice is True:
      sub_product = prod

      values ={
      'price_unit': sub_product.list_price,
      'product_id': sub_product.product_variant_id.id,
      'uom_id': record.order_line.product_uom_qty,
      'name': 'Name'
      }
    
  sub.write({'recurring_invoice_line_ids':[(5, 0, 0),(0,0,values)]})
0

There are 0 answers