Modify record from the OnAfterGetRecord trigger

SolmyrSolmyr Member Posts: 51
Hallo All!
exuse for my English, it's not so good but I try to explain the problem in the understandable way

I added new boolean field on the table 37 sales line. This field shows is the cost adjusted for line item or not. It's not the standard NAV cost adjustments, it is additional functionality which determines the value of this field through few different tables (Item, Sales Shipment Line, Item Ledger Entry, Item Application Entry). It is the reason why I can't make this field as a simple FlowField. Purpose of this field is possibility to filter sales order (subform) and see which lines are adjusted and which aren't. The problem is that the new field should be updated each time I run the sales order.
If I call my function (which updates this field for current line) from sales order subform - OnAfterGetRecord trigger and function does not modify sales line - the value of new field is showed correctly on the form but still equal to old value in the table. So the filtering is not possible because table is not modified with new values.
If the function tries to modify sales line the standard trigger error appears ("you cannot make any changes to the database in this trigger").
Now I don't see any solution...
The only way is add the new button to call this function manually and update the subform. But it's inconveniently for my needs.

Navision version: 3.7 NA

Thanks In advance :)
Oleg Dovgalenko

Comments

  • SolmyrSolmyr Member Posts: 51
    Maybe I named the topic in a wrong way. Actually I know that we can't modify a record from the OnAfterGetRecord. But probably there are some solutions to my problem to avoid modification from that trigger...
    Oleg Dovgalenko
  • SavatageSavatage Member Posts: 7,142
    the value of new field is showed correctly on the form but still equal to old value in the table.

    Maybe it's me but I don't know what this means.

    In your function are you specifing the Doc Type Doc No & Line No so it knows which one to update?

    What's the code?
  • SolmyrSolmyr Member Posts: 51
    I'll try to explain
    the function is called from sales order subform, OnAfterGetRecord trigger
    ShowShortcutDimCode(ShortcutDimCode);
    SetCostAdjustment; // this one
    

    Function SetCostAdjustment
    IF (Type = Type::Item) AND ("No." <> '') THEN BEGIN
      Item.RESET;
      IF Item.GET("No.") THEN BEGIN
        "Cost Adjusted" := TRUE;
        CASE "Document Type" OF
         "Document Type"::Order:
           BEGIN
             SalesShptLine.SETCURRENTKEY("Order No.","Order Line No.");
             SalesShptLine.SETRANGE("Order No.","Document No.");
             SalesShptLine.SETRANGE("Order Line No.","Line No.");
           END;
         "Document Type"::Invoice:
           BEGIN
             SalesShptLine.SETRANGE("Document No.","Shipment No.");
             SalesShptLine.SETRANGE("Line No.","Shipment Line No.");
           END;
        END;
        SalesShptLine.SETFILTER("Qty. Shipped Not Invoiced",'<>0');
        IF SalesShptLine.FIND('-') THEN
          IF (SalesShptLine.Type = SalesShptLine.Type::Item) AND
            (SalesShptLine."Item Shpt. Entry No." <> 0) THEN BEGIN
            ItemLedgEntry.GET(SalesShptLine."Item Shpt. Entry No.");
            ItemAppEntry.SETCURRENTKEY("Item Ledger Entry No.", "Outbound Item Entry No.");
            ItemAppEntry.SETRANGE("Item Ledger Entry No.",ItemLedgEntry."Entry No.");
            ItemAppEntry.SETRANGE("Outbound Item Entry No.",ItemLedgEntry."Entry No.");
            IF ItemAppEntry.FIND('-') THEN BEGIN
              REPEAT
                ItemLedgEntry2.RESET;
                ItemLedgEntry2.GET(ItemAppEntry."Inbound Item Entry No.");
                  IF NOT ItemLedgEntry2."Completely Invoiced" THEN
                    "Cost Adjusted" := FALSE;
              UNTIL ItemAppEntry.NEXT = 0;
            END;
          END;
      END;
    END;
    
    it works with the rec of subform so I don't need to specify Doc Type, Doc No & Line No

    as you can see there is no MODIFY expression
    after opening sales order I have the set of records on the subform where Cost Adjusted is calculated right.
    but, if I find these records in the table 37 the Cost Adjusted field will be set to No. in all of them
    and I can't filter sales lines by this field because it isn't filled in table
    if I place MODIFY into my function the trigger error ("you cannot make any changes to the database in this trigger") appears
    Oleg Dovgalenko
  • matteo_montanarimatteo_montanari Member Posts: 189
    Solmyr wrote:
    I'll try to explain
    the function is called from sales order subform, OnAfterGetRecord trigger
    ShowShortcutDimCode(ShortcutDimCode);
    SetCostAdjustment; // this one
    

    Function SetCostAdjustment
    IF (Type = Type::Item) AND ("No." <> '') THEN BEGIN
      Item.RESET;
      IF Item.GET("No.") THEN BEGIN
        "Cost Adjusted" := TRUE;
        CASE "Document Type" OF
         "Document Type"::Order:
           BEGIN
             SalesShptLine.SETCURRENTKEY("Order No.","Order Line No.");
             SalesShptLine.SETRANGE("Order No.","Document No.");
             SalesShptLine.SETRANGE("Order Line No.","Line No.");
           END;
         "Document Type"::Invoice:
           BEGIN
             SalesShptLine.SETRANGE("Document No.","Shipment No.");
             SalesShptLine.SETRANGE("Line No.","Shipment Line No.");
           END;
        END;
        SalesShptLine.SETFILTER("Qty. Shipped Not Invoiced",'<>0');
        IF SalesShptLine.FIND('-') THEN
          IF (SalesShptLine.Type = SalesShptLine.Type::Item) AND
            (SalesShptLine."Item Shpt. Entry No." <> 0) THEN BEGIN
            ItemLedgEntry.GET(SalesShptLine."Item Shpt. Entry No.");
            ItemAppEntry.SETCURRENTKEY("Item Ledger Entry No.", "Outbound Item Entry No.");
            ItemAppEntry.SETRANGE("Item Ledger Entry No.",ItemLedgEntry."Entry No.");
            ItemAppEntry.SETRANGE("Outbound Item Entry No.",ItemLedgEntry."Entry No.");
            IF ItemAppEntry.FIND('-') THEN BEGIN
              REPEAT
                ItemLedgEntry2.RESET;
                ItemLedgEntry2.GET(ItemAppEntry."Inbound Item Entry No.");
                  IF NOT ItemLedgEntry2."Completely Invoiced" THEN
                    "Cost Adjusted" := FALSE;
              UNTIL ItemAppEntry.NEXT = 0;
            END;
          END;
      END;
    END;
    
    it works with the rec of subform so I don't need to specify Doc Type, Doc No & Line No

    as you can see there is no MODIFY expression
    after opening sales order I have the set of records on the subform where Cost Adjusted is calculated right.
    but, if I find these records in the table 37 the Cost Adjusted field will be set to No. in all of them
    and I can't filter sales lines by this field because it isn't filled in table
    if I place MODIFY into my function the trigger error ("you cannot make any changes to the database in this trigger") appears

    Hi

    Add a menu option "Update Adjusted Cost" on "Function" button.
    I think it isn't another solution.

    Matteo
    Reno Sistemi Navision Developer
  • SolmyrSolmyr Member Posts: 51
    Eto wrote:
    Hi

    Add a menu option "Update Adjusted Cost" on "Function" button.
    I think it isn't another solution.

    Matteo
    Hi, thanks!
    but as I wrote in my first message adding a button/menu button is inconveniently. It's possible and easy way but it sholud be updated automatically...
    Oleg Dovgalenko
  • SavatageSavatage Member Posts: 7,142
    Well I guess that why there's a function button :mrgreen:
    the new field should be updated each time I run the sales order.
    Well you're not really running a sales order. Just getting a field is not going to let to change it in this way.
  • lubostlubost Member Posts: 623
    Hi,
    solution I often use is:

    in OnAfterGetRecord put statement
    SAVERECORD;

    and calling your code put in ONMODIFY trigger.
  • SolmyrSolmyr Member Posts: 51
    lubost wrote:
    Hi,
    solution I often use is:

    in OnAfterGetRecord put statement
    SAVERECORD;

    and calling your code put in ONMODIFY trigger.

    Hi, I tried this solution. When I opened sales order the confirmation message appeared:
    Do you want to rename the rocord?
    If I pressed Yes - I saw the error message:
    You cannot rename a Sales Header.
    Oleg Dovgalenko
Sign In or Register to comment.