How to prevent an entity from being commited after a file is uploaded?

0
Hello,   I have two entities: Entity and EntityFile. EntityFile stores the file documents and is associated with Entity. I have an Entity_Overview page. In the page there is a "New" button. When the button is clicked, I create a new Entity object and open the Entity_NewEdit page. There are of course some fields of the entity. I also use the file dropper widget to let the user upload files. The widget uses EntityFile entity and association to context associates it to Entity.   Normally, the user either clicks "Save" button and validation check is made before commiting the object. Or the user clicks the "Cancel" button and everything is rolled back.   However, the issue is, when the user uploads a file, the Entity is automatically commited. If the user doesn't click Save or Cancel buttons and does something else (e.g. reload the page), my Entity object remains commited which I don't want. Is there any way to prevent this?
asked
5 answers
0

The System.FileDocument, due to it's implementation always autocommits. You hvae to handle this behaviour in custom logic.

A way to do this is to keep track of an attribute _isNew or something similar on true when uploading, setting it to false when saving, and removing any FileDocuments where _isNew is true when the session ends. You might also need some access rule xpath restriction to the owner, as else other users might have access while it's in the database

answered
0

 

Hi Ahmer,

check these community question

 

https://community.mendix.com/link/space/korean-community/questions/141855

            or use this method to solve the answer both are same if you understand the below answer gothrough the above posted link

Automatic Commit After File Upload:

When a file is uploaded using the file dropper, it automatically commits the file to the system.Add Attribute (isSaved):

Add an attribute named isSaved to track whether the file has been successfully committed. After committing the file, set isSaved to true.Retrieve from Database with Condition:

When retrieving documents from the database, filter the results by checking the condition isSaved = true.This ensures that only the uploaded and committed documents are displayedcheck the list count it will show correctly.

answered
0

Hello Ahmet, 

Did you any Solution for this?

 

answered
0

Hi Ahmet, this happens because uploading a file via the file dropper widget forces the parent Entity to be committed immediately, to establish the association with the EntityFile. As a result, even if the user hasn’t clicked “Save,” the Entity gets saved prematurely. It was a widget features

 

Alternative Solution: 

  • Create the Entity only after saving don’t create the Entity when clicking “New.” Instead, wait until the user clicks “Save” to create and save it.

  • Store files temporarily let users upload files to a temporary place before the Entity exists. After saving the Entity, link the files to it.

  • Clean up if canceled if the user cancels or leaves the page without saving, delete any unfinished Entities or files that were saved automatically.

Files must link to a saved Entity, the Entity gets saved early. To avoid this, delay creating the Entity or clean up unwanted records.

 

I hope this one helps you! :)

answered
0

Hello Ahmet,

You may try to avoid create Entity Object and show page on click on 'New' button.

instead,

You may try to call a MF on click on 'New' button and try creating Object of Entity inside that MF and then show Page.

This way, I hope it won't auto commit Entity Object.

Thanks.

 

answered