You need to update the code to include the new attachment ID and Modify the ID Update Logic:
You can modify the logic where the ID update occurs, so that it first updates the attachment records before updating the header ID
begin
// First, update the Document Attached table
if DocumentAttachedRec.Get(OldID) then begin
DocumentAttachedRec."Header ID" := NewID; // Update the ID in the Document Attached table
DocumentAttachedRec.Modify();
end;
// Then, update the header ID
HeaderRec."ID" := NewID;
HeaderRec.Modify();