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.
0
Answers
2) You can call the lookup form in this way: 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...
MVP - Dynamics NAV
My BLOG
NAVERTICA a.s.
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.
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?
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:
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.