Setting the default value of a User Identifier field in a Reactive app
Application Type
Reactive

I am new to OutSystems and I'm sure that there will be a simple answer to this question but I can't work it out.

I have an Entity that has an attribute called EmpName which is the User Identifier data type.  I cannot set a default value for this attribute on the entity.

I then have a screen which contains a form.  The EmpName attribute is bound to a Dropdown and I would like the default value to be set to the current user's name.  I've found that if I set the Empty Text property to be GetUserID() it displays the ID of the User Identifier associated with the current user.  I would like it to show the name of the current user though (so that it is the same as the values that are seen in the drop down's list).

What is the best practice way of achieving setting the default value of a User Identifier attribute to be the name of the current user?

mvp_badge
MVP
Solution

Hi Julie Peck,


The best way then is to use the OnAfterFetch event of your query (see image below). This event, as the name mentions, allows you to run logic after the data is fetched from the database. There you can place your logic (assign the GetUserId() to your logic if no value is found on the database, otherwise assign the value of the database) to achieve your use case:



Kind Regards,
João

Hi João, 

Perfect, thank you!  I had just come up with a solution but it was much more convoluted than your solution so I've learnt a lot which I really appreciate :-)

Julie

mvp_badge
MVP
Solution

Hi Julie Peck,


You can add the OnReady event to your screen:


Do an assign setting the value of the variable bound to the dropdown (in my case UserId) to GetUserId():



And that's it. If your current user is in the list of users you use to populate the dropdown, it will appear his / her name.


Kind Regards,
João

Thanks João.

I have now got this working by creating a local variable called UserID which I have bound to the dropdown.  I then added an assign on the SaveDetail action that assigns the local variable to the database variable so that it gets saved back to the database.  

Is that correct way of achieving it or can I use a database variable on the OnReady event?

There is one big downside though - if I then open an existing record that has the EmpName attribute set to a different user, the dropdown displays the current user's name rather than the name saved on the record.  What is the best way to workaround this issue?

mvp_badge
MVP
Solution

Hi Julie Peck,


The best way then is to use the OnAfterFetch event of your query (see image below). This event, as the name mentions, allows you to run logic after the data is fetched from the database. There you can place your logic (assign the GetUserId() to your logic if no value is found on the database, otherwise assign the value of the database) to achieve your use case:



Kind Regards,
João

Hi João, 

Perfect, thank you!  I had just come up with a solution but it was much more convoluted than your solution so I've learnt a lot which I really appreciate :-)

Julie

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.