Regarding form data assignment and manipulation

I have a form with SourceRecord set to GetPersonById.List.Current with several fields such as person's country id, full name and email address.

Full Name field variable is set to PersonForm.Record.Person.FullName

Email field variable is set to PersonForm.Record.Person.EmailAddress

Country is a combo box with 

  • Variable set to PersonForm.Record.Person.CountryId 
  • Source Identifier Attribute set to Country.Id 
  • Source Attribute set to Country.Name 
  • Source Record List set to GetCountries.List

On page load, if the person's country is not selected, i want to assign the ID value of 100 (which is the country of Canada) as the default value for the person.

I tried two approach, the first one works but the second one always default to the first item of the list.

(1) In preparation, I set GetPersonById.List.Current.Person.CountryId = IntegerToIdentifier(100).

(2) In preparation, I set PersonForm.Record.Person.CountryId = IntegerToIdentifier(100).

Why does the first one work but the second one don't?

Hi EnthusiasticNewbie,


By setting the Source Record to GetPersonById.List.Current, you are saying that the data to be put into the form on load should come from (i.e. be copied from) that aggregates results.  

I think this happens by copying the values over at some point after the Preparation has run, and before it is sent back to the browser for rendering, so anything you might have put into the form's record fields in the preparation is overwritten.

If you debug with a breakpoint in the preparation, opening for example an existing person's details, you will see that after the GetPersonById Aggregate has executed, that the record list of the aggregate is already filled with the person's details, but if you at that point (before end of preparation) look at the form's record, it is still empty.




Thank you for your reply.

Let me add some additional information which would make my question clearer i hope.

The person is new (to be added), hence the GetPersonById will not return anything and the fields will be blank. I want to set Canada as the default combo box selection.

The country field is newly added but the Person entity already contains some data (without country).

But even if the person is not new, and the Get Aggregate pulled out the person information, I also want to manually change it so that the default country is set to Canada.

Solution

Yes, I get that, and if you use your method 1, that works, just put an if in front of it, to only set the value if it is empty (either on a new person or an existing one that has never gotten his country filled yet).


Method 2 doesn't because whatever is in the database (even empty value) and comes back in the output of your aggregate, overrides whatever you set during preparation in the form fields.  And if the value is empty, the dropdown will show the first one in your list.


In a non-exercise setting I would be careful to actually hardcode it to the id value, though, as with migrations to other environments (dev->test->prod) id's might possibly get reassigned.  So it's safer to add little aggregate retrieving the id of the country you want.  One of the ways is to add a boolean attribute to your country entity, to flag the default one.








Solution

Thank you Dorine

Method 2 doesn't because whatever is in the database (even empty value) and comes back in the output of your aggregate, overrides whatever you set during preparation in the form fields.  And if the value is empty, the dropdown will show the first one in your list.

This line answered my question.