Hi Team,
I would like to check with you regarding "OnBeforeModifyEvent - xRec". I can see the xRec.xxx (previous value) if we change the value from table or page. But xRec.xxx value is always same as current record modified value if we modifed by code. Does is standard behavior or bug?
NAV - 2016 CU14 (AU)
Regards,
Yukon
*This post is locked for comments
Right! Today it was my turn to spend a good hour, reading 14 websites, and then running a few code tests on this topic.
Wondering whether we can clean up such threads with well-meant but non-working suggestions, untested code or what ever. In the end it is super simple. Microsoft confirms that it has been like this for many versions. And it still works exactly the same today in AL. So, if you want to use this in an OnBeforeModify event, be sure to call xRec.FIND('=') to obtain the current/previous/old (whatever way you look at it) value as stored in the database.
Hi Yukon,
The post is not recent but I'd just like to mention that it's been like this for a long time: xRec is different from Rec inside the OnModify trigger and the relevant event triggers unless the record is modified by code.
Regards,
Alex
And now try this:
OBJECT Codeunit 50099 TEST
{
OBJECT-PROPERTIES
{
Date=16.06.17;
Time=09:46:38;
Modified=Yes;
Version List=;
}
PROPERTIES
{
OnRun=VAR
Item@1000000000 : Record 27;
BEGIN
Item.FINDFIRST;
Item.VALIDATE(Description, 'foobar');
Item.MODIFY(TRUE);
END;
}
CODE
{
[EventSubscriber(Table,27,OnAfterModifyEvent)]
LOCAL PROCEDURE OnAfterModifyEvent@1000000000(VAR Rec@1000000000 : Record 27;VAR xRec@1000000001 : Record 27;RunTrigger@1000000002 : Boolean);
BEGIN
MESSAGE(xRec.Description);
MESSAGE(Rec.Description)
END;
BEGIN
END.
}
}
Hi guys, I have just checked it in NAV 2017 and works fine, so xRec is different from Rec. For both OnBeforeModifyEvent and OnAfterModifyEvent.
I have tested with the following code:
OBJECT Codeunit 88889 Update Item
{
OBJECT-PROPERTIES
{
Date=16-06-17;
Time=09:16:42;
Version List=OnBeforeModifyEvent - xRec;
}
PROPERTIES
{
OnRun=BEGIN
UpdateDescriptionAssigment;
UpdateDescriptionValidate;
END;
}
CODE
{
VAR
Item@1000 : Record 27;
LOCAL PROCEDURE UpdateDescriptionAssigment@1();
BEGIN
WITH Item DO BEGIN
GET('1000');
Description := 'Bicycle UpdateDescriptionAssigment';
VALIDATE(Description);
END
END;
LOCAL PROCEDURE UpdateDescriptionValidate@2();
BEGIN
WITH Item DO BEGIN
GET('1000');
VALIDATE(Description,'Bicycle UpdateDescriptionValidate');
END
END;
BEGIN
END.
}
}
OBJECT Codeunit 88888 Item - Events
{
OBJECT-PROPERTIES
{
Date=16-06-17;
Time=08:42:12;
Version List=OnBeforeModifyEvent - xRec;
}
PROPERTIES
{
OnRun=BEGIN
END;
}
CODE
{
[EventSubscriber(Table,27,OnBeforeValidateEvent,Description)]
LOCAL PROCEDURE BeforeOnValidateDescription@1(VAR Rec@1000 : Record 27;VAR xRec@1001 : Record 27;CurrFieldNo@1002 : Integer);
BEGIN
MESSAGE('BeforeOnValidateDescription')
END;
[EventSubscriber(Table,27,OnAfterValidateEvent,Description)]
LOCAL PROCEDURE AfterOnValidateDescription@2(VAR Rec@1000 : Record 27;VAR xRec@1001 : Record 27;CurrFieldNo@1002 : Integer);
BEGIN
MESSAGE('AfterOnValidateDescription')
END;
BEGIN
END.
}
}
No, Nikolay, that's not strange as the record in the table has not been modified yet, so a GET on that record will give you the old (=current) version.
It looks a little bit strange, but if you write in event xRec.GET before your code everything be as you expecting
Thanks, Luc. Please report it to MS. Nowadays, we are star using Event.
Regards,
Yukon
Thanx, Dennis.
Yukon, if you want I can report it to MS as MVP.
It's the same on 2017.
If I remember right, it's only if you modify by code.
Yes, I think same as you but haven't test on other CU and 2017. Let see other member reply.
Regards,
Yukon
Sohail Ahmed
2
mmv
2
Amol Salvi
2