Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

Creating Purchase orders from Service orders

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Creating Purchase orders from Service orders

    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;

  • Suggested answer
    RE: Creating Purchase orders from Service orders

    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
    Community Member Microsoft Employee on at
    RE: Creating Purchase orders from Service orders

    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;

  • Verified answer
    RE: Creating Purchase orders from Service orders

    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 :)

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 Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans