> For the complete documentation index, see [llms.txt](https://docs.avonnicomponents.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/pdf-viewer/show-the-latest-contract-on-an-opportunity.md). Agents can also search and read this documentation through its [MCP server](https://docs.avonnicomponents.com/~gitbook/mcp).

# Show the Latest Contract on an Opportunity

Open the most recent contract PDF attached to an Opportunity right on its record page, found by a Flow each time the page loads.

An account executive preparing a renewal call wants the signed contract in front of them, not a search through the Files related list. On the Northwind Fleet Renewal opportunity, a Flow finds the most recent PDF whose title contains "Contract" and the [PDF Viewer](https://docs.avonnicomponents.com/dynamic-components/components/pdf-viewer.md) opens it on the page.

<figure><img src="https://2532358799-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FODPvvv7Cx9Z9RECLn3oV%2Fuploads%2FsEQJfFjIBkncUntLzkaN%2Fpdf-viewer-latest-contract-result.png?alt=media" alt="PDF Viewer showing page 1 of Service Contract 2027, signed November 18, 2026"><figcaption><p>On Northwind Fleet Renewal, the Flow returns the newest contract and the viewer opens it.</p></figcaption></figure>

## Before you start

* An Opportunity with at least one PDF uploaded to it, with "Contract" in its title. This page uses Northwind Fleet Renewal, which holds Service Contract 2027 (draft), then Service Contract 2027 (signed), uploaded later.
* PDFs set to open in the browser: see the warning in [PDF Viewer](https://docs.avonnicomponents.com/dynamic-components/components/pdf-viewer.md).
* Permission to create and activate a Flow in your sandbox.

## Build it

Choose how you want to build:

✨ **Build with AI**: give your AI assistant a ready-to-use prompt.

🛠️ **Build with clicks**: follow the steps in the builder yourself.

{% tabs %}
{% tab title="Build with AI" icon="wand-magic-sparkles" slug="latest-contract-build-with-ai" %}
The `avonni-dynamic-components` skill drafts the Dynamic Component (variable, **On Load** Flow call and PDF Viewer); Salesforce must already hold the autolaunched Flow and the contract files.

{% stepper %}
{% step %}

### Set up the Avonni Skills and MCP server

Follow [Setup](https://docs.avonnicomponents.com/dynamic-components/build-with-ai/setup.md) to install the Avonni Skills, connect your AI assistant to the Avonni MCP server, and sign in to your sandbox through Salesforce CLI.

Build and activate the Flow first (step 1 of the **Build with clicks** tab), or ask the assistant to draft it as well.
{% endstep %}

{% step %}

### Copy the prompt and review the plan

Paste the prompt into your configured assistant. Check the target org and the field mappings in its plan before you approve it.

{% prompt description="Prompt for this tutorial" icon="wand-magic-sparkles" openInAIProviders="false" defaultExpanded="partial" %}
{% code overflow="wrap" collapsedlinecount="10" %}

```
Use the avonni-dynamic-components skill. Verify the dcPdfViewer component, the On Load interaction and the ExecuteFlow interaction against the Avonni MCP server (dynamic toolset) before you use any name.

Inspect my sandbox: confirm the autolaunched Flow Get_Latest_Contract_PDF (replace with the API name of my flow) is active, with a Text input variable recordId and a Text output variable contentDocumentId. Confirm that ContentVersion records exist with FirstPublishLocationId set to an Opportunity and FileExtension pdf.

Create a Dynamic Component with Target Page Object Opportunity. Add a Text variable resource contractFileId. Add an On Load Execute Flow interaction: Flow API Name Get_Latest_Contract_PDF, input variable recordId of type Text with the current record Id, output variable contentDocumentId stored in contractFileId. Add a PDF Viewer with Source {!contractFileId}, Title "Signed service contract", and a Height of 600px in its Size style.

Show me the plan and the field bindings before writing files. Then draft the local metadata, list what is left to set up by hand, and stop before deployment or activation.
```

{% endcode %}
{% endprompt %}
{% endstep %}

{% step %}

### Deploy to the sandbox and check the result

Review what the assistant generated and what it left for you to configure. Deploy the approved files to your sandbox only, finish any remaining setting with the **Build with clicks** tab, then check that Northwind Fleet Renewal opens on Service Contract 2027 (signed).
{% endstep %}
{% endstepper %}
{% endtab %}

{% tab title="Build with clicks" icon="screwdriver-wrench" slug="latest-contract-build-with-clicks" %}
{% stepper %}
{% step %}

### Build the Flow that finds the contract

The viewer needs the file's ContentDocumentId, and the page only knows the Opportunity Id. An autolaunched Flow bridges the two.

1. In Setup, create an **Autolaunched Flow (No Trigger)** named `Get Latest Contract PDF` (API name `Get_Latest_Contract_PDF`).
2. Create a Text variable `recordId`, **Available for input**, and a Text variable `contentDocumentId`, **Available for output**.
3. Add a **Get Records** element on **Content Version** with these conditions, all met: **First Publish Location ID** equals `{!recordId}`, **File Extension** equals `pdf`, **Is Latest** equals `{!$GlobalConstant.True}`, **Title** contains `Contract`. Sort by **Created Date**, descending, and keep only the first record.
4. Add an **Assignment**: `contentDocumentId` equals the **Content Document ID** of the record found.
5. Save and activate the Flow.

*Why Content Version:* its **First Publish Location ID** is the record the file was uploaded to, so one query finds the PDFs of this Opportunity and sorts them by date.
{% endstep %}

{% step %}

### Point the component at the Opportunity

1. Open your Dynamic Component, or create one, and set the **Target Page Object** to `Opportunity`.
2. Open **Resources** and click **New Resource**. Create a **Variable** with the **API Name** `contractFileId` and the **Data Type** **Text**.
   {% endstep %}

{% step %}

### Run the Flow when the page loads

1. Open the **Interactions** panel, click **Add On Load Interaction** (see [On Load](https://docs.avonnicomponents.com/dynamic-components/component-builder/on-load-interaction.md)) and set **Type** to **Execute Flow**.
2. **Flow API Name**: pick your flow, listed by its label, for example **Get Latest Contract PDF**.
3. Under **Flow Input Variables**, add one row: **Name** `recordId`, **Value** the current record's Id, **Type** **Text**.
4. Under **Output Variables**, add one row: **API Name** `contentDocumentId`, **Resource Name** `contractFileId`.

<figure><img src="https://2532358799-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FODPvvv7Cx9Z9RECLn3oV%2Fuploads%2FCVIgT1Xel20GiivNQ63q%2Fpdf-viewer-latest-contract-on-load.png?alt=media" alt="Edit On Load Interaction dialog with Execute Flow, Get Latest Contract PDF, input recordId and output contentDocumentId stored in contractFileId" width="480"><figcaption><p>The flow runs on load: recordId in, contentDocumentId out into contractFileId.</p></figcaption></figure>
{% endstep %}

{% step %}

### Add the PDF Viewer

1. Drag a **PDF Viewer** onto the canvas.
2. Set **Source** to `{!contractFileId}`.
3. Set **Title** to `Signed service contract`. Screen readers announce it; it is not printed on the page.
4. In the **Style** tab, under **Size**, set **Height** to `600` (unit **px**).
   {% endstep %}

{% step %}

### Save and test

1. Save the component and add it to the Opportunity record page in Lightning App Builder.
2. Open Northwind Fleet Renewal. The viewer shows Service Contract 2027 (signed), the most recent of the two contracts.
3. Upload a newer PDF with "Contract" in its title and reload the page: the viewer switches to it.
   {% endstep %}
   {% endstepper %}
   {% endtab %}
   {% endtabs %}

{% hint style="info" %}
**First Publish Location ID** is the record the file was first uploaded to. A file uploaded elsewhere and shared to the Opportunity later is linked through **Content Document Link** instead: query **Content Document Link** on **Linked Entity ID** in the Flow if your team shares files that way.
{% endhint %}

## Troubleshooting

<details>

<summary>The viewer shows an empty frame on an Opportunity with no contract</summary>

The Flow found no file, so **Source** is empty. Turn on **Show Placeholder** to show a message instead: see [Show a Placeholder When No PDF Is Attached](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/pdf-viewer/show-a-placeholder-when-no-pdf.md).

</details>

<details>

<summary>The contract exists but the frame stays blank</summary>

Check, in this order: the Flow is active and its output variable is **Available for output**; the **Output Variables** row stores `contentDocumentId` in `contractFileId`; **Source** is `{!contractFileId}`. For the other causes, see the blank viewer checklist in [Show a PDF from a URL on a Record](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/pdf-viewer/show-a-pdf-from-a-url.md).

</details>

<details>

<summary>The viewer shows an older contract</summary>

The newer file probably was not uploaded on this Opportunity, or its title does not contain "Contract". Check its **Title** and the hint above about files shared from another record.

</details>

## Related tutorials

* [Show a Placeholder When No PDF Is Attached](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/pdf-viewer/show-a-placeholder-when-no-pdf.md): what the viewer shows when the Flow finds nothing.
* [Pick a PDF from a List and Read It](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/pdf-viewer/pick-a-pdf-from-a-list.md): let users choose among all the PDFs of the record.
* [Execute Flow](https://docs.avonnicomponents.com/dynamic-components/component-builder/interactions/execute-flow.md): every setting of the Flow call used here.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.avonnicomponents.com/dynamic-components/tutorials/components/pdf-viewer/show-the-latest-contract-on-an-opportunity.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
