Umbraco forms Automaticley Delete Form Data after submittion / send as email

Hi,

I been asked by one of my customers if it possible to delete a form entry once we have emailed it to them, I sort of got some code working.

But it crashes out with a FK Key Error.

public override Task ExecuteAsync(WorkflowExecutionContext context)
{
Guid formId = context.Form.Id;
var forms = _formService.Get(formId);

if (forms != null)
{
    var records = _recordService.GetAllRecords(forms);
    foreach (Record record in records)
    {
        if ( record.Id == context.Record.Id)
        {
            _recordService.DeleteAsync(record, forms);
            break;
        }

    }
}

return Task.FromResult(WorkflowExecutionStatus.Completed);

}

Here the part of the code I am using.

This is part of a work flow that fires after the records approved and been sent as an email to the customer.

Then I need it to delete the form record from the specific form.

So the only place there a copy of the data is in the person inbox.

Hi @darrenhunterEmerald

If you go to form settings in Umbraco Forms you can disable storing of entries in the database:

Can you do that instead of via a custom workflow?

If you need to store for approval first, the workflow should work. What error and exception do you get?

Justin

I am getting the following error:

SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint “FK_UFRecordFields_UFRecords_Id”. The conflict occurred in database “CombinedSiteV132024”, table “dbo.UFRecords”, column ‘Id’.
The INSERT statement conflicted with the FOREIGN KEY constraint “FK_UFRecordFields_UFRecords_Id”. The conflict occurred in database “CombinedSiteV132024”, table “dbo.UFRecords”, column ‘Id’.
The INSERT statement conflicted with the FOREIGN KEY constraint “FK_UFRecordFields_UFRecords_Id”. The conflict occurred in database “CombinedSiteV132024”, table “dbo.UFRecords”, column ‘Id’.
The INSERT statement conflicted with the FOREIGN KEY constraint “FK_UFRecordFields_UFRecords_Id”. The conflict occurred in database “CombinedSiteV132024”, table “dbo.UFRecords”, column ‘Id’.
The INSERT statement conflicted with the FOREIGN KEY constraint “FK_UFRecordFields_UFRecords_Id”. The conflict occurred in database “CombinedSiteV132024”, table “dbo.UFRecords”, column ‘Id’.
The INSERT statement conflicted with the FOREIGN KEY constraint “FK_UFRecordFields_UFRecords_Id”. The conflict occurred in database “CombinedSiteV132024”, table “dbo.UFRecords”, column ‘Id’.
The INSERT statement conflicted with the FOREIGN KEY constraint “FK_UFRecordFields_UFRecords_Id”. The conflict occurred in database “CombinedSiteV132024”, table “dbo.UFRecords”, column ‘Id’.
The INSERT statement conflicted with the FOREIGN KEY constraint “FK_UFRecordFields_UFRecords_Id”. The conflict occurred in database “CombinedSiteV132024”, table “dbo.UFRecords”, column ‘Id’.
The INSERT statement conflicted with the FOREIGN KEY constraint “FK_UFRecordFields_UFRecords_Id”. The conflict occurred in database “CombinedSiteV132024”, table “dbo.UFRecords”, column ‘Id’.
The INSERT statement conflicted with the FOREIGN KEY constraint “FK_UFRecordFields_UFRecords_Id”. The conflict occurred in database “CombinedSiteV132024”, table “dbo.UFRecords”, column ‘Id’.
The INSERT statement conflicted with the FOREIGN KEY constraint “FK_UFRecordFields_UFRecords_Id”. The conflict occurred in database “CombinedSiteV132024”, table “dbo.UFRecords”, column ‘Id’.
The statement has been terminated.
The statement has been terminated.
The statement has been terminated.
The statement has been terminated.
The statement has been terminated.
The statement has been terminated.
The statement has been terminated.
The statement has been terminated.
The statement has been terminated.
The statement has been terminated.
The statement has been terminated.

Regards

Darren

The only problem we have is if I turn off the store in DB, we have a custom code that exports the record to file. We used the form Record ID as the filename and it sets each file name to 0.csv. So I can update that to the time and date when the file was exported.

Has your code worked in the past?

Your workflow runs after the form is submitted but before Umbraco Forms finishes writing all UFRecordFields to the database.

_recordService.DeleteAsync(record, forms);

which from eye balling, deletes the parent UFRecord while Umbraco is still trying to insert UFRecordFields for that same record.

Is there a way after it saved and updated to delete the record???

So my understanding

  1. It creates the parent row first
  2. Your workflow runs
  3. Creates any child rows
  4. Saves the transaction

Your workflow is step 2, but the foreign‑key constraint requires that the parent record still exists at step 3. It could be a race condition you could have faced.

If available, you could look into the RecordSaved event (if your version allows it) alternatively this may help

public class DeleteRecordAfterSave : ApplicationEventHandler
{
private readonly IRecordService _recordService;
private readonly IFormService _formService;

public DeleteRecordAfterSave(IRecordService recordService, IFormService formService)
{
    _recordService = recordService;
    _formService = formService;

    RecordService.RecordSaved += RecordService_RecordSaved;
}

private void RecordService_RecordSaved(object sender, RecordSavedEventArgs e)
{
    var record = e.Record;
    var form = _formService.Get(record.Form);

    // Only delete if your workflow marked it as approved
    if (record.State == "Approved")
    {
        _recordService.Delete(record, form);
    }
}

}

Or feel free to list your version and someone can point you in the right direction.

You could potentially use a notificationHandler?

RecordSubmittedNotification
RecordApprovedNotification
RecordRejectedNotification
RecordSavingNotification
RecordStateChangeNotification

Another way is to wait until the successmessage/page is seen, you can get the recordId from the tempdata.. and delete then?

Thought this could be a little fraught with danger.. As if any workflows executing out of process you might be deleting the record before it’s still needed?

Perhaps a daily cleanup of records might be better? There’s something out of the box…

Umbraco Forms scheduled record deletion task will not run as it has been not enabled via configuration. To enable, set the configuration value at 'Umbraco:Forms:Options:ScheduledRecordDeletion:Enabled' to true.

Configuration | Forms | Umbraco Documentation

If I’m reading your use case, you probably want to DisableRecordIndexing too.

Though I have to say only retaining data in emails for the client is also dangerous as then you have a single point of failure non-delivery of email for whatever reason and your lead is gone!
If it’s a data protection issue, then you have sensitive data fields if you want to restrict who can view what.

Also nothing worse than a shared inbox means multiple emails sent to the end user by the client rather than embrace the forms backoffice for a workflow of submitted → approved if one of the team already dealt with it.

Also ideally if this is high traffic and you want to see the end user get a success message as fast as possible the ideal is to take the actual email sending out of process and add to a message queue.

Hi,

Thanks both I have a look and see what solution I can come up with.