Consolidate New Sales Orders in Previous SO

rlawsonrlawson Member Posts: 48
I am trying to emulate the F5 function key (Sales List) within code for the Sale Order Form. I hit F3 in the SO Form, tab, and enter a Sell-to Customer No.. My code checks to see if an existing order exists for that customent and brings up the Sales List Form (populated with only that customer). The client then selects a previous Order which then populates the SO Form. My code in in the OnValidate() trigger on the Sell-to Customer field:

IF "Sell-to Customer No." <> '' THEN
BEGIN
COMMIT;
SalesHeader2.RESET;
SalesHeader2.SETFILTER("Document Type",'=%1',"Document Type"::Order);
SalesHeader2.SETFILTER("Sell-to Customer No.",'=%1',"Sell-to Customer No.");
SalesHeader2.SETFILTER("No.",'<>%1',"No.");
IF SalesHeader2.FIND('-') THEN
BEGIN
CLEAR(SalesList);
SalesList.SETTABLEVIEW(SalesHeader2);
SalesList.LOOKUPMODE(TRUE);
CLEAR(FORM);
IF SalesList.RUNMODAL = ACTION::LookupOK THEN
BEGIN
SalesList.GETRECORD(SalesHeader2);
GET("Document Type"::Order,SalesHeader2."No.");
SETRANGE("No.");
END;

END;
END;
Everything looks like it functions properly; however, when I click on the SubForm or out of the Form it comes up with the error message:

"Do you want to rename the record?

and resets the form back to the new order.

Where should I put my code and what code should I be using so I don't get that rename error.

Answers

  • kinekine Member Posts: 12,562
    1) What is the purpose of the CLEAR(FORM) line?
    2) You can call the lookup form in this way:
    IF "Sell-to Customer No." <> '' THEN BEGIN
      SalesHeader2.RESET;
      SalesHeader2.SETRANGE("Document Type","Document Type"::Order);
      SalesHeader2.SETRANGE("Sell-to Customer No.","Sell-to Customer No.");
      SalesHeader2.SETFILTER("No.",'<>%1',"No.");
      IF not SalesHeader2.IsEmpty THEN BEGIN
        IF FORM.RUNMODAL(0,SalesHeader2) = ACTION::LookupOK THEN BEGIN
          GET("Document Type"::Order,SalesHeader2."No.");
          SETRANGE("No.");
          CurrForm.UPDATE(False);
        END;
      END;
    END;
    
    3) Call CurrForm.UPDATE(False) after you have changed the Rec to refresh the form.

    4) In your process there is one problem. When you are entering the Sell-To Customer No., you have already created new header with new number. After you select another Order, this new header will be still there, it means you will have empty header but user will be on some old order for the same custmer. In this way you will have still new and new and new header but working on another orders... just look strange for me...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • rlawsonrlawson Member Posts: 48
    I used your code and received the following error:
    The following C/AL functions can be used only to a limited degree during write transactions ((be....)
    Form.RunModal() is not allowed in write trasactons.
    ...
    ...
    Use the COMMIT function to save the changes before this call, or structure the code differently.


    When I add a COMMIT ABOVE THE reset, I get the error:
    CurrForm.UPDATE cannot be called from here.
  • rlawsonrlawson Member Posts: 48
    I moved my code from OnValidate to OnAfterValidate and commented out the CurrForm.UPDATE and it runs however, is CurrForm.Update required since it was there before. What does Currform.Update do in this instance? Oh yea, I was just trying to get my code to work with the clear(form). It is not needed.
  • rlawsonrlawson Member Posts: 48
    The Following code seems to work:

    Sell-to Customer No. - OnAfterValidate()
    CurrForm.UPDATE;

    IF "Sell-to Customer No." <> '' THEN BEGIN
    SalesHeader2.RESET;
    SalesHeader2.SETRANGE("Document Type","Document Type"::Order);
    SalesHeader2.SETRANGE("Sell-to Customer No.","Sell-to Customer No.");
    SalesHeader2.SETFILTER("No.",'<>%1',"No.");
    IF SalesHeader2.FIND('-') THEN BEGIN
    DELETE;
    COMMIT;
    IF FORM.RUNMODAL(0,SalesHeader2) = ACTION::LookupOK THEN BEGIN
    GET("Document Type"::Order,SalesHeader2."No.");
    SETRANGE("No.");
    CurrForm.UPDATE(False);
    END;
    END;
    END;


    Do you see anything wrong with it?
  • apertierraapertierra Member Posts: 61
    Well... Kine's code was fine, the problem comes with the "additions" in your code not being properly placed.
    The form.RUNMODAL can't be run if you are modifying the records, and your "DELETE" modifies the record.
    A proper way to do it would be:
    The Following code seems to work: 
    
    IF ("Sell-to Customer No." <> '') THEN 
    BEGIN 
      SalesHeader2.RESET; 
      SalesHeader2.SETRANGE("Document Type","Document Type"::Order); 
      SalesHeader2.SETRANGE("Sell-to Customer No.","Sell-to Customer No."); 
      SalesHeader2.SETFILTER("No.",'<>%1',"No."); 
      IF SalesHeader2.FIND('-') THEN 
      BEGIN 
        IF (FORM.RUNMODAL(0,SalesHeader2) = ACTION::LookupOK) THEN 
          BEGIN 
             DELETE;
             GET("Document Type"::Order,SalesHeader2."No."); 
             SETRANGE("No."); 
             CurrForm.UPDATE(False); 
          END
          ELSE
             DELETE;
        END; 
    END; 
    
  • rlawsonrlawson Member Posts: 48
    Thanks so much for your help.

    For some reason I have to have a commit before the runmodal:

    Sell-to Customer No. - OnAfterValidate()
    CurrForm.UPDATE;

    //KLH.04 start ***************************************************************************
    IF ("Sell-to Customer No." <> '') THEN
    BEGIN
    SalesHeader2.RESET;
    SalesHeader2.SETRANGE("Document Type","Document Type"::Order);
    SalesHeader2.SETRANGE("Sell-to Customer No.","Sell-to Customer No.");
    SalesHeader2.SETFILTER("No.",'<>%1',"No.");
    IF SalesHeader2.FIND('-') THEN
    BEGIN
    COMMIT;
    IF (FORM.RUNMODAL(0,SalesHeader2) = ACTION::LookupOK) THEN
    BEGIN
    DELETE;
    GET("Document Type"::Order,SalesHeader2."No.");
    SETRANGE("No.");
    CurrForm.UPDATE(False);
    END
    ELSE
    DELETE;
    END;
    END;
    //KLH.04 end



    It might have to do with all the other code I have in Sales Form.
Sign In or Register to comment.