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:
- Add a form to edit a given note (lets its galNotes.Selected). Select Title and Description fields
- Add another form to display the related Account. Select only one field, Attachment
- Make the following updates to the attachment control:
- 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
- Set the Max. attachments to 1
- Set the Items property to
Before I share the OnSelect code of the Save/Update button, I want to outline the approach:
- First step is to create a new attachment (which will create a new associated note)
- Next step is to update the original note's following columns with the corresponding values from the new note created in step 1
- Document
- Mime Type
- File Name
- These above list of columns are what define the attachment of a note
- 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:
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)!


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