Forms with Line No. and Filter

Some forms in Dynamics NAV use the property “AutoSplitKey”.  That means that the “Line No.” field will be automatically assigned the next line number or a number between lines if the user tries to insert a line between two lines.

If the user adds a filter to the form then this will fail.  I created a code that will solve this problem.  First you need to reset the “AutoSplitKey” property.  Then you create two functions.

NextLineNo() : Integer
CommentLine.RESET;
CommentLine.SETRANGE("Table Name","Table Name");
CommentLine.SETRANGE("No.","No.");
IF CommentLine.FINDLAST THEN
EXIT(CommentLine."Line No." + 10000)
ELSE
EXIT(10000);
BeforeLineNo(BeforeCommentLine : Record "Comment Line") : Integer
CommentLine.RESET;
CommentLine.SETRANGE("Table Name","Table Name");
CommentLine.SETRANGE("No.","No.");
CommentLine := BeforeCommentLine;
IF CommentLine.FIND('<') THEN
EXIT(CommentLine."Line No." +
ROUND((BeforeCommentLine."Line No." -
CommentLine."Line No.") / 2,1))
ELSE
EXIT(ROUND(BeforeCommentLine."Line No." / 2,1));

 

And then insert this code on the “Form – OnNewRecord(BelowxRec : Boolean)” trigger.

SetUpNewLine;

IF BelowxRec THEN
"Line No." := NextLineNo
ELSE
"Line No." := BeforeLineNo(xRec);

This will create automatically assign a “Line No.” to your line even if you are using filter.