Options

How to modify record on OnAfterGetRecord

AntidotEAntidotE Member Posts: 61
Hello

Imagine, user needs reminder about not matching field value when he opens form/page with possibly incorrect data.
He is asked if he would like to change value to correct one. But he still has possibility to refuse suggestion because he does not need to change value.

How it is supposed to work: on the form/page in the trigger OnAfterGetRecord() or OnAfterGetCurrRecord() we call checking procedure which should change value to correct in case user agrees with suggestion.

What happens in real life - user does see changed value on the form/page but real data is not changed.

My question is the following: Did I miss something?

To illustrate test case I have created 2 tables and one form (NAV 2009, but same principle does not work even in 2013R2)
Field No.	Field Name	Data Type	Length
1	keyfield	Code	10	<-- PK
2	description	Text	50
3	check code	Code	10	<-- this field is compared to
Field No.	Field Name	Data Type	Length
1	line no.	Integer	<-- PK
2	master code	Code	10	<-- table relation to First Table
3	description	Text	50
4	check code	Code	10	<-- this field is compared
ldk38Hs.png
IF master.GET("master code") THEN
  IF "check code" <> first."check code" THEN
    IF CONFIRM('First has check code %1\slave has check code %2\do you want to change slave''s code to master''s?',
                FALSE,master."check code","check code") THEN
      "check code" := master."check code";

I filled Master table with the following data:
keyfield	description	check code
C10	smth1	01
C20	smth2	01
u9iUoqJ.png

Also I filled Slave table with the following data:
line no.	master code	description	check code
10000	C10	here nothing happens	01
20000	C20	here something wrong	02
QRLsnTX.png

Now, when I run form which shows records on Slave table, I see the following:
Ub8Tz9V.png
Nothing happens, because check code on master and slave are identical

Now I press PgDn and second record comes, which have non-identical check code on master and slave tables:
cUxZEeO.png

When I agree to change and press Yes, I see the following:
u6TSK61.png
See? looks like check code now equals master's...

Here comes BUT: when I switch to another record and back / close form and open again / whatsoever I get confirmation dialog again! Why? Did not I change value already?
SczjyCo.png


When I add in the code any kind of MODIFY or CurrForm.Update I get an error:
wARSPY9.png

What I missed?
It is hard to swim against self bloodstream... (c) Old, experienced kamikadze.

Comments

  • Options
    RydenRyden Member Posts: 24
    AntidotE wrote:
    When I add in the code any kind of MODIFY or CurrForm.Update I get an error:
    wARSPY9.png

    What I missed?

    You won't see any permanent changes unless you apply MODIFY, and that can't be done at this stage.
    The old error message used to say something like "You cannot make any changes from this trigger" which was a lot more helpful.

    Move your code to some later stage in the chain of events and it will work
    Good luck!
    //Anders
    --
    www.nabsolutions.se
  • Options
    Rishi1109Rishi1109 Member Posts: 43
    Why are you not writing your code on OnInsert trigger of Slave Table ?
    Thanks and Regards
    Rishi
  • Options
    AntidotEAntidotE Member Posts: 61
    Ryden wrote:
    You won't see any permanent changes unless you apply MODIFY, and that can't be done at this stage.
    The old error message used to say something like "You cannot make any changes from this trigger" which was a lot more helpful.

    Move your code to some later stage in the chain of events and it will work
    Good luck!
    //Anders
    Rishi1109 wrote:
    Why are you not writing your code on OnInsert trigger of Slave Table ?

    Main task is to notify user about mismatching values in check code when he opens form/page with existing record or sees another record.

    Actual code has assigning matching check code on slave table, but it can be different from master's one. This happens rarely and is not usual practice, that's why user is asked if he wants to change to "normal" value.

    I am afraid this task cannot be solved this manner and has to be changed to just show user message "mismatching values" and he will press some button to change values actually.

    Thanks for answers.
    It is hard to swim against self bloodstream... (c) Old, experienced kamikadze.
  • Options
    vaprogvaprog Member Posts: 1,118
    Did you try setting input focus to the modified field/control? (just a thought).
  • Options
    AntidotEAntidotE Member Posts: 61
    vaprog wrote:
    Did you try setting input focus to the modified field/control? (just a thought).
    No possibility
    It is hard to swim against self bloodstream... (c) Old, experienced kamikadze.
Sign In or Register to comment.