how to insert value in custom table form

Marcel Guldemond asked on April 24, 2017 21:34

Is there a way to add a hidden field with a value from a macro to a custom table's alternative form?

We're trying to associate custom data with website users, so we have custom tables for their data. The custom table has a column to store the user's id, and we'd like to set the value when the form is submitted.

Do we have to write a custom form event handler or a custom webpart for this to work?

Correct Answer

Brenden Kehren answered on April 24, 2017 22:57

Assuming your user has to log in to submit that form, you can simply set a default value with a macro on a field not shown on the form. Add a field called UserID and in the default value click the arrow and enter {%CurrentUser.UserID%} and be sure to uncheck the box to display field on editing form.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Marcel Guldemond answered on April 25, 2017 15:31

Thanks Brenden, I've added a field for the UserID (called it EmployerUserID) and put the macro in. When saving the changes to the field, Kentico automatically changes the closing '%}' to '#%}', and then when I save the form, it inserts this into the custom table record: {% CurrentUser.UserID |(user)<username>|(hash)<hashcode>%}

Am I missing something about adding macros?

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on April 25, 2017 15:39

Make the macro unsigned by using {%CurrentUser.UserID@%} The macro doesn't need to be signed.

0 votesVote for this answer Mark as a Correct answer

Marcel Guldemond answered on April 25, 2017 16:06 (last edited on April 25, 2017 16:38)

Thanks again Brenden, that does get rid of the hash code stored in the record, but the form is just saving the macro, not actually running it, so the value in the database is: {%CurrentUser.UserID@%}

0 votesVote for this answer Mark as a Correct answer

Marcel Guldemond answered on April 25, 2017 16:44

One other question I have, is if a site administrator edits the data at a later point, will the user id stored in the custom table get overwritten by the admin's UserID, or will it be left alone because the macro only applies when there's no value?

thanks,

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on April 25, 2017 16:49

The default value is only entered when the record is set or inserted so if you're reviewing other records, they will not automatically update, you have to enter new records after you update that macro. Also, be sure to use the arrow next to the words "Default value" and not just place the macro in the textbox.

1 votesVote for this answer Mark as a Correct answer

Marcel Guldemond answered on April 25, 2017 17:15

I just found that you have to use the little black arrow to open up a macro editor to get the macro to run:

https://devnet.kentico.com/questions/ascx-macros-in-form-field-default-values

0 votesVote for this answer Mark as a Correct answer

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