0

I would like to use Umbraco Forms to not only insert data but to edit it as well. So far when I want to edit a record I am passing in the form guid and the record id via querystring and populating the correct data in the fields. So far so good.

I am then hooking in to the Umbraco.Forms.Data.Storage.RecordStorage.RecordInserting event successfully like so

void RecordStorage_RecordInserting(object sender, Umbraco.Forms.Core.RecordEventArgs e)
    {
        var ms = (Umbraco.Forms.Data.Storage.RecordStorage)sender;
        if(this record exists){
         ms.UpdateRecord(e.Record, e.Form);
        }
    }

However when I try to submit an edited record, and the ms.RecordUpdate(e.Record, e.Form) line runs I get this error

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_UFRecordDataString_UFRecordFields_Key". The conflict occurred in database "UmbracoPlay", table "dbo.UFRecordFields", column 'Key'.
The statement has been terminated.

I can't delete the old record and then insert a new record because it will re raise the same event everytime I call ms.InsertRecord

What am I missing? How can I use Umbraco Forms to edit existing data?

etoisarobot
  • 7,684
  • 15
  • 54
  • 83
  • If you're editing the data, I don't think you should need to hook into the inserting event. What version of Umbraco/Contour are you using? – Tim Sep 09 '15 at 09:26

1 Answers1

0

I couldn't see a fix for this bug- it appears as though the UpdateRecord method actually tries to insert all UFRecordField objects a second time rather than updating the existing values ( or the existing field values ) resulting in this key violation.

If you really need to work around this - as I did - then one thing that works ( but leaves you with somewhat more fragmented primary keys ) is simply to remove and then reinsert the form data:

    var ms = (Umbraco.Forms.Data.Storage.RecordStorage)sender;
    if(this record exists){
       ms.DeleteRecord(e.Record, e.Form);
       ms.InsertRecord(e.Record, e.Form);
    }

An untidy solution, but seemingly effective.

glenatron
  • 11,018
  • 13
  • 64
  • 112