Skip to Content
Menu
This question has been flagged
1 Reply
1040 Views

in Automation Rules we do not have Triggers like Before Create but there is On Save which work fine for creating record(s) in another table(s),  but if there is no relation field for Action Create Record and also if i want to create parent-child records how it can be done?

i tried using Execute Code as below but it throws error.

Model : Approval Request

Trigger : On Save

Execute Code:

    if record.request_status=='approved' and record.category_id in (8, 9, 13):

        # Create stock.picking record

        picking_data = {

            'picking_type_id': 1,

            'company_id': 1,

            # Add other required fields

        }

        stock_picking = env['stock.picking'].create(picking_data)

    

        for approval_line in record.approval_product_line: 

            # Create stock.move record

            move_data = {

                'picking_id': stock_picking.id,

                'product_id': approval_line.product_id.id,

                'product_uom_qty': approval_line.quantity,

                'company_id': 1,

                'product_uom': 1,

                # Add other required fields

            }

        stock_move = env['stock.move'].create(move_data)


error for stock.move:

ValueError: : "'approval.request' object has no attribute 'approval_product_line'" while evaluating


this is my first attempt to create a parent-child record using python code in Automation Rule, i don't know how to about the child side records in this scenario.

can somebody please help with correction in code for stock.move where i want to create Line records which can be multiple as i am totally blank here.

regards


please note: there is an another Automation Rule exists for On Save trigger for same Model mentioned above.


Avatar
Discard
Author Best Answer

it has been done, i am using below code to create Internal Transfer records successfully using below code, the juniors like me can get benefit. request to all juniors please at least respond with counter question(s) if someone misunderstood or approach is wrong like Oracle related forum seniors.

-----

#raise UserError(f'{record.request_status} - {record.category_id.id}')

if record.request_status=='approved' and record.category_id.id in (8, 9, 13):

    new_stock_picking = env['stock.picking'].create({

      'picking_type_id': 1,

      'company_id': 1,

    })


    for approval_line in record.product_line_ids: 

        # Create stock.move record

        move_data = {

            'picking_id': new_stock_picking.id,

            'picking_type_id': 5,

            'product_id': approval_line.product_id.id,

            'product_uom_qty': approval_line.quantity,

            'name': approval_line.product_id.name,

            'company_id': 1,

            'product_uom': 1,

            'location_id': 1,

            'location_dest_id': 1,

            # Add other required fields

        }

        stock_move = env['stock.move'].create(move_data)

the 'picking_type_id': 5  it means picking type is Internal Transfer, system will use INT sequences for this type of entries.

regards

Avatar
Discard
Related Posts Replies Views Activity
4
May 25
2429
2
May 25
5763
1
Mar 25
1612
4
Mar 25
4404
3
Feb 25
5381