Cath Acumatica error message
Hi! Today I want to show you how to override the Acumatica error message:
“The key can’t be updated at this time. Try to save your previous changes first.
This issue typically occurs when your DAC (Data Access Class) uses custom fields as keys instead of PXDBIdentity, and you attempt to update or insert a record with keys that already exist in the cache.
The error appears before the RowUpdated or RowPersisted events are triggered, which means it's difficult to handle gracefully in those events. So what can you do if you want to show a more user-friendly message?
I'm going to show you two ways to handle this:
- Use the FieldUpdating event.
- Customize the message for the entire Acumatica instance using Translation Dictionaries.
- Use the FieldUpdating event.
private void CheckForDuplicate(
ASPRProjection newrow,
object newValue,
Func<ASPRProjection, bool> matchPredicate)
{
if (newrow == null || IsImport || newValue == null)
return;
foreach (ASPRProjection cached in Caches[typeof(ASPRProjection)].Cached)
{
if (cached == null || newrow.NoteID == null || newrow.NoteID == cached.NoteID)
continue;
if (matchPredicate(cached))
{
throw new PXSetPropertyException(Helpers.Messages.DuplicateProjectionError, PXErrorLevel.RowError);
}
}
}
protected virtual void _(Events.FieldUpdating<ASPRProjection.periodID> e)
{
var newrow = (ASPRProjection)e.Row;
CheckForDuplicate(newrow, e.NewValue, cached =>
cached.CustomerGrouping == newrow.CustomerGrouping &&
cached.InventoryID == newrow.InventoryID &&
cached.SiteID == newrow.SiteID &&
cached.PeriodID?.ToString() == e.NewValue?.ToString());
}
As you can see here, I check the NoteID in the cache along with all four of my custom fields to detect a duplicate.
You should add this event for all you keys fields.
The result:
2. Customize the message for the entire Acumatica instance using Translation Dictionaries.
You could go the Translation Dictionaries screen and select the Language (English on my test) and click on Collect Strings button, this will take some time to collect the strings.
-Then locate the Value that you would like to translate and set its new value/Translation on the English column, and save changes.
Then refresh the screens and the new value will be display. Please note this will be the message used in all Acumatica screens,
Feel free to leave a comment if you have questions or want to share your own approach.
Happy coding, and have a great time developing with Acumatica!