OnAfterValidate fail if Modify(true) on purchase line - line does not exist error

samantha73samantha73 Member Posts: 106
edited 2021-11-09 in NAV Three Tier
Hi Guys
I have this code on purchase line Onvalidate on quantity field as a table extension on purchase line table. With Modify statement it doesn't work
modify(Quantity)
        {
            trigger OnAfterValidate()
            var
                PurchHeader: Record "Purchase Header";
            begin
                PurchHeader.Reset();
                PurchHeader.SetRange("Document Type", PurchHeader."Document Type"::Order);
                PurchHeader.SetRange("No.", Rec."Document No.");
                If PurchHeader.Findfirst() then
                    Rec.Validate(MY_AssetID, PurchHeader.MY_AssetID);
                //[b]Modify(true);[/b]//*LINE DOES NOT EXIST ERROR AS LINE NOT INSERTED AT THIS POINT IF NEW LINE
            end;
        }[img]https://us.v-cdn.net/5022383/uploads/editor/wd/99u184yecjmk.png[/img]

Not sure why the line does not exist as quantity is not one of the primary keys? When Modiy statement is commented out it works but trying to understand at what point the line is inserted

Best Answer

  • ShaiHuludShaiHulud Member Posts: 228
    Answer ✓
    samantha73 wrote: »
    Hi Guys
    I have this code on purchase line Onvalidate on quantity field as a table extension on purchase line table. With Modify statement it doesn't work
    modify(Quantity)
            {
                trigger OnAfterValidate()
                var
                    PurchHeader: Record "Purchase Header";
                begin
                    PurchHeader.Reset();
                    PurchHeader.SetRange("Document Type", PurchHeader."Document Type"::Order);
                    PurchHeader.SetRange("No.", Rec."Document No.");
                    If PurchHeader.Findfirst() then
                        Rec.Validate(MY_AssetID, PurchHeader.MY_AssetID);
                    //[b]Modify(true);[/b]//*LINE DOES NOT EXIST ERROR AS LINE NOT INSERTED AT THIS POINT IF NEW LINE
                end;
            }[img]https://us.v-cdn.net/5022383/uploads/editor/wd/99u184yecjmk.png[/img]
    

    Not sure why the line does not exist as quantity is not one of the primary keys? When Modiy statement is commented out it works but trying to understand at what point the line is inserted

    You are getting the error because the record doesn't exist yet. Purchase Order Subform has "DelayedInsert = true" property, meaning the record is not actually inserted until you leave the line.

    Your solutions would be to either check if the line exists first before modifying (if it doesn't exist, then no need to modify), or use OnBeforeValidate event and not modify at all.

Answers

  • txerifftxeriff Member Posts: 500
    Hi,

    I think you are doing it wrong.
    You have to subscribe to an event subscriber, not "modify" quantity field which I think modifies the behaviour, something like:
    [EventSubscriber(ObjectType::Table, database::"Purchase Line", 'OnAfterValidateEvent', 'Quantity', false,false)]
            local procedure MyProcedure()
            begin
                
            end;
    
  • samantha73samantha73 Member Posts: 106
    Both methods can be applied in this kind of scenarios with pros and cons but the question was specifically why wasn't the line inserted when using Quantity field trigger ?
  • ShaiHuludShaiHulud Member Posts: 228
    Answer ✓
    samantha73 wrote: »
    Hi Guys
    I have this code on purchase line Onvalidate on quantity field as a table extension on purchase line table. With Modify statement it doesn't work
    modify(Quantity)
            {
                trigger OnAfterValidate()
                var
                    PurchHeader: Record "Purchase Header";
                begin
                    PurchHeader.Reset();
                    PurchHeader.SetRange("Document Type", PurchHeader."Document Type"::Order);
                    PurchHeader.SetRange("No.", Rec."Document No.");
                    If PurchHeader.Findfirst() then
                        Rec.Validate(MY_AssetID, PurchHeader.MY_AssetID);
                    //[b]Modify(true);[/b]//*LINE DOES NOT EXIST ERROR AS LINE NOT INSERTED AT THIS POINT IF NEW LINE
                end;
            }[img]https://us.v-cdn.net/5022383/uploads/editor/wd/99u184yecjmk.png[/img]
    

    Not sure why the line does not exist as quantity is not one of the primary keys? When Modiy statement is commented out it works but trying to understand at what point the line is inserted

    You are getting the error because the record doesn't exist yet. Purchase Order Subform has "DelayedInsert = true" property, meaning the record is not actually inserted until you leave the line.

    Your solutions would be to either check if the line exists first before modifying (if it doesn't exist, then no need to modify), or use OnBeforeValidate event and not modify at all.
  • samantha73samantha73 Member Posts: 106
    Thanks heaps ..totally forgot the delayed insert ..learn something really useful
Sign In or Register to comment.