Automated Action to make a copy and remove attachment

This is another example of using Automated Actions and demonstrates some useful functionality (though the example may not be a very good one – I’ll try to update this later).

The requirement is to make a copy of a Lead (in CRM) and then remove an attachment (so you will have the archived Lead with the attachment, and the original without).

For this example, we’ll add a new image (binary) field) to the Lead. Note that the field must be set as “copied” (as shown below)

We’ll add an Odoo logo as an attachment.

Create an Automated Action

  • Model = crm.lead (Lead / Opportunity)
  • Trigger Condition = On Update
  • Watched Field: stage_id (Stage)
  • No domains (but these could be added)
  • Action To Do: Execute Python Code
  • Python Code:

for rec in records:
    old_name = ""
    rec['active'] = False
    old_name = rec.name
    rec['name'] = record.name + " (archived)"
    rec.copy()
    rec['name'] = old_name
    rec['x_studio_image'] = False
    rec['active'] = True

Explanation of Python code

  1. The first line uses a fairly standard technique to loop through the records
  2. Clears the temporary field old_name
  3. Sets the record as ‘archived’ [active = False] before copying it
  4. Save the original name in the temporary field old_name
  5. Add “(archived)” to the name before making a copy
  6. Copy the record
    • Note that this will only copy the data in some fields (which was why we set the image field to “copied” above).
  7. Set the name back to what is was before we added “(archived)”
  8. Remove the image: rec['x_studio_image'] = False
  9. Set the record back to ‘active’

Obviously this doesn’t make sense as it stands. It would be better to add some logic so that the image is only removed in specific circumstances.

Results:

Drag the Lead to another stage:

The image has been removed

An archived version has been created. As it is a CRM Lead it shows as “Lost”

This is the archived record with the image.

Leave a comment