StrataFrame Forum

Default values upon adding or modifying records...

http://forum.strataframe.net/Topic964.aspx

By StarkMike - 4/26/2006

When I am adding a new record how would I specify a default value for this record? Like say for instance when I add a new record I want the Created field to be set with the current date. I'm assuming this is something that would be done in the business object just not sure where or how.
By StrataFrame Team - 4/26/2006

Within the main code file, you should have a handler for [BOName]_SetDefaultValues(). This event is raised whenever a new record is added to a business object, so within it, add the line "Me.Created = DateTime.Now" and that will do the trick. You can also set any other default values you need to, and you can pull the default values in whatever means necessary. For instance, in several cases, we have the default values stored off as "soft" default values within the database (i.e.: the end user can set the default City for new customer records, and when you set the default values, you can pull the default value for City from the business object containing the default values).
By StarkMike - 4/26/2006

Great! That works for new records, but what about existing records? Let say I want to update the Modified field with the current date after a user modifies the record.
By StrataFrame Team - 4/26/2006

You'll need to go to the BOMapper and configure the business object. Inside the configuration, select "Single Event for All Fields" for the "Changed Events" type. After rebuilding the partial class for the business object, you'll have a new event called FieldValueChanged. Add a handler to this event (within the business object itself) and within the handler add the line "Me.Modified = DateTime.Now" Whenever you change a field on the business object, it will go through this event and set the "Modified" field to the current time. When you save the record, that fieldl will also be updated back to the server.
By StarkMike - 4/27/2006

I selected "Single Event for All Fields" for the "Changed Events" type and clicked Ok inside the properties for the BO. It didnt look like anything changed because I saw no indication that I needed to rebuild the BO, but I did it anyway. Then when I went into the BO the only event handler I saw that was close to FieldValueChanged was FieldPropertyChanged.



Any ideas?
By StrataFrame Team - 4/27/2006

Oops, sorry, FieldPropertyChanged is the event, not FieldValueChanged...



In the event arguments, we pass the the field that changed, so if you only want to change the Modified field when specific fields are modified, then you can test on which field changed before you set the Modified value.
By StarkMike - 4/27/2006

Great! Thanks, that works.



Question: So if the user edits the record this FieldPropertyChanged event fires and would put the current time in the Modified field. However, if the user clicks the undo button its as if it never happened. The value set in the Modified field would never make it back to the database because the save button was never clicked. Right?
By StrataFrame Team - 4/27/2006

That's correct... when they click undo, it cancels any changes within the business object, whether the fields were changed programmatically or through the form Smile