As part of the GDPR compliance process, we are updating a few name fields in Account with a random text and then calling the AccountHistory delete function for the tracked field. We are running the Spring '18 release with 'Enable Delete from Field History' and 'Delete from Field History Archive' user permissions enabled on the account executing this code.
However, we are facing a weird situation where the last change made in AccountHistory is not visible even when we are deleting in a separate method after the update was completed and verified.
All the previous AccountHistory entries are deleted without issue. Just the last update made does not even show up until a subsequent and separate call is made.
Interestingly, it does delete if I run the delete operation again in a separate stack.
Has anybody else faced this issue?
Simplified code sample below. You can run this in Workbench if you put it in a class.
public static void SequencerforPubliclyPostedCode()
{
// Data Note: AccountHistory: 6 rows at this point
UpdateMethodforPubliclyPostedCode('******************', 'Redacted');
// Data Note: AccountHistory: 6 Acct History rows at this point. Does not seem to have updated yet.
HistoryDeleteMethodforPubliclyPostedCode('******************');
// Data Note: AccountHistory: 0 Acct History rows at this point.
// Data Note: However on the page there are 3 Acct History rows for the updates made above.
// Data Note: Running HistoryDelete... again in a separate stack, deletes the 3 created in the update above.
}
public static void UpdateMethodforPubliclyPostedCode(Id pAccountId, String pRedactedText)
{
// Retrieve Account
Account acct = [SELECT Id, FirstName, LastName, Preferred_Name__pc, Global_Number__c FROM Account WHERE Id =: pAccountId];
// Anonymize Name
acct.FirstName = pRedactedText;
acct.LastName = pRedactedText;
acct.Preferred_Name__pc = pRedactedText;
// UPDATE Account. (Removed error handling for brevity during posting)
update acct;
}
public static void HistoryDeleteMethodforPubliclyPostedCode(Id pAccountId)
{
List<AccountHistory> lstAcctHistory = new List<AccountHistory>();
// Get Applicable Account History rows
lstAcctHistory.addall([SELECT Id FROM AccountHistory WHERE AccountId =: pAccountId AND Field IN ('FirstName','LastName','Preferred_Name__pc')]);
// DELETE AccountHistory. (Removed error handling for brevity during posting)
delete lstAcctHistory;
}