根据NAV系统登录用户设置单据编号系列
Alright, let's fix this up properly. You're looking to auto-assign a document number in the Gen Journal Line table based on the logged-in user, pulling the next sequential number from their dedicated series (MRVRPOS1 for user LACT-MIRA-POSMR01, MRVRPOS2 for LACT-MIRA-POSMR02). Your initial code had a couple of gaps—let's walk through the correct implementation step by step.
First, let's break down what was missing in your original approach:
- You weren't specifying which series to pull (your
SeriesLine.GET("Series Code")didn't target the user's specific series) - You tried to assign the series code directly to
Document No., but we need to generate the next valid number from that series, not just use the code itself - Multiple
IFstatements can be cleaned up with aCASEstructure for better readability
Here's the correct AL code (works for both Dynamics NAV and Business Central) to implement this logic, typically added to the OnInsert trigger of the Gen Journal Line table:
trigger OnInsert() var Series: Record Series; CurrentUserID: Code[50]; AssignedSeriesCode: Code[20]; begin // Grab the current logged-in user's ID CurrentUserID := UserId(); // Map each user to their designated series case CurrentUserID of 'LACT-MIRA-POSMR01': AssignedSeriesCode := 'MRVRPOS1'; 'LACT-MIRA-POSMR02': AssignedSeriesCode := 'MRVRPOS2'; else // Handle users without an assigned series—adjust this to fit your needs Error('No document series assigned to user %1', CurrentUserID); end; // Verify the assigned series exists in the system if not Series.Get(AssignedSeriesCode) then Error('Series %1 is not set up in the system', AssignedSeriesCode); // Generate the next valid document number from the series "Document No." := Series.GetNextNo(WorkDate(), true); // Note: In most NAV/Business Central versions, GetNextNo() automatically updates the series' "Last No. Used" field. // If your setup requires manual updates (uncommon), uncomment the lines below: // Series."Last No. Used" := "Document No."; // Series.Modify(); end;
Key Notes to Make This Work:
- Series Setup: Ensure the series
MRVRPOS1andMRVRPOS2are properly configured in the Series table (with valid number ranges, posting date settings, etc.). - Trigger Timing: The OnInsert trigger runs when a new Gen Journal Line record is created—this ensures the document number is assigned as soon as the line is added, whether manually via the page or through automated processes.
- Error Handling: The
elseclause in theCASEstatement throws an error for unassigned users, but you can adjust this to use a default series instead if needed. - Permissions: Make sure the user accounts have read access to the Series table and write access to the Gen Journal Line table's Document No. field.
That should do it—this logic will correctly pull the next sequential number from the user's dedicated series every time a new journal line is created.
内容的提问来源于stack exchange,提问作者Cátia Matos

