How to initialize a Structure in OutSysten

How to initialize a Structure in OutSysten

  

I have a need to populate a database from a List. I need to construct a temporary database Structure from the list record + external information. This structure is the parameter to the Create function of the table. This is done in a loop until all records of the List is processed.

Question: How do I reinitialize the Temporary Database Structure between each iteration in the loop. I have to do this so that data from the previous record is not mixed with the current.


Sudo code

UserProfile tmpProfile;

for ( item on itemList) {

tmpProfile = new UserProfile()   // how do I do this in OutSystems

//assign values to fields of tmpProfile

createRecord(tmpProfile)

}

Thanks

Hi Kishan,

I'm not sure I understand your question. There aren't "database structures" in Outsystems. If you need to store information in the database, you have to use an Entity.

If you use a temporary variable of your entity's type as an input parameter to the Create action, there's no need to "initialize" it. Simply assigning the new values should be enough.

Hi Kishan ,

I undestand you hava an tmpProfile as local variable in your context and its type is UserProfile.

Inside the for you need to reset the values of this variable. 

There are 2 easy way to do it:

First you can made your assign and be sure that all fields has been replaced for the values you filled in the last interation


Other way, is create a secondary local variable , ex: EmptyProfile of UserProfile type.

So you assing tmpProfile = EmptyProfile when you need to initialize it.



 



Kishan Siram wrote:

I have a need to populate a database from a List. I need to construct a temporary database Structure from the list record + external information. This structure is the parameter to the Create function of the table. This is done in a loop until all records of the List is processed.

Question: How do I reinitialize the Temporary Database Structure between each iteration in the loop. I have to do this so that data from the previous record is not mixed with the current.


Sudo code

UserProfile tmpProfile;

for ( item on itemList) {

tmpProfile = new UserProfile()   // how do I do this in OutSystems

//assign values to fields of tmpProfile

createRecord(tmpProfile)

}

Thanks



The assignment to an empty version doesn't seem to work when there's a List involved.

I'm trying to "bin" some values, and I have a structure with an index and a list of items, and then a list of those structures:

I'm having a heck of a time getting the list of items to be a new list.

I try assigning the blank:

Then I do a ListAppend to its list:


...and now the empty one has something on its list as well:

Assigning the "empty" next time through shows there is still something on this list:

Now note that the "empty" is actually a different object, because one of its values is different:

...but the assignment of the list is by copying the reference.

I'm desperate to find a way to reset that damned list to a new one!

-- Ritchie

Hi,


The safer way is to create an empty action (marked as function to use in expressions) with just a record as output.
The output of that will be initialized in each call, so its guaranteed to be new and empty.


Regards,
João Rosado


I did manage to cheat by creating a function action that returned a list of that type and assigned it to the list in the structure, and that works, because the list returned by an action is always going to be a new one.

(I did a ListClear inside to stop it complaining about the unused output parameter)

It's an okay workaround, but it would be nice if there was a more "official" way to "reset"/recreate a structure variable next time through a loop :)

-- Ritchie

Haha, I didn't see your post, as I was off trying things; I guess I had a similar idea :)

Now mind you, if you return a whole blank record, then it's cloning the whole record and you're technically "wasting" the non-list part of the record, but if you return just a list, you don't waste anything ;)

Still, the "OutSystems way" would more likely be to return a blank record (or to otherwise use a function action as a "constructor") :)

-- Ritchie


I would recommend you to avoid over optimizations like those.

It not only makes your application harder to understand/maintain (just having to explain new developers why actions that should return records return lists is an overhead) but takes away the capability of the platform from doing optimizations by itself (even if that case is not optimized now it could be in the future without you having to change anything).


Note: that said, I'm pretty sure having a list in that case introduces more overhead than just the "wasted" non-list attributes in the other scenario.


Regards,

João Rosado