We have some data that needs to be attached to each record created in Contour / Forms. Currently we're adding some fields manually and let a workflow add the data. However - that requires editors to remember to add those (hidden) fields. And also it means the form gets a bit cluttered with the hidden fields.
So one way would be to let the workflow check for the fields, and if they are not present - add them programmatically.
Another way would be to add the custom data to the record, "outside" the fields, but I don't think that is possible?
Jonas, Here's a snippet that could provide you with some direction:
var recordStorage = new RecordStorage()
var formStorage = new FormStorage();
var form = formStorage.GetForm(new Guid("f07e9028-a14e-4f74-99ee-c0a8fbfa4969"));
var record = new Record(); //for new record
//or
//var records = recordStorage.GetAllRecords(form);
//foreach (var record in records)
//{
//find your record here;
//}
foreach (var f in form.AllFields)
{
if (f.Alias == "firstName" && model.CustomerFirstName != null)
{
var key = Guid.NewGuid();
var recordField = new RecordField()
{
Alias = f.Alias,
FieldId = f.Id,
Field = f,
Key = key,
Record = record.Id,
Values = new List<object>() { model.CustomerFirstName }
};
var recordFieldStorage = new RecordFieldStorage();
recordFieldStorage.InsertRecordField(recordField);
record.RecordFields.Add(key, recordField);
}
...add other variables here
}
var recordData = String.Empty;
foreach (var rf in record.RecordFields)
{
recordData += "'" + rf.Value.FieldId + "':'" + rf.Value.Values[0] + "',";
}
record.RecordData = "{" + recordData + "}";
record.Form = new Guid("f07e9028-a14e-4f74-99ee-c0a8fbfa4969");
record.IP = Request.UserHostAddress;
record.UmbracoPageId = 1114;
record.State = FormState.Approved;
recordStorage.InsertRecord(record, form);
// or recordStorage.UpdateRecord(record, form);
Thanks for posting this, Gus. It worked great for us. We had figured most of it out, except we were missing some values from the RecordField and we had no idea what to put in record.RecordData.
Speaking of, it seems insane to me that a developer using the Umbraco Forms API would have to construct a blob of JSON after having just constructed the form submission using class instances.
It's even worse that this doesn't seem to be documented anywhere.
Add fields programmatically? Attach custom data?
We have some data that needs to be attached to each record created in Contour / Forms. Currently we're adding some fields manually and let a workflow add the data. However - that requires editors to remember to add those (hidden) fields. And also it means the form gets a bit cluttered with the hidden fields.
So one way would be to let the workflow check for the fields, and if they are not present - add them programmatically.
Another way would be to add the custom data to the record, "outside" the fields, but I don't think that is possible?
Thanks
Jonas, Here's a snippet that could provide you with some direction:
Thanks for posting this, Gus. It worked great for us. We had figured most of it out, except we were missing some values from the RecordField and we had no idea what to put in record.RecordData.
Speaking of, it seems insane to me that a developer using the Umbraco Forms API would have to construct a blob of JSON after having just constructed the form submission using class instances.
It's even worse that this doesn't seem to be documented anywhere.
I've come across the same requirement today, not solved it yet, but 2 additional comments:
will generated the json block for you.
Also, if you have a workflow associated with submitting the form you can do this by calling
Off to find out why I can trigger an email with the data even though it's not being saved (yet)
t
Hi,
just came across this requirement myself and this post helped me sort it.
A couple of additional points:
You can generate the json data with:
and you can also trigger an associated workflow by adding a call to the submit method
Hope this helps someone
t
is working on a reply...