Skip to Content
Menu
This question has been flagged

I need an automated action python code that does the following:

Establish a relation between the sale.order.line model and the project.task model. I added a new field in the sale.order.line with technical name : x_studio_phase_name_1 and its a char field. We should write a condition that if the name of the task is similar to the name I recorded in the sale.order.line, then we need to update the quantity field in the sale.order.line model to be equal to the value of a newly created field in the project.task model under the technical name of : x_studio_area_1

This is the code I have done but its not working:

order_lines = record.env['sale.order.line'].search([])


for order_line in order_lines:

if order_line.x_studio_phase_name_1:

tasks = record.env['project.task'].search([

('project_id.sale_order_id', '=', order_line.order_id.id),

('name', '=', order_line.x_studio_phase_name_1)

])


if len(tasks) == 1:

task = tasks[0]

if order_line.order_id.state == 'sale':

order_line.write({'product_uom_qty': task.x_studio_area_1})

 

Avatar
Zrušiť
Best Answer

try this way:
order_lines = self.env['sale.order.line'].search([])


for order_line in order_lines:

if order_line.x_studio_phase_name_1:

tasks = self.env['project.task'].search([

('project_id.sale_order_id', '=', order_line.order_id.id),

('name', '=', order_line.x_studio_phase_name_1)

])

if tasks:

task = tasks[0] # Assuming you want to consider only the first matching task

order_line.quantity = task.x_studio_area_1

we first search for all the sale.order.line records. For each order line, we check if the x_studio_phase_name_1 field has a value. If it does, we search for project.task records based on the conditions you provided, and if there's a matching task, we update the quantity field of the order line with the value of x_studio_area_1 from the task.

Avatar
Zrušiť
Best Answer

Hi 


Try this Code

order_lines = env['sale.order.line'].search([])
tasks = env['project.task'].search([])
for line in order_lines:
for task in tasks:
if line.x_studio_phase_name_1 == task.name:
line.product_uom_qty = task.x_studio_area_1


Regards

Avatar
Zrušiť
Related Posts Replies Zobrazenia Aktivita
3
máj 25
2547
0
feb 24
1614
2
okt 23
1782
1
júl 23
1990
0
jún 24
2373