Hint: Save a backend call when creating new records

Maybe you are aware, maybe you’re not…
Let’s consider what Outsystems teach us to do to scaffold new entities: You just need to drag a new database entity into the Interface tab and voilá, Service Studio will scaffold a list page and a detail page for your entity. It only takes 5 seconds to have a List and CRUD functionality working for an entity.
After removing the audit fields that we don’t want to be visible in the screen, we ended with the following logic for our ContactDetail:

But let’s see it closely…
When Service Studio generated GetContactById aggregate the following “magic” occurred:

- MaxRecords was automatically set to 50, where we know that Id is unique and we’re filtering by Id so it should be set to 1;
- Fetch is set to “At start”, so this will always trigger a backend call even if we’re creating a new entity and obviously that in this scenario no call to the backend is required;
To fix the first problem we simply change 50 to 1, but what about the second one?
Before tackling the problem, let’s see it in action:

As you can see even when we’re creating a new record, there’s a call to the backend requesting for a contact with id = nullidentifier(), but this obviously will return nothing, so it shouldn’t be called in first place.
How can we avoid this call then?
Step 1: Set aggregate fetch parameter to “Only on demand”

Step 2: Create a new client action that refreshes the aggregate:

Step 3: Declare OnInitialize and test if ContactId is not null, then use Javascript to call the Client Action declared in Step 2.

After applying this “recipe” you will be saving a backend call whenever you’re creating a new entity:

As you can see, we’re no longer calling the GetContactById when creating a new Contact record but it will continue to be called when retrieving an existing entity.
Obviously that this is not a big problem, but it’s good to be aware of.