Server Actions on Initializing a Screen

Server Actions on Initializing a Screen

  

Hi everybody,

I want to do a server action as soon as possible after a new view is initialized / loaded. What's the best practice for this?

Background: I'd like to present some widgets in different colors, which depend on the retrieved values of that server action.


Thanks and kind regards

Sebastian

Hi Sebastian,

Can't you call that Server Action in the Preparation of the Screen?

Cheers,

José

Hi Sebastian,

Simply call that server action inside the preparation of that webscreen.

Hi José and Pravi,

thanks for your quick responses!

I'm currently creating a mobile application. In the initializing action of the view there is a comment by OutSystems which says:
"Don’t:
– use server calls;
– perform local storage operations."

That's why I was hesitating to catch data from the database within that action... 

Regards
Sebastian

Hi Sebastion,

You can use that Server Action as a "Data Action" attached directly to the screen. That is a good practice.

Right click in the Screen and select the "Fetch Data from Other Sources":

Cheers,

José

José Costa wrote:

Hi Sebastion,

You can use that Server Action as a "Data Action" attached directly to the screen. That is a good practice.

Right click in the Screen and select the "Fetch Data from Other Sources":

Cheers,

José

And this will be executed as soon as the screen is loaded?
Thanks again & regards

Sebastian


Solution

Hi Sebastian,

No. That will be executed asynchronously. So it will arrive sometime after the screen is rendered.

Since you need that execution to change the css of widgets in the page I would advice you to get those colors from local storage (from the device) by doing a synchronization (beforehand) with the server to get that data.

Then in the OnInitialize you compute the color from data in local storage. It's not an optimal solution but at least you will be fetching data from the device and you will not see flickering of the widgets when the style arrives from the execution of the Data Action.

Cheers,

José 


Solution

Hi José, 

thank you for your Response! That was my though as well to do the Server call before and then pass the Parameters on in the segue to that view. If I decide to do the asynchronous call however and calling this in "On Initalize", how do I force the widgets to reload to Change to the new Color? Is ending the asynchronous call with going to the same view as a Destination the only Option? 


Thanks and best regards

Sebastian

Hi Sebastian,

I did not understood...

You are calling the server action on the OnInitialize? In that case the OnInitialize will only end after the server action returns.

Cheers,

José

In my suggest you can call the server action in the OnAppReady system event, store the output in a local storage, then in the screen you create the client action OnInitialize and add the aggregate who store all the information you need, the assign the information into local variables and use that variables instead of use and after fetch. You going to have more control of your information with this 

Thank you José and Omar! Great Input from you guys! 

Kind regards

Sebastian

Hi Sebastian,

Following up on José's answer, you can:

  • On your screen, create a new Fetch Data from Other Sources, where you call the server action and assign it's values outputs to the output parameters of the Fetch Data Action.
  • Whenever an Aggregate or a Data Fetch Action finishes, the platform will trigger an After Fetch event, and re-render any portion of the screen that is dependent on the outputs of that Aggregate/Data Fetch Action (which means, if your widgets are using the output of the Data Fetch action even before it finished, they will be refreshed so they can use the up-to-date values)

If the outputs of the server action don't change depending on the screen or other inputs, you can do as Omar suggests, and create an event handler for On Application Ready where you call the server action once, and store those results on Local Storage.