@PostConstruct
protected void init() {
// Set the 'systemUser' attribute to a currently logged in user
setSystemUser(AppBeans.get(UserSessionSource.class).getUserSession().getUser());
}
As I understand, when some new entity is created you want to set the id of the currently logged user?
You can simply get the current user by using UserSession.
In the editor screen, override the initNewItem method and change initial values of a created entity instance.
Code example:
I ended up doing something similar at the entity level.
@PostConstruct
protected void init() {
// Set the 'systemUser' attribute to a currently logged in user
setSystemUser((ExtUser) AppBeans.get(UserSessionSource.class).getUserSession().getUser());
}
I actually extended the user (ExtUser) to add an additional association attribute. What I would like to do is also record this association attribute to the item being created. For sake of discussion, let’s imagine that attribute is Department. Since it is likely that a user may change departments, we can’t depend on the current department of a user, rather we need to record the depart a user was in at the time an item was created.
Unfortunately, the additional attribute (department) is not in the UserSession.
I think I need to use the data manager to grab this and am trying to find some syntax that will work.
Could you clarify does ExtUser contain required field? If no, you can use DataManager with a query or create some service that will return Organization by the user from UserSession.