Monday, 7 March 2016

The record you were editing was modified by...

Changes Cannot Be Saved. The record you were editing was modified by ... during your edit session.


Anyone who has spent time writing Triggers in Apex will probably have come across this error before.


If 2 people ever try to edit the same record in Salesforce at the same time, one of you will see an error message stating:
Changes Cannot Be Saved. The record you were editing was modified by ... during your edit session.

Why is it doing this?

Seeing this error message appear when you're trying to finish some work off can be quite annoying but it's actually really useful that Salesforce does this - I promise!

The general concept is that of collision detection. That error message is telling you that someone else is editing the record you're working on at the same time. If Salefsorce didn't have this detection mechanism you could end up losing your work just because another user manages to hit Save first.

How do I avoid it?

There are a few great suggestions on Salesforce's IdeaExchange on how Salesforce could prevent this error from appearing to users. This one idea in particular suggests that Salesforce gives the user the choice on which record to save in a future version.

One really common reason you might suddenly see this error a lot is that you might have installed a Salesforce App that's updating records automatically and locking you out of your work. Always check which user you install your apps with.


 What if I need to live with it?

If the error message isn't being caused by an automated system updating a record whilst users are working on it, then you shouldn't really see this error too often. To clear it, the user just has to reload the current record they are working on.

However, at a recent customer site there was a huge concern that these collisions could be resulting in lost data so I had to think of a process to ensure the data was updated.


 Triggers and Process Builder - A match made in Heaven

If you haven't yet used Process Builder, what are you waiting for? It's a hugely powerful, yet straighforward way to automate processes in your environment. There's also a great overview of Process Builder here on Trailhead.

Good explanation of Process Builder from http://andyinthecloud.com/category/process-builder/


 Our Solution

To ensure that the records got updated even if there was a collision between users saving information to the same record I turned to Time Dependant Actions in Process Builder.

I left a Trigger in place that was copying some much needed information between records, but I also set up a 1 hour delayed Time Dependant Process to catch any records that may have not updated.

The Process Builder runs 1 hour after the record is created. 

Time Delayed Process Builder
If the Trigger has done it's work, the criteria of the Process Builder would not be met so on the vast majority of occasions it has nothing to do.

However, if ever a record has been missed, it's good to know the process is there mopping up records as we go.

Icon from IconFinder, artist JustUI