This question has been flagged

I'm developing a custom module that adds some functionality to the crm.lead, this module let you set a product on the lead when a customer choose a product to buy, a special requirement for our customer:

class CRMLeadProduct(models.Model):
    _inherit = ["crm.lead"]
    product_id = fields.Many2one('product.template',)


The problem is when the sale person press "new quotation" button, it necessary when that button is pressed the sale order must have and order line with the product from the lead, I try to do this:


class SaleOrder(models.Model):
    _inherit = 'sale.order'
    def _get_lead_product(self):
        product_id = self.env['product.template'].search(
            [('id', '=', self.opportunity_id.product_id.id)])
        return [(4, {'order_id': self.id, 'product_id': product_id.id, 'product_uom_qty': 1, 'product_uom': 1, })]
    order_line = fields.One2many(default=_get_lead_product)


But as the order still doesn't exist we can't set the order line, how can we accomplish this?

 

Avatar
Discard
Best Answer

You should do it from the onchange of the opportunity field.

Ex:

@api.onchange('opportunity_id')
def onchange_opportunity(self):
if self.opportunity_id:
product = self.env['product.template'].search([('id', '=', self.opportunity_id.product_id.id)])
return [(0, 0, {'product_id': product.id, 'product_uom_qty': 1, 'product_uom': 1})]

Hope this will solve your issue.




Avatar
Discard
Author

Thanks, Just in time.!!!