diff --git a/src/System Application/App/Retention Policy/src/Apply Retention Policy/ApplyRetentionPolicyImpl.Codeunit.al b/src/System Application/App/Retention Policy/src/Apply Retention Policy/ApplyRetentionPolicyImpl.Codeunit.al index b191b1baf6..4539ac3961 100644 --- a/src/System Application/App/Retention Policy/src/Apply Retention Policy/ApplyRetentionPolicyImpl.Codeunit.al +++ b/src/System Application/App/Retention Policy/src/Apply Retention Policy/ApplyRetentionPolicyImpl.Codeunit.al @@ -243,13 +243,18 @@ codeunit 3904 "Apply Retention Policy Impl." RetenPolDeleting.DeleteRecords(RecordRef, TempRetenPolDeletingParam); if not TempRetenPolDeletingParam."Skip Event Indirect Perm. Req." then begin - ApplyRetentionPolicyFacade.OnApplyRetentionPolicyIndirectPermissionRequired(RecordRefDuplicate, Handled); + // Indirect-permission path: RecordRef is still open and retains its record marks. + // RecordRef.Duplicate() copies filters and the MarkedOnly flag but NOT the marks, so the + // subscriber must receive the original marked RecordRef - otherwise subset (marked) deletes + // would delete nothing. Count the remaining marked records, then close the original ref. + ApplyRetentionPolicyFacade.OnApplyRetentionPolicyIndirectPermissionRequired(RecordRef, Handled); if not Handled then - RetentionPolicyLog.LogError(LogCategory(), StrSubstNo(IndirectPermissionsRequiredErr, RecordRefDuplicate.Number, RecordRefDuplicate.Caption)); + RetentionPolicyLog.LogError(LogCategory(), StrSubstNo(IndirectPermissionsRequiredErr, RecordRef.Number, RecordRef.Caption)); Handled := false; - end; - - RecordCountAfter := Count(RecordRefDuplicate); + RecordCountAfter := Count(RecordRef); + RecordRef.Close(); + end else + RecordCountAfter := Count(RecordRefDuplicate); NumberOfRecordsDeleted := RecordCountBefore - RecordCountAfter; TotalNumberOfRecordsDeleted += NumberOfRecordsDeleted; diff --git a/src/System Application/App/Retention Policy/src/Apply Retention Policy/RetenPolDeleteImpl.Codeunit.al b/src/System Application/App/Retention Policy/src/Apply Retention Policy/RetenPolDeleteImpl.Codeunit.al index 8681eb43a8..522be7866e 100644 --- a/src/System Application/App/Retention Policy/src/Apply Retention Policy/RetenPolDeleteImpl.Codeunit.al +++ b/src/System Application/App/Retention Policy/src/Apply Retention Policy/RetenPolDeleteImpl.Codeunit.al @@ -41,10 +41,14 @@ codeunit 3916 "Reten. Pol. Delete. Impl." implements "Reten. Pol. Deleting" LimitRecordsToBeDeleted(RecordRef, RecordReferenceIndirectPermission, RetenPolDeletingParam."Skip Event Rec. Limit Exceeded", RetenPolDeletingParam."Max. Number of Rec. To Delete", RetenPolDeletingParam."Total Max. Nr. of Rec. to Del."); end; - if not RetenPolDeletingParam."Indirect Permission Required" then + if not RetenPolDeletingParam."Indirect Permission Required" then begin RecordReferenceIndirectPermission.DeleteAll(RecordRef, true); + // Direct-permission path: close here. For the indirect-permission path the caller needs + // the still-open RecordRef (with its record marks intact) to raise + // OnApplyRetentionPolicyIndirectPermissionRequired, and closes it afterwards. + RecordRef.Close(); + end; RetenPolDeletingParam."Skip Event Indirect Perm. Req." := not RetenPolDeletingParam."Indirect Permission Required"; - RecordRef.Close(); end; local procedure LimitRecordsToBeDeleted(var RecordRef: RecordRef; RecordReferenceIndirectPermission: Interface "Record Reference"; var SkipOnApplyRetentionPolicyRecordLimitExceeded: Boolean; MaxNumberOfRecordsToDelete: Integer; TotalMaxNumberOfRecordsToDelete: Integer)