I'm trying to create an AL Extension that copies records from one BC company to another. In my case, the relevant table is Gen. Journal Line (81), Page: IC General Journal (610, Worksheet), but the question applies to any record in any table. At the moment, I'm getting the following error when the Modify line below is executed: 'The record in table Gen. Journal Line already exists. Identification fields and values: Journal Template Name='IC-JNL',Journal Batch Name='IC',Line No.='10000''.
The record described in the error exists in the source company but not the target. So this doesn't make sense. It's like the ChangeCompany method hasn't taken effect?
Any thoughts?
Here's my code:
local procedure CloneICJRec(var SrcRecordRef: RecordRef)
var
txtBCCompanyName: Text;
SrcRecord: Record "Gen. Journal Line";
TrgRecord: Record "Gen. Journal Line";
begin
// SrcRecordRef is of type: Record "Gen. Journal Line"
txtBCCompanyName := 'Target Company';
// Tie Record object to RecordRef object using RecordID so that we can use TransferFields
SrcRecord.Get(SrcRecordRef.RecordId);
// Setup Target
Clear(TrgRecord);
TrgRecord.Reset();
TrgRecord.Init();
TrgRecord.ChangeCompany(txtBCCompanyName);
// Set key values
// field(1; "Journal Template Name"; Code[10])
// field(2; "Line No."; Integer)
// field(51; "Journal Batch Name"; Code[10])
TrgRecord."Journal Template Name" := SrcRecord."Journal Template Name";
TrgRecord."Line No." := SrcRecord."Line No.";
TrgRecord."Journal Batch Name" := SrcRecord."Journal Batch Name";
TrgRecord.Insert(true); // Save it
Message('Record inserted in ' + TrgRecord.CurrentCompany);
TrgRecord.TransferFields(SrcRecord, false, true);
TrgRecord.Modify(true); // Save it
Message('Record modified in ' + TrgRecord.CurrentCompany);
end;