> 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="/files/olWc7nNOYIT1oDsG3EKJ" 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="/pages/RiC2nQmzANa54fevkXPW">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 %}
