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...
Answered

SetAutoCalcField is not working fine

(1) ShareShare
ReportReport
Posted on by 85
Pag Hi All,
 
I have issue with SetAutoCalcField, that I wanted to check the condition that if there is Out of Stock or Oversold Inventory Status(which is my custom field) then my flowfields should return True else False. 
 
It's working fine when I'm doing my changes live, but when I publish my code again it always send False, whether there is Out of Stock or Oversold value is coming in Sales Line. 

I have also tried CalcField but still not getting it. 
 
Please see the below code:
 
PageExtension Code
pageextension 50351 SalesOrderPagExt extends "Sales Order"
{
    layout
    {
        addafter("Sell-to Customer No.")
        {
            field("Has Out of Stock Items"; Rec."Has Out of Stock Items")
            {
                Caption = 'Has out of Stock Items';
                ApplicationArea = all;
            }
            field("Has Oversold Items"; Rec."Has Oversold Items")
            {
                Caption = 'Has Oversold Items';
                ApplicationArea = all;
            }
        }
    }
 
    trigger OnAfterGetRecord()
    var
    begin
        Rec.CalcFields("Has Out of Stock Items");
        Rec.calcFields("Has Oversold Items");
    end;
}
 
 
Table Extension Code:
tableextension 50351 SalesHeaderTabExt extends "Sales Header"
{
    fields
    {
        field(50301; "Has Out of Stock Items"; Boolean)
        {
            Caption = 'Has Out of Stock Items';
            FieldClass = FlowField;
            CalcFormula = Exist("Sales Line" WHERE("Document Type" = FIELD("Document Type"),
                                                   "Document No." = FIELD("No."),
                                                   "Inventory Status" = CONST("Out of Stock")));
            Editable = false;
        }
        field(50302; "Has Oversold Items"; Boolean)
        {
            Caption = 'Has Out of Stock Items';
            FieldClass = FlowField;
            CalcFormula = Exist("Sales Line" WHERE("Document Type" = FIELD("Document Type"),
                                                   "Document No." = FIELD("No."),
                                                   "Inventory Status" = CONST("Oversold")));
            Editable = false;
        }
    }
I have the same question (0)
  • Suggested answer
    Gerardo Rentería García Profile Picture
    23,590 Most Valuable Professional on at
  • MA-22070947-0 Profile Picture
    85 on at
    SetAutoCalcField is not working fine
    Hi @Gdrenteria,
     
    I have tried all these but still not able to get calculations when I am publishing my project, OnAfterGetRecord is not calculating the field.
     
    thanks.
  • UmutDoganKoc Profile Picture
    2 on at
    SetAutoCalcField is not working fine
    Hi,

    You don't need to calcfields on OnAfterGetRecord trigger, page should already shows field data without calling calcfields. You can to use calcfields, if flow fields are need to be used in a calculation or another function such as:
     
     
     
    Can you try by removing  OnAfterGetRecord trigger completely and can you let me know field types of "Out of Stock" and "Oversold" in sales line extension?
  • Yi Yong Profile Picture
    2,464 Super User 2025 Season 2 on at
    SetAutoCalcField is not working fine
    Hello,
     
    You don't need to use the CalcField method.
     
    You will need to add 'CurrPage.Update()' at the place where the field (Inventory Status) changes/validated.
    For example, if I were to manually toggle the 'Inventory Status' then I would call the CurrPage.Update on the OnValidate function.
     


     
    If the 'Inventory Status' is retrieved from the Item Table, then you will want to call the CurrPage.Update after validating SalesLine.No.

    To sum it up, the header is referencing from the salesline table.
    When there is a change to the salesline field, you will need to run Currpage.Update to refresh the header (sort of like refreshing the page).
  • MA-22070947-0 Profile Picture
    85 on at
    SetAutoCalcField is not working fine
    Hi Yong,
     
    Thanks for your reply. I just tried it but still not able to retrive values from flowfield when I publish my project. I have also added CurrPage.update() on OnOpenPage trigger of sales order and sales order subform.
     
    thanks.
  • Yi Yong Profile Picture
    2,464 Super User 2025 Season 2 on at
    SetAutoCalcField is not working fine
    Hello,
     
    Can you show the code where the Inventory Status field will be validated as Out of Stock or Oversold?
  • MA-22070947-0 Profile Picture
    85 on at
    SetAutoCalcField is not working fine
    Yes Sure, this is how i am retriving the Inventory Status and Stock Status:
     
    tableextension 50349 Arm_SalesOrderLines extends "Sales Line"
    {
        fields
        {
            field(50300; "Inventory Status"; Option)
            {
                Caption = 'Inventory Status';
                DataClassification = ToBeClassified;
                OptionMembers = " ","Oversold","Out of Stock";
     
            }
            field(50301; "Stock Status"; Option)
            {
                Caption = 'Stock Status';
                DataClassification = ToBeClassified;
                OptionMembers = " ","Discontinued","Limited Availability";
            }
        }
        var
            ItemRecord: Record Item;
            InventoryStatus: Text;
            InventoryOrStockStatusLbl: Text;
            StockStatus: Text;
     
        procedure GetInventoryStatus(): Option " ","Oversold","Out of Stock"
        var
            Inventory: Decimal;
        begin
            Clear(ItemRecord);
            if (Rec."Document Type" = Rec."Document Type"::Order) or (Rec."Document Type" = Rec."Document Type"::Invoice) then begin
                if Rec.Type = Rec.Type::Item then begin
                    ItemRecord.SetRange("No.", Rec."No.");
                    ItemRecord.SetRange("Location Filter", Rec."Location Code");
                    if ItemRecord.FindFirst() then begin
                        ItemRecord.CalcFields(Inventory);
                        Inventory := ItemRecord.Inventory;
                        if Inventory < Rec.Quantity then begin
                            if Inventory <> 0 then begin
                                Rec."Inventory Status" := Rec."Inventory Status"::Oversold;
                                Rec.Modify(true);
                                exit(Rec."Inventory Status"::"Oversold");
                            end;
                        end;
                        if (Inventory = 0) or (Inventory < 0) then begin
                            Rec."Inventory Status" := Rec."Inventory Status"::"Out of Stock";
                            Rec.Modify(true);
                            exit(Rec."Inventory Status"::"Out of Stock");
                        end;
                    end;
                end
                else begin
                    Rec."Inventory Status" := Rec."Inventory Status"::" ";
                    Rec.Modify(true);
                    exit(Rec."Inventory Status"::" ");
                end;
            end;
        end;
     
        procedure GetStockStatus(): Option " ","Discontinued","Limited Availability"
        var
            Inventory: Decimal;
        begin
            Clear(ItemRecord);
            if (Rec."Document Type" = Rec."Document Type"::Order) or (Rec."Document Type" = Rec."Document Type"::Invoice) then begin
                if Rec.Type = Rec.Type::Item then begin
                    if ItemRecord.Get(Rec."No.") then begin
                        if ItemRecord."Stock Status" = ItemRecord."Stock Status"::Discontinued then begin
                            Rec."Stock Status" := Rec."Stock Status"::Discontinued;
                            Rec.Modify(true);
                            exit(Rec."Stock Status"::Discontinued);
                        end
                        else if ItemRecord."Stock Status" = ItemRecord."Stock Status"::"Limited Availability" then begin
                            Rec."Stock Status" := Rec."Stock Status"::"Limited Availability";
                            Rec.Modify(true);
                            exit(Rec."Stock Status"::"Limited Availability");
                        end
                        else begin
                            Rec."Stock Status" := Rec."Stock Status"::" ";
                            Rec.Modify(true);
                            exit(Rec."Stock Status"::" ");
                        end;
                    end;
                end;
            end;
        end;
    }

    and here is a functionality of this code:
    When an item is added to a sales order line or an unposted sales invoice line, the
    system will check the current stock status of the item as indicated on the Item master record. The
    system will set the Stock Status on the sales line to the same value as the Stock Status on the Item
    record

    When an item is added to a sales order line or an unposted sales invoice line, the
    system will check the current inventory levels of the item in the Location specified on the line. If the
    current quantity on hand in that Location is less than the quantity on sales orders in that
    Location, then it will set Inventory Status to Oversold. If the current quantity on hand in that
    Location is zero or negative, then it will set Inventory Status to Out of Stock.
  • Suggested answer
    Yi Yong Profile Picture
    2,464 Super User 2025 Season 2 on at
    SetAutoCalcField is not working fine
    Hello,
     
    When will you call the GetInventoryStatus and GetStockStatus functions?
     
    If it still doesn't work,
    maybe you can keep this simple by adding the 'Inventory Status' field to the subform page and manually change the value to see if the header fields are updated.
     
  • MA-22070947-0 Profile Picture
    85 on at
    SetAutoCalcField is not working fine
    Maybe we cannot enter on Table Extension.

    I can not enter Inventory Status it manually.

    thanks.
  • MA-22070947-0 Profile Picture
    85 on at
    SetAutoCalcField is not working fine
    and i call them on modify of No. in sales order subform page extension.

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
Sumit Singh Profile Picture

Sumit Singh 2,204

#2
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 1,933

#3
YUN ZHU Profile Picture

YUN ZHU 1,885 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans