Mass updating sales lines

markclemarkcle Member Posts: 65
Hi All

Written a few small extensions and need to write one that updates all sales lines with no shipments. We frequently have to swap out items for replacements (but need to keep the old ones as we may sell later)

So I need to create a codeunit to swap the item on sales lines. I've got it working except for when the sales order is released.

How do I check if it's open, reopen and re-release after update?



Here's where I got to with the code unit
var
        // Local var
        SalesLine: record "Sales Line";
        SalesHeader: record "Sales Header";    
        ReleasesSalesLine: record "Sales Line";
        LineCount: Integer;
        SalesOrderList: Text;


    begin
        LineCount := 0;

        // Find all sales lines with the item on
        SalesLine.SETRANGE("Document Type", 1);            
        SalesLine.SETRANGE(Type, SalesLine.Type::Item);
        SalesLine.SetFilter("No.", '= %1', SourceItemNo);


        IF SalesLine.FINDSET(TRUE) THEN
            REPEAT
                SalesHeader.Get(SalesLine."Document Type", SalesLine."Document No.");

 
                

                 SalesLine.Validate("No.", NewItemNo);     // Change item no and validate

                SalesOrderList += SalesLine."Document No." + ' Line: ' + Format(SalesLine."Line No.") + '\';
                SalesLine.MODIFY(TRUE);
                LineCount := LineCount + 1;                 // Update line count var
            UNTIL SalesLine.NEXT = 0;
        Message('Done ' + '\' + SalesOrderList);
       


    end;

Answers

  • AlexeyShaminAlexeyShamin Member Posts: 80
    edited 2020-09-21
    Hello!
    Try this one



    IF SalesLine.FINDSET(TRUE) THEN
    REPEAT
    SalesHeader.Get(SalesLine."Document Type", SalesLine."Document No.");
    LastState := SalesHeader.Status; //LastState - integer
    SalesHeader.Status := SalesHeader.Status::Open ; //Don't use VALIDATE
    SalesHeader.MODIFY;
    //Don't run MODIFY trigger

    SalesLine.Validate("No.", NewItemNo); // Change item no and validate

    SalesOrderList += SalesLine."Document No." + ' Line: ' + Format(SalesLine."Line No.") + '\';
    SalesLine.MODIFY(TRUE);

    SalesHeader.Status := LastState;
    SalesHeader.MODIFY;

    LineCount := LineCount + 1; // Update line count var
    UNTIL SalesLine.NEXT = 0;

    But best way:
    1. reopen Sales Order using CU 414 "Release Sales Document" function Reopen
    2. Release Sales Order after Line modification using same CU
  • markclemarkcle Member Posts: 65
    edited 2020-09-21
    Thanks a lot. That seems to work, but did get this:

    An implicit conversion is being performed from a value of type 'Enum "Sales Document Status"' to a value of type 'Integer'. This conversion can lead to unexpected runtime issues. This warning will become an error in a future release.

    Fixed by using this:

    LastState: Enum "Sales Comment Document Type";
Sign In or Register to comment.