Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jonas Eriksson 930 posts 1825 karma points
    Jan 29, 2015 @ 13:35
    Jonas Eriksson
    0

    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

  • Gus Braz 14 posts 38 karma points
    Jul 24, 2015 @ 06:49
    Gus Braz
    3

    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);
    
  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Dec 12, 2015 @ 19:59
    Nicholas Westby
    2

    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.

  • Tom Madden 252 posts 454 karma points MVP 4x c-trib
    Sep 28, 2016 @ 12:20
    Tom Madden
    0

    I've come across the same requirement today, not solved it yet, but 2 additional comments:

    record.RecordData = record.GenerateRecordDataAsJson();
    

    will generated the json block for you.

    Also, if you have a workflow associated with submitting the form you can do this by calling

    recordService.Submit(record, form);
    

    Off to find out why I can trigger an email with the data even though it's not being saved (yet)

    t

  • Tom Madden 252 posts 454 karma points MVP 4x c-trib
    Sep 28, 2016 @ 13:18
    Tom Madden
    0

    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:

                    record.RecordData = record.GenerateRecordDataAsJson();
    

    and you can also trigger an associated workflow by adding a call to the submit method

    recordService.Submit(record, form);
    

    Hope this helps someone

    t

Please Sign in or register to post replies

Write your reply to:

Draft