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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

Creating Purchase orders from Service orders

(0) ShareShare
ReportReport
Posted on by

I have been working on some code to create purchase orders from service orders. But I am getting stuck when trying to do multiple items. If anyone would have some pointers I would appreciate it. I am sure I am not doing something right on finding all records in the service lines.

here is my code on a single.

IF Type = Type::Item THEN BEGIN
  WITH NewPurchLine DO BEGIN
      INIT;
      "Document No." := PurchNo;
      "Document Type" := "Document Type"::Order;
      IF LineNo = 0 THEN
        LineNo := 10000;
        "Line No." := LineNo;
        INSERT(TRUE);
        VALIDATE(Type,Type::Item);
        VALIDATE("No.",Item."No.");
        VALIDATE(Quantity,parServLine.Quantity);
        VALIDATE("Unit Cost",parServLine."Unit Cost");
        VALIDATE("Unit of Measure Code",parServLine."Unit of Measure Code");
        MODIFY;
        LineNo := LineNo +1;
  END;
END;

vs multiple

IF Type = Type::Item THEN BEGIN
 WITH NewPurchLine DO BEGIN
  ServiceLine.RESET;
  ServiceLine.SETRANGE(ServiceLine."No.", "Document No.");
  IF ServiceLine.Findset THEN REPEAT
   INIT;
   "Document No." := PurchNo;
   "Document Type" := "Document Type"::Order;
   IF LineNo = 0 THEN
   LineNo := 10000;
   "Line No." := LineNo;
   INSERT(TRUE);
   VALIDATE(Type,Type::Item);
   VALIDATE("No.",Item."No.");
   VALIDATE(Quantity,parServLine.Quantity);
   VALIDATE("Unit Cost",parServLine."Unit Cost");
   VALIDATE("Unit of Measure Code",parServLine."Unit of Measure Code");
   MODIFY;
   LineNo := LineNo +1;
  UNTIL ServiceLine.NEXT = 0;
 END;
END;

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Nareshwar Raju Vaneshwar Profile Picture
    5,596 on at

    Hi,

    Your multiple line code is looking good and it even works for the single line, it just ends the loops with count 1. No difference.

    Check the line, ServiceLine.SETRANGE(ServiceLine."No.", "Document No."); You must replace with a ServiceLine.SETRANGE(ServiceLine."Document No.", "Document No."); I guess.

    Also, when you are calculating the Line No., you can add a small function rather adding so many variables.

    You can write something like this,

    "Line No." := GetNextPurchLineNo(PurchHeader);

    In GetNextPurchLineNo(PurchHeader);,

    PurchaseLine.FINDSET(PurchaseLine."Document No.", PurchHeader."No.");

    IF PurchaseLine.FIND('+') THEN

     EXIT(PurchaseLine."Line No." + 10000)

    ELSE EXIT(10000);

    GetNextPurchLineNo(PurchHeader)'s return type must be Integer here. By doing that, your code will be more cleaner.

    Hope it helps :)

  • Community Member Profile Picture
    on at

    I am a little closer. I had to move a few things around. but now I get the same count of items in my po, 1 item duplicated instead of creating both. I will use the function as you showed above after I get the rest of it too work.

    IF Type = Type::Item THEN BEGIN

     ServiceLine.RESET;

     ServiceLine.SETRANGE(ServiceLine."Document No.", "Document No.");

     IF ServiceLine.Findset THEN REPEAT

       WITH NewPurchLine DO BEGIN

       INIT;

       "Document No." := PurchNo;

       "Document Type" := "Document Type"::Order;

       IF LineNo = 0 THEN

         LineNo := 10000;

         "Line No." := LineNo;

         INSERT(TRUE);

         VALIDATE(Type,Type::Item);

         VALIDATE("No.",Item."No.");

         VALIDATE(Quantity,parServLine.Quantity);

         VALIDATE("Unit Cost",parServLine."Unit Cost");

         VALIDATE("Unit of Measure Code",parServLine."Unit of Measure Code");

         MODIFY;

         LineNo := LineNo +10000;

       END;

     UNTIL ServiceLine.NEXT = 0;

    END;

  • Suggested answer
    Nareshwar Raju Vaneshwar Profile Picture
    5,596 on at

    Hi,

    By looking at your code, I cannot come to a conclusion, because there are many references on, how you fetch the Purchase Line (Eg. Item No./Quantity). The best method to find out is debugging your code.

    Skip the loop until the code fetches the duplicate entry, and try fixing from there itself.

    Any further coding help, you can ask here.

    Hope it helps.

  • Community Member Profile Picture
    on at

    after looking into it some more. I made some mods and this worked.

    //create po for items selected -- only works on the selected item

    IF Type = Type::Item THEN BEGIN

    ServiceLine.RESET;

    ServiceLine.SETRANGE(ServiceLine."Document No.", "Document No.");

     IF ServiceLine.FINDSET THEN REPEAT

       IF Item.GET(ServiceLine."No.") THEN BEGIN

        WITH NewPurchLine DO BEGIN

        Item.RESET;

        INIT;

        "Document No." := PurchNo;

        "Document Type" := "Document Type"::Order;

        IF LineNo = 0 THEN

          LineNo := 10000;

          "Line No." := LineNo;

          INSERT(TRUE);

          VALIDATE(Type,Type::Item);

          VALIDATE("No.",Item."No.");

          VALIDATE(Quantity,ServiceLine.Quantity);

          VALIDATE("Unit Cost",ServiceLine."Unit Cost");

          VALIDATE("Unit of Measure Code",ServiceLine."Unit of Measure Code");

          MODIFY;

          LineNo := LineNo +10000;

        END;

    END;

    UNTIL ServiceLine.NEXT = 0;

    END;

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics NAV (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans