Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested answer

lookup based on condition doesn't work directly and the UI for both lookups is different

(0) ShareShare
ReportReport
Posted on by 1,497

Hi,

I have this lookup code:

    /// 
    ///
    /// 
    /// 
    /// 
    [FormControlEventHandler(formControlStr(CaseDetailCreate, AAWorker), FormControlEventType::Lookup)]
    public static void AAWorker_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        FormStringControl customerContract = sender.formRun().design().controlName(formControlStr(CaseDetailCreate, AACustomerContract)) as FormStringControl;

        if(customerContract.text() != '')
        {
            SysLookupMultiSelectCtrl multiSelectCtrl;
            Query query = new Query();
            QueryBuildDataSource qbds = query.addDataSource(tableNum(ResourceView));
            QueryBuildDataSource qbds1 = qbds.addDataSource(tableNum(PROJVALEMPLPROJSETUP));
            Qbds1.joinMode(JoinMode::InnerJoin);
            Qbds1.addLink(fieldNum(ResourceView, RecId), fieldNum(PROJVALEMPLPROJSETUP, Resource));
            QueryBuildDataSource qbds2 = qbds1.addDataSource(tableNum(AATable1));
            qbds2.joinMode(JoinMode::InnerJoin);
            qbds2.addLink(fieldNum(PROJVALEMPLPROJSETUP, ProjId), fieldNum(AATable1, OrderNumber));
            qbds2.addRange(fieldNum(AATable1, OrderNumber)).value(queryValue(customerContract.text()));

            qbds.addSelectionField(FieldNum(ResourceView, ResourceId));
            qbds.addSelectionField(FieldNum(ResourceView, Name));
            multiSelectCtrl = SysLookupMultiSelectCtrl::constructWithQuery(sender.formRun(), sender, query);
        }
        else
        {
            SysLookupMultiSelectCtrl multiSelectCtrl;
            Query query = new Query();
            QueryBuildDataSource qbds = query.addDataSource(tableNum(HcmWorkerDetailsView));
            qbds.addSelectionField(FieldNum(HcmWorkerDetailsView, PersonnelNumber));
            qbds.addSelectionField(FieldNum(HcmWorkerDetailsView, Name));
            multiSelectCtrl = SysLookupMultiSelectCtrl::constructWithQuery(sender.formRun(), sender, query);
        }
    }


1. the lookup field is on a dialog, and as you can see the lookup depends on another form control. When the dialog is still open, if i change the value of the control to match the other lookup it doesn't work, it keeps showing the old one until i close the dialog and open it again... how to solve this?

2. The UI for both lookups is different, why? i want them both to be the same things, as you can see for the hcmworkerDetailsView i only selected 2 fields but i can still see a lot of fields
pastedimage1665949595818v1.png

however the other lookup looks good and smaller in size.. why is that?
pastedimage1665949750276v2.png

  • GirishS Profile Picture
    27,825 Moderator on at
    RE: lookup based on condition doesn't work directly and the UI for both lookups is different

    Then the only work around will be creating view with the required tables and in the view fields add only the fields you want in the lookup.

    You can use that view as a datasource in the lookup method.

    Thanks,

    Girish S.

  • .. Profile Picture
    1,497 on at
    RE: lookup based on condition doesn't work directly and the UI for both lookups is different

    Hi Girish,

    yes selection field doesn't work because this is the query it returns for two dataSources:  -- as you can see it returned the 2 selected fields i chose for the first dataSource but it returned all fields for the the other dataSource, maybe if there is a way to say addSeletcionField(null) for other dataSources then it might work

    {Query object 4fb3a510: SELECT ResourceId, Name FROM ResourceView(ResourceView_1)
    JOIN GroupId, Projid, Resource, dataAreaId, recVersion, Partition, RecId FROM ProjValEmplProjSetup(ProjValEmplProjSetup_1)
    ON ResourceView.RecId = ProjValEmplProjSetup.Resource}


    As for the new way you suggested: it's still not working

    container selectedFields = [tableNum(ResourceView),fieldnum(ResourceView,ResourceId),fieldnum(ResourceView,Name)];
    multiSelectCtrl = SysLookupMultiSelectCtrl::constructWithQuery(sender.formRun(), sender, query, false, selectedFields);


    it returned un-needed fields:

    pastedimage1666434161287v1.png



    Also would you mind looking at this question:  it's related to this one

    community.dynamics.com/.../lookup-event-handler-method-is-not-being-called-more-than-once

  • GirishS Profile Picture
    27,825 Moderator on at
    RE: lookup based on condition doesn't work directly and the UI for both lookups is different

    Edited the code. You can initialize container and add fields to that.

    Thanks,

    Girish S.

  • GirishS Profile Picture
    27,825 Moderator on at
    RE: lookup based on condition doesn't work directly and the UI for both lookups is different

    Yo already said that adding selection field doesn't work. That's why I have given an alternate suggestion.

    Try the below code.

    container selectedFields = [tableNum(ResourceView),fieldnum(ResourceView,ResourceId),fieldnum(ResourceView,Name)];
    multiSelectCtrl = SysLookupMultiSelectCtrl::constructWithQuery(sender.formRun(), 
    sender, 
    query, 
    false, 
    selectedFields);

    Thanks,

    Girish S.

  • .. Profile Picture
    1,497 on at
    RE: lookup based on condition doesn't work directly and the UI for both lookups is different

    Hi Girish,

    So you don't want me to use add selection field, right?

    can you give me the syntax exactly for multiple fields as i'm getting errors on it

     multiSelectCtrl = SysLookupMultiSelectCtrl::constructWithQuery(sender.formRun(), sender, query, false, [(tableNum(ResourceView), fieldNum(ResourceView,ResourceId)), (tableNum(ResourceView), fieldNum(ResourceView,Name)));

    here's what i did:

        [FormControlEventHandler(formControlStr(CaseDetailCreate, AAWorker), FormControlEventType::Lookup)]
        public static void AAWorker_OnLookup(FormControl sender, FormControlEventArgs e)
        {
            FormStringControl customerContract = sender.formRun().design().controlName(formControlStr(CaseDetailCreate, AACustomerContract)) as FormStringControl;
    
    
            SysLookupMultiSelectCtrl multiSelectCtrl;
            Query query = new Query();
            QueryBuildDataSource qbds = query.addDataSource(tableNum(ResourceView));
            QueryBuildDataSource qbds1 = qbds.addDataSource(tableNum(PROJVALEMPLPROJSETUP));
            qbds1.joinMode(JoinMode::InnerJoin);
            qbds1.addLink(fieldNum(ResourceView, RecId), fieldNum(PROJVALEMPLPROJSETUP, Resource));
            QueryBuildDataSource qbds2 = qbds1.addDataSource(tableNum(Table1));
            qbds2.joinMode(JoinMode::InnerJoin);
            qbds2.addLink(fieldNum(PROJVALEMPLPROJSETUP, ProjId), fieldNum(Table1, OrderNumber));
            qbds2.addRange(fieldNum(Table1, OrderNumber)).value(queryValue(customerContract.text()));
    
            //qbds.addSelectionField(FieldNum(ResourceView, ResourceId));
            //qbds.addSelectionField(FieldNum(ResourceView, Name));
            multiSelectCtrl = SysLookupMultiSelectCtrl::constructWithQuery(sender.formRun(), sender, query, false, [(tableNum(ResourceView), fieldNum(ResourceView,ResourceId)), (tableNum(ResourceView), fieldNum(ResourceView,Name))]);
            FormRun formRun = sender.formRun();
            formRun.AAMultiSelectCtrl(multiSelectCtrl);
            
    
        }

  • GirishS Profile Picture
    27,825 Moderator on at
    RE: lookup based on condition doesn't work directly and the UI for both lookups is different

    SysLookupMultiSelectCtrl::constructQuery arguments takes the container as parameter for showing fields in the lookup. Try passing the required fields to that and check.

    multiSelectCtrl = SysLookupMultiSelectCtrl::constructWithQuery(sender.formRun(), sender, query,
    [tableNum(TableName), fieldNum(TableName,FieldName)]));

    Thanks,

    Girish S.

  • .. Profile Picture
    1,497 on at
    RE: lookup based on condition doesn't work directly and the UI for both lookups is different

    Hi Girish,

    How about the extra fields appearing in the lookup?

    even though i specified only 2 fields using addSelectionField but alot are appearing as i described in previous comments

  • Suggested answer
    GirishS Profile Picture
    27,825 Moderator on at
    RE: lookup based on condition doesn't work directly and the UI for both lookups is different

    Hi IntegrationBeginner,

    If you declare the SysLookupMultiSelectCtrl multiSelectCtrl globally which means in the form declaration itself, you can access the buffer in any if the form methods.

    Thanks,

    Girish S.

  • .. Profile Picture
    1,497 on at
    RE: lookup based on condition doesn't work directly and the UI for both lookups is different

    Hi Girish,

    I want to get the values from another control and not in the lookup method.

    so in another control clicked method i want to loop through the multi selected values in another control. How to do that?

  • GirishS Profile Picture
    27,825 Moderator on at
    RE: lookup based on condition doesn't work directly and the UI for both lookups is different

    Hi IntegrationBeginner,

    You need to make use of getSelectedFieldValues method of SysLookupMultiSelectCtrl class to get the selected values line below.

    Container con;
    con = multiSelectCtrl.getSelectedFieldValues();

    It will return as container and you can loop through the values.

    Thanks,

    Girish S.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,217 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,978 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans