Lookup picker with current user name

I have an app that has a “Requested By” field. The lookup container works great for showing all users using this code

    <collection id="requestedBySelectorDc"  class="com.company.sample.entity.ExtUser" view="_minimal">
        <loader id="requestedBySelectorDl">
            <query>
                <![CDATA[select e from sec$User e]]>
            </query>
        </loader>
    </collection>

image

And here is the container code in the form.

<lookupField id="requtestedBy" caption="Requested By" dataContainer="safeRequestDc" property="requestSubmitedBy" optionsContainer="requestedBySelectorDc" />

I would like to limit the lookup field to only show the currently logged in user as an option. .

I tried this but it didn’t work for me -

 <collection id="requestedBySelectorDc"  class="com.company.sample.entity.ExtUser" view="_minimal">
            <loader id="requestedBySelectorDl">
                <query>
                    <![CDATA[select e from sec$User e where e.email = :userLogin]]>
                </query>
            </loader>
        </collection>

Any suggestions would be helpful.

Hi @Stephen,

I think you almost finished what you were trying to do. You forgot to set the parameter in the dataloader object. Take a look at this sample.

I don’t know your application usage scenario, but it seems a little strange to use a lookup field with only one possible value. If this “Requested By” should always be set to the logged user when a new item is created, I would suggest you to change this field to a readonly TextField and set this value when a new item is created.

Screen:

<textField id=“requtestedBy” caption=“Requested By” property=“requestSubmitedBy” editable=“false” />

Controller:

@Inject
private UserSession userSession;

@Subscribe
public void onInitEntity(InitEntityEvent<SampleEntity> event) {
    event.getEntity().setRequestedBy(userSession.getCurrentOrSubstitutedUser());
}

Regards.

1 Like

I appreciate the suggestion for this screen. I tried the text field option seeing as for this screen I only need the currently logged in user. I know I must be over complicating it.

The controller gives me the following error
image

The XML file
<textField id="RequestedBy" caption="Requested By" property="requestSubmitedBy" editable="false" />

Here is my Entity setup
image

When the drop down shows all names this is how the data is entered into the db.
image

image

Hi, @Stephen!

By the text of the error, you don’t have a method called setRequestedBy(User) for the class SafeRequest. Also, according to the screenshot that you attached from the CUBA entity designer, the name of the field is the requestSubmitedBy, which means the method should consist of this name - setRequestSubmitedBy(User).

Sample code that you should get:

    @Inject
    private UserSession userSession;

    @Subscribe
    public void onInitEntity(InitEntityEvent<SafeRequest> event) {
        event.getEntity().setRequestSubmitedBy((ExtUser) userSession.getCurrentOrSubstitutedUser());
    }

Did you change the implementation of the SafeRequest class? If you changed, please send the code of this class.

Regards,
Gleb

1 Like

THANK YOU - The issue was partly with my onInitEntity code like you suggested @durygin. The issue also turned out to be my view. For the Extended Users Requested By i had the View in the viewset to user.browse. I changed it to Minimal and everything loaded as @peterson.br

Thank you both very much. I really appreciate your help.