> For the complete documentation index, see [llms.txt](https://developer.collibra.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.collibra.com/tutorials/add-an-attachment-with-a-workflow.md).

# Add an attachment with a workflow

You can use workflows to add attachments to a community domain or asset in your Collibra Platform.

<img src="https://2912049260-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8DYDBo6jzvQ73H6CZj6k%2Fuploads%2Fgit-blob-1649f0882f8cf2f1c160322a22cdf1672d3c88d2%2Fdocs_howto_attachment-workflow-overview.png?alt=media" alt="" width="266">

## Example workflow

Take the user task script tasks from this [example workflow](https://cdn.collibra.com/developer/AddAttachment.bpmn) and integrate the upload functionality into your own wrokflow. The user task asks for a file to be uploaded. The script task retrieves the required information from that file and uses the `AddAttachmentRequest` builder of the Java Core API to attach it to the current resource.

The user task form has a `fileUpload` field with the ID `file` that is marked as required.

The Groovy script task performs the following operations:

* Sets a fileName and a *fileStream* variable that are required by the add attachment request builder:

  ```groovy
  UUID fileUUID = string2Uuid(file)
  FileInfo fileInfo = fileApi.getFileInfo(fileUUID)
  String fileName = fileInfo.getName()

  InputStream fileStream = fileApi.getFileAsStream(fileUUID)
  ```

  <div data-gb-custom-block data-tag="hint" data-style="success" class="hint hint-success"><p>The first three lines are meant to illustrate the various Java classes that are being used to get the name of the uploaded file. You can use <code>String fileName = fileApi.getFileInfo(string2Uuid(file)).getName()</code> instead.</p></div>
* Builds the add attachment request:

  ```groovy
  AddAttachmentRequest attachmentRequest = AddAttachmentRequest.builder()
      .baseResourceId(item.getId())
      .baseResourceDiscriminator("Asset")
      .fileStream(fileStream)
      .fileName(fileName)
      .build()
  ```

  <div data-gb-custom-block data-tag="hint" data-style="success" class="hint hint-success"><p>See the <a href="/tutorials/introduction-to-builders.md">Introduction to builders</a> tutorial for details about the builder method.</p></div>

  <div data-gb-custom-block data-tag="hint" data-style="info" class="hint hint-info"><p>The <code>item</code> variable refers to the current resource you are attaching the file to: a community, domain or asset. You use it to access the <strong>WorkflowBusinessItem</strong> bean of the Workflow Java API.</p></div>
* Attaches the uploaded file to the current resource:

  ```groovy
  attachmentApi.addAttachment(attachmentRequest)
  ```

Here is the complete script:

```groovy
import com.collibra.dgc.core.api.dto.instance.attachment.AddAttachmentRequest
import com.collibra.dgc.core.api.model.file.FileInfo

UUID fileUUID = string2Uuid(file)
FileInfo fileInfo = fileApi.getFileInfo(fileUUID)
String fileName = fileInfo.getName()

InputStream fileStream = fileApi.getFileAsStream(fileUUID)

AddAttachmentRequest attachmentRequest = AddAttachmentRequest.builder()
    .baseResourceId(item.getId())
    .baseResourceDiscriminator("Asset")
    .fileStream(fileStream)
    .fileName(fileName)
    .build()

attachmentApi.addAttachment(attachmentRequest)
```

{% hint style="info" %}
There is no need to explicitly import `com.collibra.dgc.core.api.component.instance.AttachmentApi` and `com.collibra.dgc.core.api.component.file.FileApi`. In the context of workflow script tasks, the **\<Resource>Api** interfaces (such as AssetApi, CommunityTypeApi, FileApi, and so on) are already instantiated and accessible via **\<resource>Api** variables (such as assetApi, communityTypeApi, fileApi, and so on).
{% endhint %}


---

# 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://developer.collibra.com/tutorials/add-an-attachment-with-a-workflow.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.
