Hi, I need to be able to change a record in Umbraco Forms.
A few obstacles, the current ones has to do with the fact that the current form might differ a bit from the form at the time of the saved record.
Here is what I have currently:
public static void UpdateEntry(string formGuid, int entryId, Dictionary<string, string> entry)
{
var recordStorage = new RecordStorage();
var formStorage = new FormStorage();
var form = formStorage.GetForm(new Guid(formGuid));
var record = recordStorage.GetRecord(entryId, form);
var isChanged = false;
foreach (var property in entry) // entry is a dictionary<string,string>
{
var fieldName = property.key; // using form captions for keys in the dictionary
if (form.AllFields.Any(f => f.Caption == fieldName)) // loop all current fields in form
{
if (record.RecordFields.Any(f => f.Value.Field!=null && f.Value.Field.Caption == fieldName)) // does it exist in the record?
{
var field = record.RecordFields.SingleOrDefault(f => f.Value.Field != null && f.Value.Field.Caption == fieldName).Value;
var value = field.ValuesAsString();
if (value != property.Value) // has it changed?
{
field.Values.Clear();
field.Values.Add(property.Value);
isChanged = true;
}
}
else // we need to add it to the record
{
var formField = form.AllFields.SingleOrDefault(f => f.Caption == fieldName);
var newField = Guid.NewGuid();
var recordField = new RecordField
{
Field = formField,
Key = newField,
Record = record.Id,
Values = new List<object> { property.Value }
};
using (var recordFieldStorage = new RecordFieldStorage())
recordFieldStorage.InsertRecordField(recordField);
record.RecordFields.Add(newField, recordField);
isChanged = true;
}
}
}
}
if (isChanged)
{
foreach (var field in record.RecordFields) // fix null value fields
{
if (field.Value == null || field.Value.Values == null)
{
field.Value.Values.Clear();
field.Value.Values.Add("");
}
}
try
{
recordStorage.UpdateRecord(record, form);
}
catch (Exception)
{
// this currently causes null execption, I think because null value in some field
}
//recordStorage.UpdateRecordXml(record, form); // do we need this?
}
formStorage.Dispose();
recordStorage.Dispose();
}
}
Code to edit entry in Umbraco Forms - am I close?
Hi, I need to be able to change a record in Umbraco Forms.
A few obstacles, the current ones has to do with the fact that the current form might differ a bit from the form at the time of the saved record.
Here is what I have currently:
Late night here, and I'm stuck at where I am, so I decided to post the code as is. Will continue tomorrow.
It would be great to have some help to see if I'm on the right track, or if I'm missing something completely.
Thanks
Jonas
I am currently also looking in to a save/continue-scenario in Umbraco Forms 4.1.4. Have you reached any conclusions in how to best achieve this?
is working on a reply...