Skip to main content

Notifications

Microsoft Dynamics NAV (Archived)

To delete the lines from Sales line

Posted on by Microsoft Employee

Hello,

I have one scenario where I have to delete the Sales line and order.


I have written a code where i am able to delete the header and line.

But the problem is if Sales header = 100 contains 3 sales line there in each line we have to check that fields:

Sales Line.Qty= Sales Line Temp. Call Off Qty  IF true then

Doc No.                Line No.                    Item No.            Qty           Call Off Qty

100                       10000                       ABC                    2               2

100                       20000                       ABC                    2               1                           //this should be there in sales line along with header

100                       30000                       ABC                    3               3

 

The bold  sales lines has to be deleted instead of whole sale line and header.

 

Can u suggest how to write the code for this :

I have written the below code where whole sales header and lines are deleting.

Its an urgent requirement please tell how to rectify my code.


IF recCustomer.GET(pCustomerNo) THEN BEGIN
  //lrecSalesLineTemp.DELETEALL;
  lrecSalesLineTemp.RESET;
  lrecSalesLineTemp.SETRANGE("Customer No.",pCustomerNo);
  lrecSalesLineTemp.SETRANGE(Processed,TRUE);
  IF lrecSalesLineTemp.FINDSET(FALSE,FALSE) THEN
  BEGIN
       lrecSalesLine.RESET;
       lrecSalesLine."Document Type" := lrecSalesLine."Document Type"::Order;
       lrecSalesLine.SETRANGE("Document No.",lrecSalesLineTemp."Document No.");
       lrecSalesLine.SETRANGE("Line No.",lrecSalesLineTemp."Line No.");
       lrecSalesLine.SETRANGE("No.",lrecSalesLineTemp."Item No.");
       lrecSalesLine.SETRANGE("Sell-to Customer No.",pCustomerNo);
       lrecSalesLine.SETRANGE("Outstanding Quantity",lrecSalesLineTemp."Outstanding Quantity");
       IF lrecSalesLine.FINDSET THEN
       BEGIN
         REPEAT
            IF ( lrecSalesLine.Quantity > lrecSalesLineTemp."Call Off Quantity") THEN
            BEGIN
                 lrecSalesLine.Quantity := lrecSalesLine.Quantity - lrecSalesLineTemp."Call Off Quantity";
                 MESSAGE('%1', lrecSalesLine.Quantity);
                 lrecSalesLine.MODIFY(TRUE);
                 //lrecSalesLineTemp."New Sales Order No.":=lrecSalesLine."Document No.";
            END

            //here in else part i am going wrong as in this it has to check each and every line and if all lines satisfies the below condition then it should delete the lines along with header else only the particular line of that SO no which satisfies the below condition has to be deleted.
            ELSE IF (lrecSalesLine.Quantity = lrecSalesLineTemp."Call Off Quantity") THEN                                  
            BEGIN
                CLEAR(lrecSalesHeader);
                IF lrecSalesHeader.GET(lrecSalesLine."Document Type",lrecSalesLine."Document No.") THEN
                  lrecSalesLine.DELETE;
                  MESSAGE('HELLO');
                  lrecSalesHeader.DELETE;
                  MESSAGE('hi');
            END;
         UNTIL lrecSalesLine.NEXT=0;
       END;
  END;
END;

*This post is locked for comments

  • bharathkarunakaran Profile Picture
    bharathkarunakaran 475 on at
    RE: To delete the lines from Sales line

    [quote user="Pushpa "]

    Hello,

    I have one scenario where I have to delete the Sales line and order.


    I have written a code where i am able to delete the header and line.

    But the problem is if Sales header = 100 contains 3 sales line there in each line we have to check that fields:

    Sales Line.Qty= Sales Line Temp. Call Off Qty  IF true then

    Doc No.                Line No.                    Item No.            Qty           Call Off Qty

    100                       10000                       ABC                    2               2

    100                       20000                       ABC                    2               1                           //this should be there in sales line along with header

    100                       30000                       ABC                    3               3

     

    The bold  sales lines has to be deleted instead of whole sale line and header.

     

    Can u suggest how to write the code for this :

    I have written the below code where whole sales header and lines are deleting.

    Its an urgent requirement please tell how to rectify my code.


    IF recCustomer.GET(pCustomerNo) THEN BEGIN
      //lrecSalesLineTemp.DELETEALL;
      lrecSalesLineTemp.RESET;
      lrecSalesLineTemp.SETRANGE("Customer No.",pCustomerNo);
      lrecSalesLineTemp.SETRANGE(Processed,TRUE);
      IF lrecSalesLineTemp.FINDSET(FALSE,FALSE) THEN
      BEGIN
           lrecSalesLine.RESET;
           lrecSalesLine."Document Type" := lrecSalesLine."Document Type"::Order;
           lrecSalesLine.SETRANGE("Document No.",lrecSalesLineTemp."Document No.");
           lrecSalesLine.SETRANGE("Line No.",lrecSalesLineTemp."Line No.");
           lrecSalesLine.SETRANGE("No.",lrecSalesLineTemp."Item No.");
           lrecSalesLine.SETRANGE("Sell-to Customer No.",pCustomerNo);
           lrecSalesLine.SETRANGE("Outstanding Quantity",lrecSalesLineTemp."Outstanding Quantity");
           IF lrecSalesLine.FINDSET THEN
           BEGIN
             REPEAT
                IF ( lrecSalesLine.Quantity > lrecSalesLineTemp."Call Off Quantity") THEN
                BEGIN
                     lrecSalesLine.Quantity := lrecSalesLine.Quantity - lrecSalesLineTemp."Call Off Quantity";
                     MESSAGE('%1', lrecSalesLine.Quantity);
                     lrecSalesLine.MODIFY(TRUE);
                     //lrecSalesLineTemp."New Sales Order No.":=lrecSalesLine."Document No.";
                END

                //here in else part i am going wrong as in this it has to check each and every line and if all lines satisfies the below condition then it should delete the lines along with header else only the particular line of that SO no which satisfies the below condition has to be deleted.
                ELSE IF (lrecSalesLine.Quantity = lrecSalesLineTemp."Call Off Quantity") THEN                                  
                BEGIN
                    CLEAR(lrecSalesHeader);
                    IF lrecSalesHeader.GET(lrecSalesLine."Document Type",lrecSalesLine."Document No.") THEN
                      lrecSalesLine.DELETE;
                      MESSAGE('HELLO');
                      lrecSalesHeader.DELETE;
                      MESSAGE('hi');
                END;
             UNTIL lrecSalesLine.NEXT=0;
           END;
      END;
    END;

    [/quote]Can i know where  put these codes .

    we should put in sales header or sales line ON delete  trigger

  • Suggested answer
    Rabin Profile Picture
    Rabin 2,976 on at
    RE: To delete the lines from Sales line

    Hi

    I agree with Mohana, Do this in phases...

    First delete Lines and keep the loop until all the line with condition is met...

    Finally you will be outside line loop then check if any line exist for True condition Do not delete header rec else Delete...

    -------------------------

  • Suggested answer
    4BzSoftware Profile Picture
    4BzSoftware 6,071 on at
    RE: To delete the lines from Sales line

    Hi Pushpa,

    In you case, use 2 Sales Line record variables: one for loop, one for delete:

    lRec_SalesLineLoop.SETRANGE("Document Type", lRec_SalesHeader."Document Type");

    lRec_SalesLineLoop.SETRANGE("Document No.", lRec_SalesHeader."No.");

    IF (lRec_SalesLineLoop.FINDSET) THEN

     REPEAT

       IF (lRec_SalesLineLoop.Quantity = lRec_SalesLineLoop."Call Off Quantity") THEN BEGIN

         lRec_SalesLineDelete.GET(lRec_SalesLineLoop."Document Type", lRec_SalesLineLoop."Document No.");

         lRec_SalesLineDelete.DELETE(TRUE);

       END;

     UNTIL (lRec_SalesLineLoop.NEXT = 0);

  • Suggested answer
    Mohana Yadav Profile Picture
    Mohana Yadav 59,135 Super User 2024 Season 2 on at
    RE: To delete the lines from Sales line

    In else part delete only Sales Line and after finishing the loop of all lines with document no. check whether there are any lines existing with that order no.

    if lines are not existing then delete the header

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans