Buildup to our problem

Since the release of Dynamics365 V9, whenever you fill in the Contact field (parencontactid) on an Opportunity, Microsoft automatically creates a Stakeholder connection record between the Opportunity and this Contact.
This is all nice and well, but in our project, we already used the Stakeholder connection for whole other purposes.

One support ticket later, we knew this new ‘feature’ could not be disabled through some magic configuration.
So we had to fix this our self.
To avoid this new standard connection, we created a new flow in Microsoft Flow.
Oops, I did it again… I’m sorry, we created a flow in Microsoft Power Automate to delete these kind of records immediately after they are created.
I’m not going into the details of this implementation as this is not the scope of this blog.

The problem… delete the historical data.

Off course, this new behavior wasn’t noticed immediately and the harm was already done.
Many invalid Stakeholder connection records where already created by the system.
So we needed to clean these up.

A quick look at these records learned us that we could easily bulk delete these based on the record object types, the role and the created by user.
Or so we thought…
These records are created by the ‘system’ user, and let that just be a problem because you are not able to find and select this user when composing the advanced find query for your bulk delete job.

Not possible to do a bulk delete on SYSTEM User

 

Lucky for us, we are developers.
And there is nothing that we like more, then to code 🙂

BulkDeleteRequest to the rescue

Somewhere between the many possible request messages you can send to your system is the BulkDeleteRequest message.
This surely will do the trick?
Let’s give it a go

BulkDeleteRequest

And sure enough, when we check the Recurring System Jobs in our system

BulkRecordDeletionJob

Problem solved!