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

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

Development - Automated testing xRec = Rec in OnAfterModify

(0) ShareShare
ReportReport
Posted on by

We are using the Microsoft Sales Test Library to create sales lines. We have an extension where we need to log certain changes to sales line fields under certain conditions (change log won't work for what we need)

We have a subscription on sales lines that works fine when we use the web client with keyboard and mouse (BC2020 wave 1)

[EventSubscriber(ObjectType::Table, Database::"Sales Line", 'OnAfterModifyEvent', '', true, true)]
local procedure LogEntrySalesLine(var Rec: Record "Sales Line"; var xRec: Record "Sales Line")
var
CU1: Codeunit "My CodeUnit";
begin
if Rec.IsTemporary then exit;
if (Rec.Quantity = 0) and (xRec.Quantity = 0) then exit; // This is always true when run from tests
if Rec.Type = Rec.Type::Item then
CU1.LogSalesLines(Rec, xRec);
end;


We create a sales line using SalesLibrary.CreateSalesLine and then do SalesLine.Validate(Quantity, 10) and SalesLine.Modify(true). In the event subscription, Rec and xRec have the same values for all fields

We then tried doing it without using the Microsoft Test Libraries.

SalesLine.Init();
SalesLine.Validate("Document Type", SalesHeader."Document Type");
SalesLine.Validate("Document No.", SalesHeader."No.");
RecRef.GetTable(SalesLine);
SalesLine.Validate("Line No.", LibraryUtility.GetNewLineNo(RecRef, SalesLine.FieldNo("Line No.")));
SalesLine.Insert(true);
SalesLine.Validate(Type, SalesLine.Type::Item);
SalesLine.Validate("No.", Item."No.");
SalesLine.Validate("Shipment Date", SalesHeader."Shipment Date");
if Qty <> 0 then
SalesLine.Validate(Quantity, Qty);
SalesLine.Modify(true);

Same problem

The only way we can get the test execute the code in the subscription and pass the assertions by using a TestPage:

var
SalesOrderSubform: TestPage "Sales Order Subform";

Begin
SalesOrderSubform.OpenEdit();
SalesOrderSubform.GoToKey(SalesDocTypeEnum::"Order", SalesHeader."No.", SalesLineNo);
SalesOrderSubform.Quantity.SetValue(10);
SalesOrderSubform.OK.Invoke();

I have the same question (0)
  • Suggested answer
    Marco Mels Profile Picture
    on at

    Hello,

    We currently do not have dedicated Dev support via the Dynamics 365 Business Central forums, but I wanted to provide you some additional resources to assist.  If you need assistance with debugging or coding I would recommend discussing this on one of our communities.

    www.yammer.com/dynamicsnavdev

    dynamicsuser.net/.../developers

    I will open this up to the community in case they have something to add.

    Thanks.

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,135

#2
YUN ZHU Profile Picture

YUN ZHU 733 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 612

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans