web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested answer

Copy record from one company to another in AL Extension.

(0) ShareShare
ReportReport
Posted on by 831

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;

I have the same question (0)
  • Nick Webb Profile Picture
    831 on at
    RE: Copy record from one company to another in AL Extension.

    Thanks for your response.

    Yes, I've tried all of the lines below. None of which have helped. I though about close & re-open but that seems a bit extreme?

    CurrPage.Update(true);
    CurrPage.Activate(true);
    CurrPage.Update(false);
    CurrPage.Activate(false);

    Any other ideas?

  • Suggested answer
    Suresh Kulla Profile Picture
    50,193 Super User 2025 Season 2 on at
    RE: Copy record from one company to another in AL Extension.

    Try CurrPage.Update to refresh your page.

  • Nick Webb Profile Picture
    831 on at
    RE: Copy record from one company to another in AL Extension.

    I've made progress on this one. I removed arguments 2 & 3 of the TransferFields method & found then that I didn't have to populate the key fields & it seemed to do the trick.

    Incidentally I should've titled this post 'Move record from one company to another in AL Extension' (NOT Copy). So, I added 'SrcRecord.Delete(true);' as the last line of the procedure. This works fine but the empty page that's left behind at the end of the process shows some yellow warning messages (see below):-

    The page has one or more warnings.
    The field Journal Batch Name of table Gen. Journal Line contains a value (IC) that cannot be found in the related table (Gen. Journal Batch).

    This appears to be some sort of timing issue, because the batch (IC) does exist and If you manually reload the page in the browser the warnings disappear.

    Is there a way of either suppressing these warnings messages or forcing a refresh/reload to make them disappear (or indeed any other solution)?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
Nimsara Jayathilaka. Profile Picture

Nimsara Jayathilaka. 4,304

#2
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 4,045

#3
Sumit Singh Profile Picture

Sumit Singh 2,970

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans