I'm working on a job queue task where I need to ensure that when I rename a Purchase Header record, the corresponding Purchase Line records associated with it should also update accordingly. Could you please review and assist in refining this code?
{
DataItemLink = /Document No./ = field(/No./);
trigger OnPreDataItem()
var
Rec_PurchaseHeader: Record /Purchase Header/;
Rec_PurchaseHeader1: Record /Purchase Header/;
Rec_PurchaseLine1: Record /Purchase Line/;
Rec_PurchaseLine: Record /Purchase Line/;
begin
Rec_PurchaseHeader1.SetRange(/Document Type/, Rec_PurchaseHeader./Document Type/::Order);
Rec_PurchaseHeader1.SetRange(/No./, '106005');
if Rec_PurchaseHeader1./Document Type/ = Rec_PurchaseHeader1./Document Type/::Order then begin
Rec_PurchaseHeader1.Rename(Rec_PurchaseHeader./Document Type/::Order, '106022');
Rec_PurchaseLine1.SetFilter(/Document No./, '%1', '106001');
if Rec_PurchaseLine1.Findset then
Rec_PurchaseLine1.Rename(Rec_PurchaseLine./Document Type/::Order, 'PORD-1000', Rec_PurchaseLine./Line No./);
end;
if Rec_PurchaseHeader1./Document Type/ = Rec_PurchaseHeader1./Document Type/::Invoice then begin
Rec_PurchaseHeader1.Rename(Rec_PurchaseHeader./Document Type/::Invoice, '106021');
end;
end
else
ERROR('Purchase Header not found.');
end;
}
}