Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

Creating salesline with C/AL code

Posted on by 255

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

  • Daniel Rimmelzwaan Profile Picture
    Daniel Rimmelzwaan 3,485 on at
    RE: Creating salesline with C/AL code

    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

  • Suggested answer
    dkatson Profile Picture
    dkatson 2,263 on at
    RE: Creating salesline with C/AL code

    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
    Daniel Rimmelzwaan 3,485 on at
    RE: Creating salesline with C/AL code

    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
    Suresh Kulla Profile Picture
    Suresh Kulla 43,745 on at
    RE: Creating salesline with C/AL code

    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('+')

  • Suggested answer
    Binesh Profile Picture
    Binesh 7,885 on at
    RE: Creating salesline with C/AL code

    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
    manjusree Profile Picture
    manjusree 175 on at
    RE: Creating salesline with C/AL code

    Hi,

    Please try

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

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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans