Saving Query Data to a File with SaveAsXml in AL

MB-820 By islever Editorial Last updated

Create queries in Business Central

Short answer

A Call SaveAsXml on the Items Query query variable to export its data to an XML file.

Question

You have a query object named Items Query.
You write code using an Items Query query variable.
You need to export the Items Query query data to a file.
Which SaveAs function should you use?
  1. SaveAsXml Correct Answer
  2. SaveAsHtml
  3. SaveAsExcel
  4. SaveAsJson

Key Takeaways

It tests your knowledge of the AL Query type's members, and the trap is assuming the Report type's SaveAsHtml/SaveAsExcel methods also apply to queries.

This MB-820 question tests which SaveAs method is available on an AL Query object. The only valid choice is A, SaveAsXml; the HTML and Excel variants belong to the Report type.

Common mistake: Choosing B, SaveAsHtml, because it produces a readable file — but SaveAsHtml exists only on the Report type, which has layouts and a rendering engine that a query object lacks.

Expert Analysis

The question asks about the Query data type in AL, not the Report data type, and that distinction decides everything.

What the code path actually does. You declare Items Query as a query variable, call Query.Open to execute the dataset against the database, and then call a SaveAs method on the same query variable to serialize the result set to a file. Microsoft's AL reference lists the members available on the Query type: Open, OpenXML, SaveAsXml, Read, GetRange, SetFilter, Close, and related members. SaveAsXml is the only SaveAs-family method that exists on the Query type — it writes the query result to an XML document (optionally with a specified XmlPort) in a file.

Why the other options fail. SaveAsHtml (B), SaveAsExcel (C), and in practice SaveAsWord/SaveAsPdf are members of the Report type, which runs a rendering engine over a report layout (RDL, Word, Excel layouts). A query object has no layout and no rendering pipeline, so there is nothing to render HTML or an Excel worksheet from. As one comment on this item correctly notes, "SaveAsHtml only exists for Reports, not Queries", so A is the only plausible option. SaveAsJson (D) does not exist on the Query type either — JSON serialization of query data must be done manually (for example by building a JsonObject/JsonArray from the record or query buffer) or by exposing the data through an API page.

The correct answer is therefore A — SaveAsXml. Use Items Query.SaveAsXml(FileName) after opening the query to export its data to an XML file.

Exam trap to remember. Microsoft exam questions routinely pair a Report scenario with a Query scenario and rotate the SaveAs method names between them. Always confirm which object type owns the method before selecting.

Exam Strategy

When a question names an object type (Query, Report, XmlPort), mentally list only the methods that type actually exposes before reading the options — this eliminates most distractors instantly. Here, seeing "query variable" should immediately rule out every Report-only rendering method such as SaveAsHtml and SaveAsExcel. If two options both seem to export data, pick the one tied to the correct object type rather than to a familiar file format.

Frequently Asked Questions

Which SaveAs methods does the AL Query data type support?

The Query type supports SaveAsXml, which writes the query result to an XML document in a file. It has no SaveAsHtml, SaveAsExcel, SaveAsWord, or SaveAsPdf methods; those belong to the Report type.

How do I export query results to Excel or JSON in Business Central?

A query cannot render Excel or JSON directly. Export to XML with SaveAsXml and transform it, or read the query buffer into records and build a JsonObject/JsonArray manually, or expose the data through an API page or an Excel-friendly report layout.

Do I need to open the query before calling SaveAsXml?

Yes. Execute the query with Items Query.Open() so it fetches data from the database, then call SaveAsXml to write the result set to a file, and finally call Close to release resources.

More MB-820 FAQ →

Community Votes

A
60%
B
40%

60% of anonymous learners picked answer A. Votes are pick records left by other test-takers — they are not the verified answer.

Discussion 1 contributions

Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.

From public community threads 1

  1. SpotSquare 👍 1 Answered A
    SaveAsHtml only exists for Reports, not Queries. So the plausible answer is A

Reader comments & corrections

No reader corrections yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

← Back to MB-820 Study Guide