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 is fairly standard
  2. Clears the temporary field old_name
  3. Sets the record as ‘archived’ [active = False] before we make a copy
  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 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