Updating form fields as they are being saved

Jessica Knaak asked on August 2, 2016 21:17

I have a form page that includes a query string. When this form is submitted, I would like to amend the comments column with the query string information. I have been able to build a string to add the query string so it is readable and call the correct record in the form data to update. I have been using examples from the documentation and this is what I have so far in my viewBiz_OnAfterSave function:

var currentUrl = RequestContext.CurrentURL.ToString(); if (currentUrl.IndexOf("xxx") > -1) { // Gets the class name of the 'xxx' form DataClassInfo formClass = DataClassInfoProvider.GetDataClassInfo(bi.FormClassID); string className = formClass.ClassName;

        // Loads the form's data
        ObjectQuery<BizFormItem> data = BizFormItemProvider.GetItems(className).TopN(1).OrderByDescending("xxx");

        // Checks whether the form contains any records
        if (!DataHelper.DataSourceIsEmpty(data))
        {
            var dataString = data.ToString();
            // Loops through the form's data records
            foreach (BizFormItem item in data)
            {
                xxx.LogEvent("I", "form data", "adding to comments", "COMMENTS: " + item.GetStringValue("Comments", ""));

                string commentsFieldValue = item.GetStringValue("Comments", "");

                string newComments = commentsFieldValue + InsertQueryString();

                item.SetValue("Comments", newComments);

                xxx.LogEvent("I", "logging", "comments", "ABout to update comments to : " + newComments);

                item.SubmitChanges(false);
            }
        }
    }

I added the xxx because I am not able to show some information. The logevents work correctly. However, the page never does the redirect to the confirmation page (so I am left with a blank screen) and the new comments do not get written to the database. Any help would be greatly appreciated.

Thanks, Jessica

Correct Answer

Brenden Kehren answered on August 3, 2016 06:28

Any particular reason you need or want to do it after the save and not before? If you do it before you can append the field data before it is committed. Nice part about this is if you have your checks in place it will still save even if it errors out with an invalid querystring.

The disadvantage of doing it after is you have to query the database again and perform another update. So 4 calls vs. 1.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Joshua Adams answered on August 2, 2016 22:03

Did you clone the kentico bizform for this or is it a custom webpart or page?

0 votesVote for this answer Mark as a Correct answer

Jessica Knaak answered on August 2, 2016 22:04

It's a custom webpart

0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on August 2, 2016 22:36

What about storing the item id from the form into the querystring? The other option would be to just use the kentico form webpart and set it up to have an alternate form which could be included on the page the the other form sends the user to.

0 votesVote for this answer Mark as a Correct answer

Jessica Knaak answered on August 3, 2016 22:18

Thanks Brenden! This was my original thought and how I would like to do it. However, I attempted to do this and it did not work. I wasn't able to find very good documentation on this however. Would you be able to point me in a direction, so I know I was doing it correctly?

0 votesVote for this answer Mark as a Correct answer

Jessica Knaak answered on August 3, 2016 23:48

Turns out that when I reset the whole file and tried to add it to the before save function. It worked. I was probably just trying too many things and had something left over. Thank you for your help.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.