Delete Tasks when deleting Opportunities

Hi

I have noticed when deleting an Opportunity Suite deletes the link (parent_id) in related Tasks but keeps the Task, I need to delete the task other wise there are lots of tasks that don’t relate to anything.

I have tried updating the tasks deleted field in Opportunities before_delete, after_delete etc logic_hook and mark_deleted in same logic_hooks and nothing works, no errors in code but never marks the taks as deleted.

Does anyone have any ideas/pointers on how I can achieve this.

TIA

I will use a before_delete logic hook to delete anything related. From there you load the beans and delete relationships if you want. Here the documentation:


Thanks,

BrozTechnologies

1 Like

Hi,
You can simply run a query in After Relationship Delete hook between Task and Opportunity. You need to apply this hook into Opportunity Side.
Now in this hook, you can run the queries like this with a sub query

Let Say if the related table is (You need to check your database for this)
opportunity_tasks
and the
Task ID field=task_id
Opportunity ID field=opportunity_id

To Soft Delete (the rows will not appear in the CRM, but they will exist in the database)

“Update tasks set deleted=1 WHERE deleted=0 AND id IN (SELECT task_id FROM opportunity_tasks WHERE deleted=0 AND opportunity_id=’.$deleted_opportunity_id.’)”
And then next query
UPDATE opportunity_tasks SET deleted=1 WHERE deleted=0 AND opportunity_id=’.$deleted_opportunity_id.’"

To Hard Delete (the rows will actually be deleted)

“DELETE FROM tasks WHERE deleted=0 AND id IN (SELECT task_id FROM opportunity_tasks WHERE deleted=0 AND opportunity_id=’.$deleted_opportunity_id.’)”
And then next query
“DELETE FROM opportunity_tasks SET deleted=1 WHERE deleted=0 AND opportunity_id=’.$deleted_opportunity_id.’”

You can get
$deleted_opportunity_id
from within the logic hook if you log the BEAN object.

Thanks a lot.
This all is helpful only if you know the coding, otherwise you have to engaged some devevloper to do it.