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
    Mar 17, 2015 @ 01:43
    Jonas Eriksson
    0

    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:

    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();
        }
    }

     

     

  • Jonas Eriksson 930 posts 1825 karma points
    Mar 17, 2015 @ 01:46
    Jonas Eriksson
    0

    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

  • David W. 159 posts 284 karma points c-trib
    Jun 23, 2015 @ 08:54
    David W.
    0

    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?

Please Sign in or register to post replies

Write your reply to:

Draft