Canvas Apps Code Sample Dataverse

How to change the attachment on a note in canvas apps

A message promoting change!
A message promoting change!

The requirement - updating attachment on notes

This, without doubt, is the most challenging requirement of working with notes and attachments in canvas apps - how to change/update the attachment on a note.

The challenge

This requirement is challenging because although the two records are related in a way (Id of the attachment record is the same as the GUID of the note), there is no way to unrelate and relate an attachment with a note. And although you can access the Notes table, you can directly access the Attachments table. You can only access attachments related to a record using the dot operator e.g. galAccounts.Selected.Attachments.

The solution

This is one of the best solution approaches I have ever come up with! Here is the approach:

  1. Add a form to edit a given note (lets its galNotes.Selected). Select Title and Description fields
  2. Add another form to display the related Account. Select only one field, Attachment
  3. Make the following updates to the attachment control:
    1. Set the Items property to
      ForAll(
          Filter(
          galAccounts.Selected.Attachments,
          GUID(Id) = galNotes.Selected.Note
      ),
          {
              Id: PlainText(ThisRecord.Value),
              Value: ThisRecord.Value,
              AbsoluteUri: ThisRecord.AbsoluteUri,
              DisplayName: ThisRecord.DisplayName
          }
      )

      This is to display the attachment linked to the selected note

    2. Set the Max. attachments to 1

Before I share the OnSelect code of the Save/Update button, I want to outline the approach:

  1. First step is to create a new attachment (which will create a new associated note)
  2. Next step is to update the original note's following columns with the corresponding values from the new note created in step 1
    1. Document
    2. Mime Type
    3. File Name
  3. These above list of columns are what define the attachment of a note
  4. Now that we have copied over the properties of the new attachment to the original note, the last step is to delete the newly created attachment/note.

Given this approach, set the OnSelect of a button to:

SubmitForm(frmAccount);
With(//fetch the latest note created by submitting the account form
    {
        varNote: First(
            Sort(
                Filter(
                    galAccounts.Selected.Notes,
                    'Created By'.User = LookUp(
                        Users,
                        'User Name' = User().Email
                    ).User
                ),
                'Created On',
                SortOrder.Descending
            )
        )
    },
    Patch(//patch the selected note with document details from the new note
        Notes,
        galNotes.Selected,
        {
            Document: varNote.Document,
            'Mime Type': varNote.'Mime Type',
            'File Name': varNote.'File Name'
        }
    );
    Remove(//delete the new note that was created at the beginning of this code
        Notes,
        varNote
    )
)

Here is a quick demo:

Changing attachment of a note
Changing attachment of a note

So this is how I solved the problem of updating the attachment of a note in a canvas app (which is super easy in a model-driven app)!

Recent articles

  1. How to add a title and description when uploading an attachment in canvas apps
  2. How to show a combined gallery of notes and attachments
  3. What happens when you create an Attachment in Dataverse?

2 thoughts on “How to change the attachment on a note in canvas apps”

Leave a Reply