web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

Lookup event handler method is not being called more than once

(0) ShareShare
ReportReport
Posted on by 1,991

Hi,

I added a code to the lookup method of the customized control that I created. As you can see that there is a range based on the value of another customized control.
So if the customerContract value changed, then the lookup method should return something else.
And when debugging, i've noticed that the lookup event handler method is only called once when debugging, so currently when the customer contract value changes, the value in the lookup is still the same, i need to close the form and open it again to see the change.

I noticed that this happens for multi select lookup only, i have other lookup methods without multi select, and the method is called all the time.

    [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));
        qbds1.addSelectionField(fieldNum(ProjValEmplProjSetup, Projid));
        qbds2.addSelectionField(fieldNum(Table1, OrderNumber));
        multiSelectCtrl = SysLookupMultiSelectCtrl::constructWithQuery(sender.formRun(), sender, query);
        FormRun formRun = sender.formRun();
        formRun.AAMultiSelectCtrl(multiSelectCtrl);
        

    }

I have the same question (0)
  • Suggested answer
    GirishS Profile Picture
    27,843 Moderator on at

    Hi IntegrationBeginner,

    I think that may be due to the SysLookupMultiSelectCtrl class. I think you need to update the query with the latest values.

    To update the lookup based on the selected values of another control you can call the refreshQueury method of SysLookupMultiSelectCtr l class.

    Create new method - In this method you build the same query you added for the lookup - Return type should be query.

    Add the below method which will return a query.

    Public Query buildQuery(CustomerId _id)
    {
        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(_id));
    
        qbds.addSelectionField(FieldNum(ResourceView, ResourceId));
        qbds.addSelectionField(FieldNum(ResourceView, Name));
        qbds1.addSelectionField(fieldNum(ProjValEmplProjSetup, Projid));
        qbds2.addSelectionField(fieldNum(Table1, OrderNumber));
        
        return query;
    }

    Add the below code in the modified method of the CustomerContractLookup control to refresh the multi select lookup query.

    multiSelectCtrl.refreshQuery(this.buildQuery(customerContract.text()));
    

    Thanks,

    Girish S.

  • .. Profile Picture
    1,991 on at

    Hi Girish,

    I'm getting "variable multiSelectCtrl is not found in scope" error, when calling refresh query

    Here's what I did:

    class AACaseDetailCreate_EventHandler
    {
        
        [FormControlEventHandler(formControlStr(CaseDetailCreate, AACustomerContract), FormControlEventType::Modified)]
        public static void AACustomerContract_OnModified(FormControl sender, FormControlEventArgs e)
        {
            multiSelectCtrl.refreshQuery(sender.formRun().buildQuery(sender.valueStr()));
        }
        
        [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));
            qbds1.addSelectionField(fieldNum(ProjValEmplProjSetup, Projid));
            qbds2.addSelectionField(fieldNum(Table1, OrderNumber));
            
            multiSelectCtrl = SysLookupMultiSelectCtrl::constructWithQuery(sender.formRun(), sender, query);
            FormRun formRun = sender.formRun();
            formRun.aaMultiSelectCtrl(multiSelectCtrl);
            
    
        }
    }


    [ExtensionOf(formStr(CaseDetailCreate))]
    final class CaseDetailCreateAA_Extension
    {
        public SysLookupMultiSelectCtrl multiSelectCtrl;
        
        public SysLookupMultiSelectCtrl aaMultiSelectCtrl(SysLookupMultiSelectCtrl _multiSelectCtrl = multiSelectCtrl)
        {
            multiSelectCtrl = _multiSelectCtrl;
            return multiSelectCtrl;
        }
        
        Public Query buildQuery(Table1OrderNumber _customerContract)
        {
            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));
    
            qbds.addSelectionField(FieldNum(ResourceView, ResourceId));
            qbds.addSelectionField(FieldNum(ResourceView, Name));
            qbds1.addSelectionField(fieldNum(ProjValEmplProjSetup, Projid));
            qbds2.addSelectionField(fieldNum(Table1, OrderNumber));
        
            return query;
        }
    }

  • GirishS Profile Picture
    27,843 Moderator on at

    Since you are using event handler its difficult to get the multiselect control buffer.

    So try one thing you are passing the mutliselect buffer to the form methods on the lookup(Line number 24).

    On the modified event handler try to get the multiselect control buffer using the AAMultuSelectCtrl method and call then call the refresh query using that buffer.

    Thanks,

    Girish S.

  • .. Profile Picture
    1,991 on at

    Hi Girish,

    It worked but there is still an issue with the lookup.

    So i filled customer contract with value A, then the lookup for worker showed correct records where i selected worker 1

    then i went to the customer contract and changed it's value to B -- the worker field is now empty, now I'm expecting the lookup to not return anything for the worker, as there are now workers for that contract, and correct it returned nothing. However, the moment i clicked on the lookup for worker, it filled it with the previous value selected which was worker 1 but worker 1 wasn't in the lookup in the first place

  • GirishS Profile Picture
    27,843 Moderator on at

    You need to debug the code and find the issue.

    Thanks,

    Girish S.

  • .. Profile Picture
    1,991 on at

    Hi Girish,

    I debugged, but I can't find the issue.

    a simpler way to describe the issue:

    So i filled customer contract with value A, then the lookup for worker showed correct records where i selected worker 1.
    if i delete the worker value from the field and click on the lookup again, the lookup show the old value as selected, so if i click in the empty space without selecting it, the field gets filled with this value anyway..any idea?
    pastedimage1666463582031v1.png

  • GirishS Profile Picture
    27,843 Moderator on at

    Try to print the query in the modified method of customer control.

    sender.formRun().buildQuery(sender.valuestr()).toString();

    Also please paste the query output here.

    Thanks,

    Girish S.

  • .. Profile Picture
    1,991 on at

    Hi Girish,

    I now call the build query method in the modified method of the "worker" control and "customer contract" control

    and i had to reset control value with conNull() as well

    But i have one last issue now. When i first click the dialog and click on the lookup control of the worker it doesn't return anything... i have to click it twice to see the lookup result

    class AACaseDetailCreate_EventHandler
    {
        
        [FormControlEventHandler(formControlStr(CaseDetailCreate, AACustomerContract), FormControlEventType::Modified)]
        public static void AACustomerContract_OnModified(FormControl sender, FormControlEventArgs e)
        {
        
            if(sender.valueStr())
            {
    
                multiSelectCtrl.set(conNull());
    
            }
            multiSelectCtrl.refreshQuery(sender.formRun().buildQuery(sender.valueStr()));
        }
        
        [FormControlEventHandler(formControlStr(CaseDetailCreate, AAWorker), FormControlEventType::Modified)]
        public static void AAWorker_OnModified(FormControl sender, FormControlEventArgs e)
        {
            FormStringControl customerContract = sender.formRun().design().controlName(formControlStr(CaseDetailCreate, AACustomerContract)) as FormStringControl;
            FormRun formRun = sender.formRun();
            SysLookupMultiSelectCtrl workerMultiSelectCtrl = formRun.aaMultiSelectCtrl();
            if(sender.valueStr() == '')
            {
                if(workerMultiSelectCtrl)
                {
                    workerMultiSelectCtrl.set(conNull());
                }
            }
    
            workerMultiSelectCtrl.refreshQuery(formRun.buildQuery(customerContract.text()));
    
        }
        
    
    
        [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(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));
                qbds1.addSelectionField(fieldNum(ProjValEmplProjSetup, Projid));
                qbds2.addSelectionField(fieldNum(Table1, OrderNumber));
                multiSelectCtrl = SysLookupMultiSelectCtrl::constructWithQuery(sender.formRun(), sender, query);
                FormRun formRun = sender.formRun();
                formRun.aaMultiSelectCtrl(multiSelectCtrl);
            }
            else
            {
                SysLookupMultiSelectCtrl multiSelectCtrl;
                Query query = new Query();
                QueryBuildDataSource qbds = query.addDataSource(tableNum(HcmWorkerDetailsView));
                qbds.addSelectionField(FieldNum(HcmWorkerDetailsView, AAPersonnelNumber));
                qbds.addSelectionField(FieldNum(HcmWorkerDetailsView, Name));
                multiSelectCtrl = SysLookupMultiSelectCtrl::constructWithQuery(sender.formRun(), sender, query);
                FormRun formRun = sender.formRun();
                formRun.aaMultiSelectCtrl(multiSelectCtrl);
            }
        }
    }


    [ExtensionOf(formStr(CaseDetailCreate))]
    final class CaseDetailCreateAA_Extension
    {
        public SysLookupMultiSelectCtrl multiSelectCtrl;
        
        public SysLookupMultiSelectCtrl aaMultiSelectCtrl(SysLookupMultiSelectCtrl _multiSelectCtrl = multiSelectCtrl)
        {
            multiSelectCtrl = _multiSelectCtrl;
            return multiSelectCtrl;
        }
        
           Public Query buildQuery(Table1OrderNumber _customerContract)
        {
            Query query;
            if(_customerContract != '')
            {
                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));
    
                qbds.addSelectionField(FieldNum(ResourceView, ResourceId));
                qbds.addSelectionField(FieldNum(ResourceView, Name));
                qbds1.addSelectionField(fieldNum(ProjValEmplProjSetup, Projid));
                qbds2.addSelectionField(fieldNum(Table1, OrderNumber));
            }
            else
            {
                query = new Query();
                QueryBuildDataSource qbds = query.addDataSource(tableNum(HcmWorkerDetailsView));
                qbds.addSelectionField(FieldNum(HcmWorkerDetailsView, AAPersonnelNumber));
                qbds.addSelectionField(FieldNum(HcmWorkerDetailsView, Name));
            }
        
            return query;
        }
    }

  • GirishS Profile Picture
    27,843 Moderator on at

    What is the edt used for the worker control?

    Thanks,

    Girish S.

  • .. Profile Picture
    1,991 on at

    Hi Girish,

    HcmPersonnelNumberId

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 659

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 465 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 304 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans