0108

The Accrual That Posts Itself

recurring-journal
allocation
dimensions
posting
general-journal

The Accrual That Posts Itself

The rent is the same every month, and the cost belongs to the departments that occupy the building. Typing that journal twelve times a year is how typos become audit findings — so you build the journal once, and let it post itself period after period, splitting the cost on the way.

Your codeunit only builds the journal. The grading tests hand you the accounts, the departments and the percentages, then post the batch with the standard Gen. Jnl.-Post Batch routine — twice — and read the general ledger. None of your code runs while the batch posts, so everything below has to be carried by the journal setup you left behind.

What you build

A codeunit named "Recurring Rent Accrual" with three public procedures:

procedure CreateAccrualBatch(TemplateName: Code[10]; BatchName: Code[10])
procedure AddAccrualLine(TemplateName: Code[10]; BatchName: Code[10]; DocumentNo: Code[20]; PostingDate: Date; AccrualAccountNo: Code[20]; LineAmount: Decimal; Method: Enum "Gen. Journal Recurring Method"; Frequency: DateFormula; ExpirationDate: Date): Integer
procedure AddDepartmentShare(TemplateName: Code[10]; BatchName: Code[10]; AccrualLineNo: Integer; ExpenseAccountNo: Code[20]; DepartmentCode: Code[20]; SharePct: Decimal)
  • CreateAccrualBatch creates the journal the accrual lives in: a general journal template named TemplateName, holding a batch named BatchName. The names handed to you are always free — no template of that name exists yet.
  • AddAccrualLine adds one line to that batch and returns its "Line No.". The line posts LineAmount to G/L account AccrualAccountNo under document number DocumentNo, is first due on PostingDate, comes back every Frequency, is treated according to Method, and stops after ExpirationDate (0D = never). LineAmount is signed the way it must reach the ledger: the rent accrual is a credit, so the tests pass a negative amount.
  • AddDepartmentShare gives SharePct percent of the line identified by AccrualLineNo to G/L account ExpenseAccountNo, tagged with DepartmentCode — a value of global dimension 1. A line can be given any number of shares, and they always add up to 100 percent.

Pick object IDs in the range 50100–50199, and refer to every other object by name, never by ID.

What has to happen when the batch is posted

  1. One run, one document. Posting the batch writes one G/L entry for the accrual line — LineAmount on AccrualAccountNo — plus one entry per department share. Every entry of the run carries the line's posting date and its document number.
  2. The shares are the balancing entries. A share posts its percentage of the line amount to ExpenseAccountNo with the opposite sign, and its DepartmentCode reaches the posted entry as its Global Dimension 1 Code. Together the shares cover the line exactly: their amounts sum to precisely minus the line amount, even when the percentages don't divide evenly (33.33 / 33.33 / 33.34 of 100.01 must still add up to 100.01, to the cent).
  3. A run leaves the journal ready for the next one. The line and its shares stay in the journal, and the line's posting date has moved on by Frequency. Posting the batch again — with nobody editing anything in between — posts the whole accrual again, one frequency later.
  4. Method decides what happens to the amount. With "F Fixed" the line keeps its amount for the next run. With "V Variable" the amount is cleared to 0 after posting, and the line waits in the journal for the next period's figure.
  5. "RF Reversing Fixed" reverses itself. Everything the run posted is posted again mirrored — accrual line and department shares alike — dated the day after the posting date.
  6. ExpirationDate is the last date the line may post. Once the line's posting date is later than its expiration date, the line posts nothing and does not move on either; other lines in the same batch post as usual.

What the tests check

Ten tests build a journal through your three procedures, post it, and read what reached the general ledger: a random rent amount split over two departments by a random percentage (hardcoding the example figures won't survive), the accrual entry itself with an exact entry count and document number for a single run, a second run that must repeat the whole accrual one frequency later on its own, the line's posting date after a run, "F Fixed" keeping the amount versus "V Variable" clearing it, the mirrored entries of a "RF Reversing Fixed" line on the following day, a batch holding one live and one expired line, a line whose posting date falls exactly on its expiration date and must still post, and the 100.01 split three ways that must not lose a cent. Amounts are matched exactly, and department amounts are read from the Global Dimension 1 Code on the posted entries.

Learn More

Hint 1
None of your code runs while the batch posts, so every rule in the statement has to be recorded in the journal's own setup tables: the template decides whether posting consumes the lines or keeps them, the line itself carries when it comes back and how it is treated, and the department split belongs to a table of its own. Start with what a general journal template can be flagged as.
Hint 2
A template with Recurring = true turns its batches into recurring journals: posting keeps the lines, moves each line's "Posting Date" on by its "Recurring Frequency", honours the "Recurring Method" and skips lines whose posting date has passed their "Expiration Date". Such a template must leave "No. Series" blank, so every line needs a "Document No." of its own — and a run only picks up lines that are due, meaning dated no later than the work date.
Hint 3
The split lives in table "Gen. Jnl. Allocation", keyed by journal template, batch, the accrual line's "Line No." and a line number of its own. Insert the record before filling it in — its OnInsert reads the journal line it belongs to — then validate "Account No.", "Shortcut Dimension 1 Code" and "Allocation %". Validating the percentage is what computes each share's amount: opposite in sign to the line, with the rounding remainder carried from share to share so the split lands to the cent.
ALBusiness Central 28.5
Press Compile to check your code compiles — Submit runs the tests.
The code editor is desktop-only
Open this problem on a computer to write and run code. Reading the description, tests and discussion works fine here.