Java · Track 2 · Tutorial 2 of 4

Check a chart before you send it

Ask BrizoConsol whether a chart is well formed before you send it - nothing is written, and every problem comes back with the record it's in.

You will needThe chart script from Send your chart of accounts.
By the endA check you can run on any payload before sending it, and an understanding of what it does and doesn't catch.

1When a check is worth it

Before you point your code at a real company, you can ask BrizoConsol whether a payload is well formed - without writing anything. Add /validate to the address and send the same body.

2Check a chart with a mistake in it

Create CheckChart.java. Its second account has no name:

CheckChart.java

// CheckChart.java - checks a chart before sending it. Nothing is written.
import com.fasterxml.jackson.databind.JsonNode;
import java.util.List;
import java.util.Map;

public class CheckChart {
    public static void main(String[] args) throws Exception {
        Brizo.Answer answer = Brizo.call("POST", "/organisations/" + Brizo.COMPANY_ID + "/accounts/validate", Map.of(
                "declarations", Map.of("source_reference", "my-ledger"),
                "records", List.of(
                        Map.of("code", "7000", "name", "Bank charges", "type", "Expense"),
                        Map.of("code", "7100", "type", "Expense")))); // no name

        if (answer.status() == 200) {
            System.out.println("Valid: " + answer.body().path("accepted").asText() + " records would be accepted.");
        } else {
            System.out.println("Not valid:");
            for (JsonNode error : answer.body().path("errors")) {
                System.out.println("  record " + error.path("records_index").asText() + ": "
                        + error.path("field").asText() + " - " + error.path("code").asText());
            }
        }
    }
}
What a check looks at, and what it doesn't
What a check covers: the shape of each record. Whether the type exists is only checked when you send.

3Run it

gradle -q run -Pmain=CheckChart

Check

Not valid:
  record 1: name - required_field_missing

Record 1 - the second record, counting from 0 - has no name. Add a name and run it again, and it answers Valid: 2 records would be accepted.

The two answers a check can give
Two possible answers: 200 with how many records would be accepted, or 422 with each problem.

4What a check can't tell you

A check looks at each record's shape. It doesn't look up whether the account type exists or whether the company has room for more accounts: those are only known when you send. And it writes nothing - a check is never a substitute for sending.

Want more detail? See the documentation