Managing a function called from both OnValidate and OnRename

Mike_HWGMike_HWG Member Posts: 104
I am using table 5717 item Cross Reference with the following code:
----------------------------------
OnRename()
----------------------------------
message('From rename');
MyFunction;

-----------------------------------
Cross-Reference No. - OnValidate()
-----------------------------------
message('From validate');
MyFunction;

------------------------------------
MyFunction()
------------------------------------
message('called myfunction');

If I go to an existing record in the table, edit the Cross-Reference No. field, then press the down arrow, the system returns the following:
Do you want to rename the record?
from validate
called myfunction
from rename
called myfunction

In a situation like this, how do I minimize calling MyFunction correctly? This is my hack so far:

Form 5721 Item Cross Reference Entries
-----------------------------------
Form - OnModifyRecord() : Boolean
-----------------------------------
IF "Cross-Reference No." <> xRec."Cross-Reference No." THEN
     AlreadyCalled := TRUE;

Table 5715 Item Cross Reference
----------------------------------
OnRename()
----------------------------------
message('From rename');
IF NOT AlreadyCalled THEN
    MyFunction;
AlreadyCalled := FALSE;

-----------------------------------
Cross-Reference No. - OnValidate()
-----------------------------------
message('From validate');
MyFunction;

------------------------------------
MyFunction()
------------------------------------
message('called myfunction');


Is this the correct way of doing things?
Michael Hollinger
Systems Analyst
NAV 2009 R2 (6.00.34463)

Comments

  • Yashojit_PandhareYashojit_Pandhare Member Posts: 38
    Hi,

    When you are Creating Function MyFunction Create it with parameter

    MyFunction(CalledFrom)
    if CalledFrom='FromValidate' then
    //Dovalidate related actions
    if CalledFrom='FromRename' then
    // doRename related action

    while calling this function call with respective parameters

    hope this helps.
  • udayrmerudayrmer Member Posts: 171
    Hi,

    If your primary Key has only field "Cross Reference No." Then Remove call of myFunction From OnRename Record or Onvlaidate.
    Because whenever you call Onvalidate automatically OnRename will be called
    Uday Mer | MS Dynamics NAV Techno-Functional Consultant
  • Mike_HWGMike_HWG Member Posts: 104
    Yashojit Pandhare, udayrmer, thanks for the responses. However, I have some complications to add:


    1. Let's say I want the logic in MyFunction to run only during OnValidate calls. If I perform "Item Cross Reference".Rename() from code, I don't get an OnValidate call!

    2. Let's say i want the logic in MyFunction to run only during OnRename calls. If I perform "VALIDATE("Cross Reference No.",'123456') from code, I don't get an OnRename call!

    I think I need to do something like this:
    ----------------------------------
    OnRename()
    ----------------------------------
    MyFunction(FALSE);
    
    ----------------------------------
    Cross-Reference No. - OnValidate()
    ----------------------------------
    MyFunction(TRUE);
    
    ----------------------------------
    MyFunction(CalledFromValidate : Boolean)
    ----------------------------------
    IF CalledFromValidate OR AllowFunction THEN BEGIN
         AllowFunction := FALSE;  //Reset the variable
         // Perform Logic
    END;
    

    With this setup, I just need to set AllowFunction to TRUE prior to explicitly calling a Rename.
    Michael Hollinger
    Systems Analyst
    NAV 2009 R2 (6.00.34463)
  • udayrmerudayrmer Member Posts: 171
    Can you tell me in myFunction code get executed is same for both called from OnRename & OnValidate ?
    Uday Mer | MS Dynamics NAV Techno-Functional Consultant
Sign In or Register to comment.