Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
1 Răspunde
1134 Vizualizări

I am trying to make an automation using studio's automations, I am doing it with the execute code action, what I want it to do is see if a many2one field exists, if it does delete that record. How do I do it only in studio (v18)? See this code snippet:

# Check if the record has a partner_id if record.partner_id: client = record.partner_id # Attempt to find a lead linked to the client # Need to verify if the lead exists and delete it # Unsure how to proceed with checking and deletion in Studio

Imagine profil
Abandonează
Cel mai bun răspuns

You can get the opportunities from the field client.opportunity_ids. If you want to get all leads, you need to search for them:

# Check if the record has a partner_id
if record.partner_id:
client = record.partner_id
# Attempt to find a lead linked to the client
lead_ids = record.env['crm.lead'].search([('partner_id', '=', client.id)])
# Check if any leads found
if lead_ids:
lead_ids.unlink()

Note that the check if lead ids were found is not necessary and you can directly call unlink on the recordset:

record.env['crm.lead'].search([('partner_id', '=', client.id)]).unlink()

If you do not want to delete the leads, I suggest to archive them:

if lead_ids:
lead_ids.write({'active': False})
Imagine profil
Abandonează
Related Posts Răspunsuri Vizualizări Activitate
1
ian. 25
1115
0
feb. 17
3604
1
feb. 25
1277
4
mai 24
2797
0
nov. 23
1600