Today I want to discuss about how to populate a sub page with SourceTableTemporary in a Wizard.
In the following example I am using the NAV mobile web client. In addition, I blurred real app data because in real life I used this for other specific things.
1) First, let’s see an example of what we want to achieve:
- For instance, we need to create a Wizard page (similar to the Assisted Setup pages) for moving all items from a specific Location to another Location.
- In the first step the wizard displays information about what the process does and which are the steps. This information can be shown only if the Wizard is ran for the first time.
- After that, the user is asked to select the ‘To Location‘:
- Then press Next (right arrow in mobile web client). As a result, the wizard now asks to select the ‘From Location’:
- After selecting a Location and pressing Next, the user is asked to Confirm the item movement:
- Now after pressing Yes we want to show in a list sub page based on a stage table what quantities of each item will be moved. This is done because the user should be able to Review the created data before accepting it. (Example of stage table structure: ‘Item movement line’ with fields: Item No, Quantity, From Location, To Location).
- As you can see above, now the ‘Finish’ button is activated (highlighted with yellow)
- At this point the records are stored in Temporary Table and only displayed in the list sub page with SourceTableTemporary.
- The user has to review this information and has 2 options:
- Press ‘Finish’: this will copy the records from the temporary table to the real table.
- Cancel the process by closing the Wizard page or press ‘Cancel’: in this case the records will not be inserted from memory into the real table. The user is asked if he really wants to cancel process as the created data will be lost:
- After that, if user pressed ‘Finish’ action, a job queue will automatically process the created real ‘Item Movement Lines’ . It will post Item Journal Negative and Positive Adjustments for each Item and To/From Location.
2) How is the Wizard page designed:
- In my example the Wizard page is done in NAV 2017. Therefore, I am still using the old Nav Development Environment (but this can be applied in VS Code as well). The page looks like this:
*Notice the Part Page Group: the page used as part has property SourceTableTemporary = ‘Yes’. In case you are not familiar with Wizard pages in Microsoft Dynamics NAV, please check these links:
3) How is designed the code that populates with Temporary data the sub page used in Wizard page :
- In Triggers OnValidate and/or OnLookup of the ‘From Location’, use code similar to the following. As a result, the record behind the sub page with SourceTableTemporary is populated:
IF FromLocationCode <> '' THEN BEGIN YourCodeunit.InsertItemMovementLineForLocation(Rec,FromLocationCode,TempItemMovementLine); TempItemMovementLineSETRANGE("Added To Temp Page",FALSE); IF TempItemMovementLine.FINDSET THEN REPEAT CurrPage.ItemMovementLineTEMP.PAGE.AddTempItemMovementLine(TempItemMovementLine); UNTIL TempItemMovementLine.NEXT = 0; TempItemMovementLine.RESET; ClearFields(); End;
*‘YourCodeunit’ processes the item ledger entries of the ‘FromLocation’ and based on that inserts temporary records in table TempItemMovementLine
* Function ‘AddTempItemMovementLine’ is a global function in the sub page and it adds each record to the temporary ‘Rec’ on which the sub page is based (because property SourceTableTemporary is set up for that page.).
*The field ‘Added to Temp Page’ is needed because after populating data related to one ‘From Location’ user can press action ‘Back’ in the Wizard. Then select a new ‘From Location’ and all items from both 2 locations will be added to be moved to the ‘ToLocation’. In other words, when we validate the second time the ‘FromLocation’ we add to the Temporary Table only the items that were not already added in a previous step.
- When the ‘Finish’ action is pressed, the following code is executed:
LOCAL FinishAction() IF NOT CurrPage.TempItemMovementLine.PAGE.PostTempToRealItemMovementLine THEN ERROR(NoItemsToMove); AssistedSetup.SetStatus(PAGE::"Item Move by Location Wizard",AssistedSetup.Status::Completed); ProcessFinished := TRUE; CurrPage.CLOSE;
*Function ‘PostTempToRealItemMovementLine’ copies and Inserts the data from the ‘Rec’ temporary table to the real table once the user pressed ‘Finish’
- In sub page ‘Item Movement Lines’ we have following code:
AddTempItemMovementLine(VAR pTempItemMovementLine : TEMPORARY Record "Item Movement Line") Rec.SETRANGE("Item No.",pTempItemMovementLine."Item No."); IF NOT Rec.ISEMPTY THEN ERROR(ItemNoAlreadyAdded,Rec."Item No."); Rec.RESET; Rec := pTempItemMovementLine; Rec.INSERT; pTempItemMovementLine."Added To Temp Page" := TRUE; pTempItemMovementLine.MODIFY; ---------------------------------------------------------------- PostTempToRealItemMovementLine() : Boolean IF Rec.FINDSET THEN REPEAT ItemMovementLine.SETRANGE("User ID",Rec."User ID"); ItemMovementLine.SETRANGE("Line No.",Rec."Line No."); IF ItemMovementLine.ISEMPTY THEN BEGIN ItemMovementLine := Rec; ItemMovementLine.INSERT; END; UNTIL Rec.NEXT = 0 ELSE EXIT(FALSE); Rec.DELETEALL; EXIT(TRUE);
- And ‘most important’ code is in triggers OnFindRecord and OnNextRecord of this ‘special’ page:
OnFindRecord(Which : Text) : Boolean IF Rec.FIND(Which) THEN BEGIN EXIT(TRUE); END ELSE EXIT(FALSE); OnNextRecord(Steps : Integer) : Integer locResultSteps := Rec.NEXT(Steps); IF locResultSteps <> 0 THEN EXIT(locResultSteps);
*This is the code that displays the data inserted in the Temporary ‘Rec’ table into the sub page based on this temporary table
In conclusion, this is a pattern that helps displaying data in sub pages based on Temporary tables. Similarly, it can also be applied in other situations, not only in Wizards.