Cloudastick Systems

Screen Flow Mandatory Attachments Without a Record Id

Screen flows offer a way to add attachments to records, but in order to do that the record should be committed to the database, as the file upload component in the screen flow needs the Id of the record to attach the file to it. Also there is no option to make that file upload component required

After some researching I found a workaround to make the file upload component required, but still the record has to be in the database to attach the file to it, which doesn't allow to make any logic after the files are uploaded and before creating and committing the record to the database.

After some more research I found a solution for it.

Add the file upload component without providing the record Id
When you add the component without providing the record Id the file is uploaded on Salesforce without being attached to anything, which allows us to attach it manually as shown in the next step.

Implement an invokable method to attach the file
The links between the files and the records are stored in a table called ContentDocumentLink. What the invocable method is going to do is adding a new record to the ContentDocumentLink table that contains the Id of the record that the file is going to be attached to (LinkedEntityId) and the Id of the file itself (ContentDocumentId). By that you can upload the files and link them whenever you want.

Below you can find a code example of the invocable method discussed above.

User-added image