Keeping record creation to one only

Topics: General, Writing modules
Mar 18, 2013 at 4:35 PM
Edited Mar 18, 2013 at 4:35 PM
I was looking around for examples of how to keep the creation of a record to just one (i.e., the user can create one record, then later he can only edit it but not create a second record) I wanted some guidance as to whether this was an acceptable method:

var myRecord = _myService.GetRecord(myRecord.Id, myType) ?? _myService.CreateRecord(myRecord.Id, myType);

this is done through a private field on the controller and is called from within an action. Basically, either retrieve the record if it exists, or create it if it does not.

Again, is this okay to do, or is there a better example out there.

Thanks.
Developer
Mar 19, 2013 at 11:57 AM
Don't you want to have site settings by chance?
Mar 19, 2013 at 1:40 PM
Sorry, I'm not following what you mean by that ContentSharing part.

Unless, of course, you are suggesting that site settings would be the place to have the logic to only allow creation of 1 record. If that's the case, do you know of any examples of that which you could point me to?

Thanks.
Developer
Mar 19, 2013 at 5:33 PM
I thought that the reason you want to have a single record to exist indicates that what you want to store is some site-wide configuration. If you tell more about what you want to store I can help better.
Mar 19, 2013 at 7:02 PM
Thanks Piedone. I have my site set up to take some information from a client. However, I don't want the client to enter more than one instance of that information. What I have is a simple form that saves low-level data in a record. Once the client fills in the form, I just want to make sure they cannot create another one. However, if they go into the form they would get a display of what they previously entered (with a button that links to edit that form).

Basically, I just wanted guidance on whether or not this was acceptable to do var myRecord = GetRecord ?? CreateRecord as a method of enforcing the create one only concept for a form.
Developer
Mar 19, 2013 at 11:14 PM
Is that client a registered user? If yes, can multiple users be clients, i.e. can you have multiple users that should be able to fill out the form but only once? Because if yes then you could just store these submissions like ordinary content items with the exception that before you show them the editor (from your custom controller) you check for the existence of their corresponding item (by e.g. storing the user id in a record property).
Mar 20, 2013 at 1:52 AM
Ahh, ok, that makes sense. Yes, they each have to be registered before they can fill out the form and there can be multiple users (clients) that fill out the form. So it would be one instance of the form per client (and obviously only a particular user can create/edit their own form).

Thanks for the guidance.