Dynamics 365 Business Central

Coding, Ideas

Adding dimensions automatically to BC and solve a business requirement.

Ok, let´s talk about something that companies do as part of their business:

Rental

Sometimes, the companies rent their equipment to their customers and receive income for those operations.

As you know, Business Central doesn’t have a rental functionality and searching in the appsource only bring us extensions for US, Canada, some European countries, and nobody cares about Latam market, a little digression here, Microsoft, remember windows phone? And the lack of support? The lack of apps? About the partners, did you know that EVERY South American version is a W1? Surprise!!!

Well, In the meantime, to give some support to those companies, here goes an idea.

Every time you add a new fixed asset to the system, create a new value in the Fixed Asset dimension (create one). When you make a sales invoice, use the G/L Account for “Rental services” and in the lines select the Fixed Asset dimension value so you can put many lines with different dimension values but the same G/L Account.

Create a new Account Schedule (or Analysis View) using the account and the Fixed Asset Dimension to get the rental income by FA.

Also, every time you post a purchase invoice (maintenance, supplies, transport, etc.), use the Fixed Asset Dimension Value too, you can have a income statement for each fixed asset.

You just need to set the G/L Account dimension value posting as “Code Mandatory” to avoid an empty posting.

Another problem to solve is create a new dimension value every time we add a new fixed asset, we need to be sure that the value is created, how can we do this without error?

We need to add a new extension to get this.

Create a setup table to set if the extension is active or not and to set the dimension to use.

/// <summary>
/// Table Auto Create Dimension Setup (ID 67400).
/// </summary>
table 67400 "Auto Create Dimension Setup"
{
    Caption = 'Auto Create Dimension Setup';
    DataClassification = ToBeClassified;
    
    fields
    {
        field(1; "Primary Key"; Code[5])
        {
            Caption = 'Primary Key';
            DataClassification = CustomerContent;
        }
        field(10; "Fixed Asset"; Code[20])
        {
            Caption = 'Fixed Asset';
            DataClassification = CustomerContent;
            TableRelation = Dimension.Code;
        }
        field(11; "Auto Create Dimension FA"; Boolean)
        {
            Caption = 'Auto Create Dimension Fixed Assets';
            DataClassification = CustomerContent;
        }
        field(12; Job; Code[20])
        {
            Caption = 'Job';
            DataClassification = CustomerContent;
            TableRelation = Dimension.Code;
        }
        field(13; "Auto Create Dimension Job"; Boolean)
        {
            Caption = 'Auto Create Dimension Job';
            DataClassification = CustomerContent;
        }
    }
    keys
    {
        key(PK; "Primary Key")
        {
            Clustered = true;
        }
    }
    
}

Add a page

/// <summary>
/// Page Auto Create Dimension Setup (ID 67400).
/// </summary>
page 67400 "Auto Create Dimension Setup"
{
    
    Caption = 'Auto Create Dimension Setup';
    PageType = Card;
    SourceTable = "Auto Create Dimension Setup";
    
    layout
    {
        area(content)
        {
            group(General)
            {
                field("Fixed Asset"; Rec."Fixed Asset")
                {
                    ToolTip = 'Specifies the value of the Fixed Asset field.';
                    ApplicationArea = All;
                }
                field("Auto Create Dimension FA"; Rec."Auto Create Dimension FA")
                {
                    ToolTip = 'Specifies the value of the Auto Create Dimension Fixed Assets field.';
                    ApplicationArea = All;
                }
                field(Job; Rec.Job)
                {
                    ToolTip = 'Specifies the value of the Job field.';
                    ApplicationArea = All;
                }
                field("Auto Create Dimension Job"; Rec."Auto Create Dimension Job")
                {
                    ToolTip = 'Specifies the value of the Auto Create Dimension Job field.';
                    ApplicationArea = All;
                }
            }
        }
    }
    
}

Create a codeunit with several event subscriptions to create the dimension and to add the dimension value to the fixed asset.

/// <summary>
/// Codeunit AutoCreateDim Mgmt (ID 67400).
/// </summary>
codeunit 67400 "AutoCreateDim Mgmt"
{
    [EventSubscriber(ObjectType::Table, Database::"Fixed Asset", 'OnAfterInsertEvent', '', true, true)]
    local procedure CreateFADim(var Rec: Record "Fixed Asset")
    var
        CreaDimSetup: Record "Auto Create Dimension Setup";
    begin
        CreaDimSetup.Get();
        if CreaDimSetup."Auto Create Dimension FA" = true then
            CreateDim(CreaDimSetup."Fixed Asset", Rec."No.", 5600);
    end;

    [EventSubscriber(ObjectType::Table, Database::Job, 'OnAfterInsertEvent', '', true, true)]
    local procedure CreateJobDim(var Rec: Record Job)
    var
        CreaDimSetup: Record "Auto Create Dimension Setup";
    begin
        CreaDimSetup.Get();
        if CreaDimSetup."Auto Create Dimension Job" = true then
            CreateDim(CreaDimSetup.Job, Rec."No.", 167);
    end;

    local procedure CreateDim(DimCode: Code[20]; DimValue: Code[20]; TabNo: Integer)
    var
        DimVal: Record "Dimension Value";
        DefDim: Record "Default Dimension";
    begin
        DimVal.Reset();
        DimVal."Dimension code" := DimCode;
        DimVal.Code := DimValue;
        DimVal."Dimension Value Type" := DimVal."Dimension Value Type"::Standard;
        DimVal.Insert(true);

        DefDim.Reset();
        DefDim."Table ID" := TabNo;
        DefDim."No." := DimValue;
        DefDim."Dimension Code" := DimCode;
        DefDim."Dimension Value Code" := DimValue;
        DefDim."Value Posting" := DefDim."Value Posting"::"Same Code";
        DefDim.Insert(true);
    end;

    [EventSubscriber(ObjectType::Table, Database::"Fixed Asset", 'OnAfterValidateEvent', 'Description', true, true)]
    local procedure RenDimName(var Rec: Record "Fixed Asset")
    var
        DimVal: Record "Dimension Value";
        DimSetup: Record "Auto Create Dimension Setup";
    begin
        DimSetup.Get();
        DimVal.Reset();
        DimVal.SetRange("Dimension Code", DimSetup."Fixed Asset");
        DimVal.SetRange(Code, Rec."No.");
        if DimVal.FindSet() then begin
            DimVal.Name := Rec.Description;
            DimVal.Modify(true);
        end;
    end;

    [EventSubscriber(ObjectType::Table, Database::Job, 'OnAfterValidateEvent', 'Description', true, true)]
    local procedure RenDimDescr(var Rec: Record Job)
    var
        DimVal: Record "Dimension Value";
        DimSetup: Record "Auto Create Dimension Setup";
    begin
        DimSetup.Get();
        DimVal.Reset();
        DimVal.SetRange("Dimension Code", DimSetup.Job);
        DimVal.SetRange(Code, Rec."No.");
        if DimVal.FindSet() then begin
            DimVal.Name := Rec.Description;
            DimVal.Modify(true);
        end;
    end;
}

As you can see, I added code for Jobs too, this allows companies to design an Accounting Schedule to get the job information including the payroll and other costs besides those posted in the job itself.

Leave a Reply