# Finding an archived document via search

# Finding an archived document via search

The [Attachment Download](/books/freeze-for-dynamics-365-bc-en-us/page/attachment-download) page describes how to download the attachments of an archive record you have already found. This page covers the step before that: how to find the matching archive record – including **cross-company**, e.g. in an intercompany scenario to locate the sales document of the partner company from a purchase invoice.

## Process

1. Build a search term (Lucene query)
2. Submit the search to Freeze
3. Load the result and its attachments into temporary tables
4. Download the attachments (see the "Attachment Download" page)

## Public procedures used

### DXP Freeze Search Mgt. (Codeunit 70954893)

| Procedure | Purpose |
|-----------|---------|
| `GetSearchCombinationForTable(SystemId: Guid; TableNo: Integer): Text` | Ready-made query for a **local** document (SysLink + customer/vendor + current company). |
| `ToSafeSearchTerm(OldSearchTerm: Text): Text` | Lucene escaping + URL encoding of a search value. Use for every dynamic value. |
| `SystemLinkSearchQuery(SystemId: Guid; TableNo: Integer): Text` | Building block `SysLink:{GUID}-{TableNo}`. |

> **Cross-company:** `GetSearchCombinationForTable` cannot be used here – it requires the `SystemId` of the original document and automatically appends the **current** `CompanyName()`. To search in another company, build your own query and set the partner company explicitly via the `Company` field.

### DXP Freeze API Mgt. (Codeunit 70954891)

| Procedure | Purpose |
|-----------|---------|
| `SubmitSearch(SearchQuery: Text): JsonObject` | Submit a search (1 result/page). |
| `SubmitSearch(SearchQuery: Text; ItemsPerPage: Integer): JsonObject` | Search with page size. |
| `SubmitSearch(SearchQuery: Text; StoreName: Text[30]): JsonObject` | Search in a specific store. |

### DXP FRZ Result Mgt. (Codeunit 70954894)

| Procedure | Purpose |
|-----------|---------|
| `CreateResultForAttachments(QueryResult: JsonObject; var TempFrzResultRecordHeader; var TempFrzAttachmentResult; SearchQueryTxt: Text)` | Loads results + attachment metadata from the JSON into temporary tables. |
| `HasRecord(SearchString: Text): Boolean` | Checks whether there is any result at all. |
| `GetEffectiveResultsCount(SearchString: Text): Integer` | Returns the number of results. |

## Query structure

A query consists of `FieldName:"Value"` pairs joined with ` AND ` / ` OR `. Field names use `_` instead of spaces/special characters (e.g. `Sell_to_Customer_No`). Always escape values via `ToSafeSearchTerm`.

Example – sales invoice by document number, document type and company:

```
(No:"SO-2026-001" AND Document_Type:"2" AND Company:"SALES INC")
```

`Document_Type:"2"` corresponds to the *Invoice* document type.

## Code example

```al
procedure FindSalesDocInArchive(SalesDocNo: Code[20]; PartnerCompany: Text): Boolean
var
    TempResultHeader: Record "DXP FRZ Record Result Header" temporary;
    TempAttachment: Record "DXP FRZ Attachment Result" temporary;
    FrzApiMgt: Codeunit "DXP Freeze API Mgt.";
    FrzResultMgt: Codeunit "DXP FRZ Result Mgt.";
    FrzSearchMgt: Codeunit "DXP Freeze Search Mgt.";
    QueryResult: JsonObject;
    SearchQuery: Text;
    Base64: Text;
begin
    // 1) Build the query (cross-company)
    SearchQuery := StrSubstNo(
        '(No:"%1" AND Document_Type:"2" AND Company:"%2")',
        FrzSearchMgt.ToSafeSearchTerm(SalesDocNo),
        FrzSearchMgt.ToSafeSearchTerm(PartnerCompany));

    // 2) Submit the search
    QueryResult := FrzApiMgt.SubmitSearch(SearchQuery, 50);

    // 3) Load results + attachments
    FrzResultMgt.CreateResultForAttachments(
        QueryResult, TempResultHeader, TempAttachment, SearchQuery);
    if TempAttachment.IsEmpty() then
        exit(false);

    // 4) Fetch the attachment as Base64 (see "Attachment Download" page)
    TempAttachment.FindSet();
    repeat
        Base64 := FrzApiMgt.GetSpecificRecordAttachment(
            TempAttachment."File Link",
            TempAttachment.ID,
            TempAttachment.Filename,
            false); // false = no direct browser download
        // ... process Base64 (e.g. store as document attachment)
    until TempAttachment.Next() = 0;

    exit(true);
end;
```

> **Note:** Storing into the document attachment (table "Document Attachment", 1173) is standard Business Central functionality and not part of the Freeze module.