2

can't figure out how to add a line to sale.subscription using the create function from another model

   subscription_pak = self.env['product.template'].search([('name', '=', pak_name),('recurring_invoice', '=', True)], limit=1)
   subscription_info = {
      'partner_id': vals['name'],
   }    
   add_subscription = self.env['sale.subscription'].create(subscription_info)    

   print('ssssss',subscription_pak)
   #works


   add_subscription_pak = {
     'product_id': subscription_pak.id,
     'partner_id': add_subscription.id,
   }    
   link_user_to_subscription = self.env['sale.subscription.line'].create(add_subscription_pak)

I am creating the subscription but can't find the field to use add product to the lines

can you please help

bigbear3001
  • 522
  • 8
  • 19
Moaz Mabrok
  • 697
  • 10
  • 32

2 Answers2

2

Thanks to odoo Mates on YouTube, this video his from odoo Mates channel How To Update One2many Field From OnChange Of Field in Odoo and @bigbear3001

this is what worked for me

        supsctiption_pak = self.env['product.product'].search([('name', '=', pak_name),('recurring_invoice', '=', True)], limit=1)
        supsctiption_info = {
            'partner_id': vals['name'],
        }    
        add_supsctiption = self.env['sale.subscription'].create(supsctiption_info)

        supsctiption_to_pak = self.env['sale.subscription'].search([('partner_id', '=', vals['name'])], limit=1)


        add_supsctiption_pak = {
                'product_id': supsctiption_pak.product_variant_id.id,
                'uom_id': supsctiption_pak.uom_id.id,
                'price_unit': supsctiption_pak.list_price,
            }    
        supsctiption_to_pak.write({'recurring_invoice_line_ids':[(5, 0, 0),(0,0,add_supsctiption_pak)]})

Moaz Mabrok
  • 697
  • 10
  • 32
1

your subscription_pak is of type product.template (Product Template) but the product_id field of sale.subscription.line requires a product.product (Product (Variant)) (can't link to it as it's Odoo Enterprise)

so this should work (if you only have one variant on the Product):

   ...
   add_subscription_pak = {
     'product_id': subscription_pak.product_variant_id.id,
     'partner_id': add_subscription.id,
   }
   ...    

for multiple variants:

   ...
   add_subscription_pak = {
     'product_id': subscription_pak.product_variant_ids.filter(lambda pv: pv.attribute == 'value')[0].id,
     'partner_id': add_subscription.id,
   }
   ...    
bigbear3001
  • 522
  • 8
  • 19
  • thank you for ur reply I am geting this error `Something went wrong ! Record does not exist or has been deleted. (Record: product.product(154,), User: 2)` @bigbear3001 – Moaz Mabrok Jun 19 '20 at 06:57
  • Can your user see the product template and variant in the backend? Do you have multicompany enabled and does the product variant belong to the same company? (See restrictions on product_id field for `sale.subscription.line`) Also the partner_id field on `sale.subscription.line` doesn't seem to be standard. Can you provide the definition for it? – bigbear3001 Jun 19 '20 at 07:10
  • This is ` _name = "sale.subscription.line"` for the class `SaleSubscriptionLine` the user is admin and the product is in the same company – Moaz Mabrok Jun 19 '20 at 07:14
  • Yes thats the name. But i don't have the definition for partner_id in the source code of `sale.subscription.line` for odoo 13.0 It should look like this: ``` ... _name = "sale.subscription.line" ... partner_id = fields... ... ``` But do not paste enterprise code here. The partner_id was present in at least version 11.0 but it's not in version 13.0 (the tag for your questions states version with odoo-13. – bigbear3001 Jun 19 '20 at 07:22
  • The more important questions are the one if you can see the product in the backend and if you are working with multicompany and the correct company is set. As the subscription lines depend on companies and enforce them. – bigbear3001 Jun 19 '20 at 07:22
  • I can add them manually from the backend and I am on the company that created those products – Moaz Mabrok Jun 19 '20 at 07:25
  • Can you make sure the context your code is running in is also running for the right company? Just to make sure: `self = self.with_context(active_company_id=select_correct_company_id.id)` If that's not it i am not able to help you further. I would switch to debugging at this time. – bigbear3001 Jun 19 '20 at 07:29
  • (can't link to it as it's Odoo Enterprise) owoo sorry I am using odoo Enterprise @bigbear3001 – Moaz Mabrok Jun 19 '20 at 10:15