Use Server Actions to copy (and delete)

An interesting use of Server Actions to copy (and delete) multiple records. This is another one from Jake Robinson

Start by enabling Developer mode and navigating to Settings / Technical / Server Actions:

for rec in records:
  rec.copy()
  record['state'] = "cancel"
  rec.unlink()

Note: This uses the standard copy() ORM method, and so (by design) it will not copy all fields.

You need to enter:

1. The model (table) – and this should work with any model

2. Action To Do = “Execute Python Code”

3. Enter the Python code as shown above. The second line sets the state to “cancel” because you can only delete a Production Order that has been cancelled. Different rules apply to different models (tables).

Click on “Create Contextual Action” and now the option is available

Beware: this is very powerful!!

Of course you can simply copy and not delete, in which case your Python code will be:

for rec in records:
  rec.copy()

Tip: you may have noticed that Server Actions and Automated Actions are very similar.

All Automated Actions are shown in the list of Server Actions and from there you can create a contextual action for them!

4 thoughts on “Use Server Actions to copy (and delete)

  1. first of all, excellent blog. Very useful. i am using server action to copy a few selected lines from a customized model (with studio) to one invoice. however, it is not grouping as expected. Can you share some tips for me please?

    case_lines=[]
    for case_line in records:
    case_lines.append([0,0,{
    ‘x_studio_case’:case_line.x_studio_case_id,
    ‘x_studio_business_unit’: case_line.x_studio_billing_centre,
    ‘product_id’:case_line.x_studio_field_8Cfba.id,
    ‘name’:case_line.x_studio_third_party_name_1,
    ‘x_studio_billing_details’:case_line.x_studio_billing_details,
    ‘quantity’:1,
    ‘price_unit’: case_line.x_studio_price,
    ‘account_id’: case_line.x_studio_field_8Cfba.property_account_income_id.id
    }])
    env [‘account.invoice’].create({
    ‘partner_id’:records.x_studio_field_hry7Z[0].id,
    ‘currency_id’:records.x_currency_id[0].id,
    ‘invoice_line_ids’:case_lines
    })

    Like

  2. Bonjour ,
    Je trouve votre blog très instructif , j’aimerais savoir si je peut utiliser l ‘action du serveur pour mettre à jour automatiquement un champs date dans mon formulaire selon une fréquence très précise.

    Like

    1. Sorry, my French is not so good, but yes it should be possible to have a Server Action to update dates automatically. Can you give an example of what you want to achieve?

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s