Enumerate Structures in Outsystem

  

Hi Everyone,

This is my first post and I am quite new to Outsystems.

First question is, may i know whether it is possible to loop through a structure. Please consider this example below.

I have a structure (Student) defined like this:

 Student
-Name = "James"
-Age = 20

I would like to enumerate the structure like in JavaScript we can use something like this

for (var key in student) {
    console.log(key + " -> " + student[key]); // Name -> James and Age -> 20
}


The example above brings me to the second question, is it possible to access the attributes in structure dynamically meaning can i do something like this to my structure, Student.[Key] or Student.{Key} ?


I hope i am clear with my explanation. 

Thanks in advance. :)


Hi Sujan, and welcome to our community.

Short answer to both your questions - no, it's not possible. At least it's not that easy.

But, could you be more specific in what are you trying to achieve, by looping through the structure? Maybe there's some other way to do it, without needing to access the structure dynamically.

If it's simply logging, maybe serializing the structure to JSON would suffice.

Hi Leonardo,

Thanks for the greeting and the reply.

Sure, i will be more specific. What i am trying to achieve is nested foreach. I will expand on the example Student.  

Now i have an Student Entity with these records, this design is only for discussion purpose. For simplicity lets assume there are only 3 courses (Arts,Science and Maths).

Id
Student
Course
1
John
Arts
2
John
Science
3
John
Maths


  1. So now i get these records using an Aggregate (GetStudentDetailsAggregate).
  2. Then depending on the course i need set a variable to true
  3. Currently, this is how i am planning to do it. But i feel something is not right. Please refer to the attached screenshot.

As you can see, i am using a switch and each case will match the course name and the it will get messy if the number of courses were to be increased . So I feel i could avoid this using a structure and loop through it. Meaning,

I define a structure (CourseStructure)with these attribute

Arts = "Arts
Science = "Science"
Maths = "Maths"

Sorry, couldn't  provide a screenshot but like in programming. If i could have a nested foreach,then i could avoid the switch but for that i need to loop through a structure and need a way to access the attributes dynamically. Example:

foreach (var record in GetStudentDetailsAggregate)
        {
            foreach (var attribute of CourseStructure )
            {
               if(record.course == attribute.value){
                  return true
               }
            }
        }


I hope i was clear with my explanation. Please let me if i could achieve the same is a different way. 


Thank you very much.

Solution

Great Sujan!


I think you can replace your structure with a list of values. You just need a list of courses, and iterating a list is much simpler, and wouldn't require a swtich node.

To simplify it even further, you can use a static entity to store the values of this list. A static entity is a table on the database, but you specify on the IDE which are the rows that it must hold. You can check this article for an example: https://success.outsystems.com/Documentation/10/Developing_an_Application/Use_Data/Data_Modeling/Create_and_Use_an_Enumerate


Depending on the logic that you have on the assign nodes of your switch, you might be able to simplify it even further. You could join the Student and Course (since they are both database tables), and perform aggregations on those.

For example, if you're trying to know how many exact science courses does the student have (only science and maths would count, but not arts or other courses), you can just do that with the aggregate. Join the Student and the Course tables, and add a calculated attribute. On the calculated attribute, you can use an If to output 1 if the course is either Math or Science, and 0 otherwise. Then, on the aggregate you just calculate the SUM of this attribute.

Solution

Thank you very much for the explanation.

I think I got it now. As per your suggestion.

  1. list of values
  2. static entity
  3. Db query

I will go with 1. list of values, I will try to have a list of structure and then will iterate through it. I believe the trick here is making values or structures as a list so it can be easily enumerated.

Thanks again for making me realize this.