Hello there, (I'm using NAV 2016 (9.00.47838))
I got a strange problem by using xRec in OnModify:
I want to do something only after OnModify if a boolean field is changed: Ensure that only one record is the Default record.
IF Rec.Default AND NOT xRec.Default THEN BEGIN
The problem is: Sometimes are Rec and xRec equal!
Yes, I know that similar questions are spreaded in the www.
Yes I know that the behaviour is different between internal call and external call and GUI call. And normally GUI call should work!
In my case there is only page interaction: Sometimes working, sometimes not working. > Changing behaviour without changing objects!
A simple table:
Only two fields:
FIELDS
{
{ 1 ; ;Code ;Code10 }
{ 2 ; ;Default ;Boolean }
}
KEYS
{
{ ;Code ;Clustered=Yes }
}
Code only in OnModify:
OnModify=VAR
TestSKM@50000 : Record 50202;
BEGIN
IF Rec.Default AND NOT xRec.Default THEN BEGIN
TestSKM.SETFILTER(Code, '<>%1', Code);
TestSKM.SETRANGE(Default, TRUE);
IF TestSKM.FINDSET(TRUE) THEN
REPEAT
TestSKM.VALIDATE(Default, FALSE);
TestSKM.MODIFY;
UNTIL TestSKM.NEXT = 0;
END;
END;
And a simple list page created by the wizzard.
So I filled the list with the codes '1', '2', ..., '7'
When setting the Default in a record after leaving the record (MODIFY) all other records should be set to Default = FALSE.
I cannot find a rule. Sometimes it works, sometimes it doesn't work.
Sometimes setting Default from top record to bottom record works.
Sometimes Setting Default from bottom record to top record leaves all record Default = TRUE.
Is there any Explanation?
With kind regards
s_keim
Answers
Edit: Table 5050 has an example of this.
thanks for the suggestion, but it will not work.
As I wrote OnModify is only triggered by user interaction.
And when in OnModify trigger Rec and xRec are equal than the wrong xRec is forwarded to the OnModify function.
With kind regards
s_keim
inhouse developer on NAV2016 (9.00.47838) NAVDE
https://docs.microsoft.com/en-us/dynamics-nav/modify-function--record-
to make it clear:
I wrote: "Code only in OnModify"
And I wrote: "In my case there is only page interaction"
My problem is not dysfunction in calling by code. There is dysfunction in user interaction on page.
When I change and leave a line on page, I don't give the command to modify.
NAV calls automatically the OnModify trigger without any user-defined code.
And sometimes Rec and xRec are equal.
To create a user-defined function OnModify helps in another Scenario.
With kindly regards
s_keim
inhouse developer on NAV2016 (9.00.47838) NAVDE
Instead of
Why not ?
Also, regarding your loop
I'd recommend not modifying directly on the same record you're looping through, i.e.
in my scenario I have existing records on my list page exactly as in the database.
I go to a record with Default = FALSE and change it to TRUE.
I leave the record and OnModify is fired automatically.
In OnModify should be:
xRec.Default = FALSE
Default = TRUE
But sometimes it isn't.
sometimes xRec.Default = TRUE.
That's the problem. The code is only sample code to give you a chance to create an example and test it.
We can struggle a bit about how to code inside OnModify.
But the xRec is already wrong.
About your suggestions:
Using I only need to do anything if Rec.Default is TRUE.
Using a second record variable to modify.
In former times we should use it. But nowadays it is not neccessary.
FINDSET(TRUE) is given for the possibility to change the record variable itself.
But these statements only by the way. My topic is the wrong xRec.
With kindly regards
s_keim
inhouse developer on NAV2016 (9.00.47838) NAVDE
So your example would be:
TestSKM.GET(Rec.Code);
IF Rec.Default AND NOT TestSKM.Default THEN BEGIN
TestSKM.SETFILTER(Code, '<>%1', Code);
TestSKM.SETRANGE(Default, TRUE);
...
surely both your suggestions would work. I already thought about it.
And if there is no better solution I would have to do it.
Why I'm hesitating?
My GET would be used in any modification case not only when Default is changed ...
And what bothers me is: When can I trust in xRec?
There are different results in calling by internal code.
But page interaction should work.
With kindly regards
s_keim
inhouse developer on NAV2016 (9.00.47838) NAVDE