Hello all,


I am making a fundraising page, and I'm a little confused on how to create a "variable" that exists in the page but keeps its value once the user changes it, leaves the page and another user comes in to see the changed value. 


I am unsure if I use a static entity? Session variable? 


Example: 

I want these two to change once I make the change, but stay changed. 


Thank you!

Hi Vinicius,

A Static Entity is, like the name says, static. You cannot change it. A Session Variable is, like the name implies, bound to the session and therefore user specific. I'm not really sure why you are thinking of Static Entities or Session Variables, why not just a plain ol' database Entity?

Hey,

You wouldn't use a static entity as these are for unchanging values. You could use a session variable, but it depends on how exactly you end up manipulating those values. Do they change very often? Are they specific to a user?

If one user changes the values for everyone, it makes it harder to propagate into a session variable: they're typically used to store user preferences, or user specific information. If its common data, you should store in the database inside an Entity.

If those values are specific to a User, I'd store them in a session variable.

The most straightforward solution is to create an entity to hold the value.  If that's not preferable, you can also create a site property and then programatically set the site property value.  The downside is that anyone who has access to the site properties in ServiceCenter can log in and change it.

I'll use an entity then. Thank you all!

@Kevin: using site properties for changing data is discouraged and bad practice! It has associated performance issues since it causes a cache flush etc.

Kilian Hekhuis wrote:

@Kevin: using site properties for changing data is discouraged and bad practice! It has associated performance issues since it causes a cache flush etc.

Agreed that it's not ideal, but if you start with the assumption that there is some reason an entity can't be used (maybe they're out of AOs??) it would be a viable alternative.


Ok I am using an entity, I want it to only have 1 entry that I can modify throughout the pages, screens etc. When I create the entity, it is originally empty, so I have to add an entry. To do that, I tried using the Create function from the entity function list, but it asks for a Source. What do I put for Source? When I used a variable, it asked for an ID which I don't have since its empty...

I'm a little confused.

Solution

Hi Vinicius,

If you have an Entity that needs only a single record (as is the case with these kind of Entities that hold configuration data), this is the usual way to go:

  1. Create a Timer that runs on Publish (set the Schedule to "When Published");
  2. In the Timer Action, query the Entity, and End if it's the result is not empty;
  3. If the result is empty, create a single record with a default value;
  4. For updating the single record, read it from the database (to fetch its Id), update whatever needs updating, and write it to the database.

Now with regard to your errors:

  • The Create, Update and CreateOrUpdate Entity Actions all require you to specify the data to write to the database, in the form of an Entity Record. You typically define a Local Variable, Assign values to it, and pass it as a Parameter to the Entity Action. Make sure the Local Variable is of the same type as the Parameter.
  • If, in general, you pass a Variable to a Parameter that has a different Entity or Structure type than the Parameter, OutSystems asks you to map the Attributes, as you have above. In your case, this means the Basetot Variable has the wrong type (not the Entity type of your Entity).
  • Also in general (but not in this case, as your Basetot Variable has the wrong type), when using a Create Entity Action, just specify NullIdentifier() for the Id.
  • That said, you probably don't need a Local Variable: with OutSystems Platform 11, if you hove with the mouse over the "Source" Attribute, a blue "+" appears in front of it. If you click it, you can specify all the Attributes without needing a Local Variable.
Solution

Hello Killian!

Thank you for pointing out the source +, it got rid of several of my variables ;). 


I did everything you told me, and it worked like a charm! Thank you for being so precise, considerate and detailed in your explanation, I was able to update the values and they stayed there. 


Thank you!

Hi Vinicius,

Glad I could be of help. Happy coding!