function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Steve ConnellySteve Connelly 

How do I delete records with a trigger?

I have an apex trigger the creates custom object records from an opportunity when the opp is ceated or when the close date is updated.

So far so good.

I have a new wrinkle and I am not sure how to apprach it.

When the opp close date is updated, I need the trigger to delete any of the custom object records it created in the earlier pass and then create new ones as normal.

Any suggestions on how to come at this?

Thanks much,
Steve
Best Answer chosen by Steve Connelly
ShirishaShirisha (Salesforce Developers) 
Hi Steve,

Greetings!

You can use the below sample code to delete the records as per your business logic.

https://developer.salesforce.com/forums/?id=906F000000090XGIAY

https://developer.salesforce.com/forums/?id=906F000000090XGIAY

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Steve,

Greetings!

You can use the below sample code to delete the records as per your business logic.

https://developer.salesforce.com/forums/?id=906F000000090XGIAY

https://developer.salesforce.com/forums/?id=906F000000090XGIAY

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
This was selected as the best answer
Steve ConnellySteve Connelly
Can this be adjusted not to delet Opps themselves but custom records related to the Opp that fires the trigger? There will typically be more than one. 

Steve