Skip to main content

Notifications

Announcements

No record found.

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

ChangeCompany and RENAME record

Posted on by
The Base App contains a code snippet within the Item Table Rename function that performs updates across related tables when an Item No. is changed. For instance, it ensures that Sales Lines associated with the renamed Item are appropriately updated.
 
// Base App - Item Table
trigger OnRename()
    var
        SalesLine: Record /Sales Line/;        
    begin
        SalesLine.RenameNo(SalesLine.Type::Item, xRec./No./, /No./);
        // Additional code follows, but kept concise for clarity.
    end;

// The following customization is causing an issue:

trigger OnAfterRename()
    begin
        IntercompanyRename();        
    end;
    
procedure IntercompanyRename()
    var
        inventorySetup: Record /Inventory Setup/;
        item: Record Item;
    begin
        InventorySetup.Get();
        if inventorySetup./Source Company/ <> '' then begin
            Item.ChangeCompany(inventorySetup./Source Company/);
            if Item.GET(xRec./No./) then begin
                item.Rename(Rec./No./); // THE ISSUE OCCURS HERE
            end;
        end;
    end;
 
When working in Company /A/ and renaming an Item record (modifying the Item No.), the Base App's /OnRename/ code from Company /B/ seems to execute in Company /A/. This results in a situation where a record is renamed in Company /B/, but the related tables are not updated as expected. This behavior could potentially lead to inconsistencies in the database, such as Sales Lines referencing a non-existing Item No. I've observed this issue in Business Central version 17.17.
 
It is important to note that the /Source Company/ field will always be empty in Company /B,/ which should prevent unintended loops due this customization. We have double checked this.
 
So basically I want to confirm if: when I rename a record in one company, I try to rename a record in another company, will the OnRename trigger for the second company run in the first company?
 
 
Categories:
  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,748 Super User 2024 Season 1 on at
    ChangeCompany and RENAME record
    No, the trigger will only run in the company that is in the scope of your record variable.
    But I think it is good practise to complete processing in one company before you move on to the next company.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans