web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

Creating salesline with C/AL code

(0) ShareShare
ReportReport
Posted on by 259

Hey Everyone,

I'm struggling with a issue, and decided to share with u guys. I want to insert a new salesline automatically when the first line is inserted. I managed to insert with below code, but instead they switch place, so the line with below code takes first position, and first line takes second position etc. 

SalesLine2.RESET;
SalesLine2.SETRANGE("Document Type",Rec."Document Type");
SalesLine2.SETRANGE("Document No.",Rec."Document No.");

IF SalesLine2.FIND('+') THEN 
NextLineNo := Rec."Line No." + 10000

ELSE 
NextLineNo := 10000;

SalesLine2.INIT;
SalesLine2.VALIDATE("Document Type", Rec."Document Type");
SalesLine2."Document No." := "Document No.";
SalesLine2."Line No." := NextLineNo;
SalesLine2.Type := SalesLine2.Type::Item;
SalesLine2."No." := Item.exItem;
SalesLine2.Quantity := Quantity;
SalesLine2.Description := Item.Description;
SalesLine2.INSERT;

I hope, i clearly explained my situation. 

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    manjusree Profile Picture
    175 on at

    Hi,

    Please try

    IF SalesLine2.FIND('+') THEN
    NextLineNo := Rec."Line No." + 10000
    ELSE
    NextLineNo := 20000;

    (Assuming that your code is in Sales Line-OnInsert)

  • Suggested answer
    Binesh Profile Picture
    7,885 on at

    Hello lynix_x,


    Instead of 

    IF SalesLine2.FIND('+') THEN 
    NextLineNo := Rec."Line No." + 10000
    
    ELSE 
    NextLineNo := 10000;


    Write bellow code

    IF SalesLine2.FINDLAST THEN 
    NextLineNo := SalesLine2."Line No." + 10000 
    ELSE
    NextLineNo := 10000;
  • Suggested answer
    Suresh Kulla Profile Picture
    50,243 Super User 2025 Season 2 on at

    Where did you add the code ? By the time you have executed the code the line you inserted may not exist and that is the reason it is giving you line No for this line as 10000 instead of 20000 as you would expect.

    You also need to increment SalesLine2."Line No." not Rec."Line No." as below

    IF SalesLine2.FIND('+') THEN

    NextLineNo := SalesLine2."Line No." + 10000

    ELSE

    NextLineNo := 10000;

    Another thing try to use FINDFIRST and FINDLAST functions instead of FIND('-) and FIND('+')

  • Daniel Rimmelzwaan Profile Picture
    3,485 on at

    When you execute this code, did you verify that both lines have the Line numbers that you expect? I would say if they have the right numbers, where the first one has line number 10000 and the one that your code creates has line number 20000, then the sort order of the lines subpage is set to sort descending.

    Question: where is this code triggered? I don't understand what the business requirement is for adding this line. Depending on where the code runs, we might have a clue about what is the cause of your issue.

    (update: basically what Suresh said, I didn't see his reply until after I hit 'Post')

  • Suggested answer
    dkatson Profile Picture
    2,263 on at

    Try to insert this code OnAfterInsert Trigger of Sales line page, or as event to this trigger.

    Also make sure that this code will be executed only after first line was inserted, otherwise it can make a recursion.

  • Daniel Rimmelzwaan Profile Picture
    3,485 on at

    Adding business logic to ANY page triggers is a TERRIBLE idea. You NEVER put any business logic in any page triggers. Business logic belongs in tables and codeunits, NOT on page triggers.

    If you want to subscribe to an OnAfterInsert event, catch the one on the table. Create a codeunit, call it "Sales Line Event Handler" and add a subscriber function that subscribes to the table's OnAfterInsert event

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics NAV (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans