Selecting a query based on a variable

Selecting a query based on a variable

So, I'm looking to create a flexible webblock where I pass in an input variable called ActivityView (text string).  I want to use this variable to determine which database query is executed and returned to be bound to a TableRecords.

Now, using a switch in the preparation, I can determine which route the runtime follows and can input all the different queries.  However, they must have different names, although I want to access it through the same name, ie bound to the TableRecords.

The query contains around 8-10 output structures but are broadly similar - looking at data using different filters.

Can anyone suggest an approach that might allow this?
Hi Steve, 
you just have to assign to a local variable type record list and then make the TableRecords source this record list.

Best regards
Great, thanks Miguel, I wasn't aware I could create a record list variable with multiple structures - you learn something new every day.
Just for reference, this is how you use a Screen Action to fetch data and display it in your screens:
  • Create a Screen Action;
  • Add input parameters (just like you create parameters in C# or Java methods);
  • Impleement the logic to fetch data
  • Add output parameters (instead of returning like in C# or Java you usually do, information gets in and out via input and output parameters)
  • Assign the output parameters with the data you've fetched
  • In the Preparation of your screen, invoke the User Action
  • In the Table Records (or List, Show, Edit Records), set its 'Source Record' to the User Action output.
Check this reply to a similar question. It contains some awesome screenshots.

The Pros

Basically all the benefits of encapsulation: you get to write the logic once and reuse it everyone.
You don't have to repeat yourself, and if you introduce a bug, it will be easier to find and fix.

The Cons

The OutSystems Platform does some awesome performance improvements. In particular, for Simple Queries the platform will only fetch the rows that you use on your logic or screens. Also, if you only display 50 records on your screen, the platform will only fetch 51 records, even if the actual query would return 2M records.

But... If you encapsulate a Simple Query inside an action, the Platform cannot easily infer where the query is being used, and is not able to make some performance improvements under the hood.
Hi Steve,

One problem you may face using an User Action as your datasource is that it cannot do query refresh when you have filters, pagination and sorting.
You may also want putting all your SELECT statements inside an Advanced Query, controlled by nested IF statements something like below:
IF (@table = 1)
    SELECT field1, field2 FROM entity1
    IF (@table = 2)
        SELECT field1, field2 FROM entity2
    ELSE <and so on..>
You can just create an ActivityView output structure with all the common fields and ensure the fields in all your SELECT statements matches your output structure.