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, ...
Answered

Look up for Active Workers list with range applied to current legal entity only.

(1) ShareShare
ReportReport
Posted on by 204
Hi,
I have a "worker" custom field in my table, for which I have given relation with HCMworker's personnel number in my table. 
And I have added lookupreference method in form datasource field as following: 

But the issue is, it is showing all legal entity values for workers. 
Can you please suggest how to filter active workers for particular legal entity only?
 public Common lookupReference(FormReferenceControl _formReferenceControl)
            {
                Common ret;
                FormReferenceGroupControl callingReferenceGroupControl;
                FormRun lookupForm;
                Args args;
                HcmWorkerLookup hcmWorkerLookup;

                if (!_formReferenceControl)
                {
                    throw(error(strFmt("@SYS137393", Error::wrongUseOfFunction(funcName()))));
                }

                hcmWorkerLookup = hcmWorkerLookup::newOnlyActiveWorkersWithinCompany();// There are many methods in this, I've tried almost all

                args = new Args(formStr(HcmWorkerLookup));

                args.parmObject(hcmWorkerLookup);
                args.record(My data source table); // I have given my custom datasource table here
                args.caller(_formReferenceControl);

                lookupForm = classfactory.formRunClass(args);
                lookupForm.init();

                if (_formReferenceControl.handle() == classNum(FormReferenceGroupControl))
                {
                    callingReferenceGroupControl = _formReferenceControl;
                    callingReferenceGroupControl.performFormLookup(lookupForm);
                    ret = lookupForm.selectRecordModeSelectedRecord();
                }

                return ret;
            }
 
I have the same question (0)
  • Adis Profile Picture
    6,597 Super User 2026 Season 1 on at
    Hi,
     
    I am not a developer and therefore cant give you an exact answer. I can give you a hint in case you are using F&O, not sure which product you are referring to.
     
    Nevertheless, the following form shows workers\employees, restricted to current legal entity, which are still active. If you check the code of this form and filter, you should have a solution I guess.
     
    Hope it helps, Adis
     
  • Suggested answer
    GirishS Profile Picture
    27,843 Moderator on at
    Hi RSX,
     
    If you want to filter the worker of the particular legal entity, you need to use the HcmEmployment table. This table has a relation with HcmWorker table, and it have field called LegalEntity which will be used to identify the legal entity where the worked is working.
     
    Thanks,
    Girish S.
  • Suggested answer
    Waed Ayyad Profile Picture
    9,089 Super User 2026 Season 1 on at
    Hi,

    Since you were adding the relation with Personnel number in Homeworker table, so you should override lookup method not Lookup Reference method, and you can't use hcmWorkerLookup directly.
     
    public void lookup()
    {
     QueryBuildRange         validFromRange;
     QueryBuildRange         validToRange;
     utcdatetime             now = DateTimeUtil::utcNow();
     Query query = new Query();
     QueryBuildDataSource queryBuildDataSource;
    QueryBuildRange queryBuildRange;
    SysTableLookup sysTableLookup;
    
    QueryBuildDataSource queryBuildDataSource1;
    RecId _LegalEntity;
    ;
    
    //Create an instance of SysTableLookup with the form control passed in
    sysTableLookup = SysTableLookup::newParameters(tablenum(HcmWorker), this);
    
    //Add the fields to be shown in the lookup form
    sysTableLookup.addLookupfield(fieldnum(HcmWorker, PersonnelNumber), false);
    sysTableLookup.addLookupMethod(‘Name’);
    
    //create the query datasource
    queryBuildDataSource = query.addDataSource(tablenum(HcmWorker));
    
    queryBuildDataSource1 = queryBuildDataSource.addDataSource(tableNum(HcmEmployment));
    queryBuildDataSource1.joinMode(JoinMode::ExistsJoin);
    queryBuildDataSource1.relations(true);
    queryBuildDataSource1.addRange(fieldNum(HcmEmployment, LegalEntity))
    .value(SysQuery::value(CompanyInfo::findDataArea(curExt()).recId));
     validFromRange          = queryBuildDataSource1.findRange(fieldNum(HcmEmployment, ValidFrom));
     validToRange            = queryBuildDataSource1.findRange(fieldNum(HcmEmployment, ValidTo));
     query.validTimeStateDateTimeRange(now, now);
     validFromRange.value(SysQuery::valueUnlimited());
     validToRange.value(SysQuery::valueUnlimited());
    
    //add the query to the lookup form
    sysTableLookup.parmQuery(query);
    
    // Perform lookup
    sysTableLookup.performFormLookup();
    
    }
    Thanks,
    Waed Ayyad
    If this helped, please mark it as "Verified" for others facing the same issue
     
  • Waed Ayyad Profile Picture
    9,089 Super User 2026 Season 1 on at
    Hi,

    Is your issue resolved?  If yes, mark the answers that helped you as verified.

    Thanks
    Waed Ayyad
  • Suggested answer
    CU27061328-0 Profile Picture
    on at
    Hi, 

    This might help you, Employment history has all the information of the legal entity, put in a validation here
  • RSX Profile Picture
    204 on at
    Hi Waed, 
    Thank you for the suggestion.  I'm trying to verify the answer, but the page just keeps on loading and it is not verifying the answer. 

    I tried the look up updated and got "object reference error", when I debugged, it was showing the error at this line: 
    validFromRange.value(SysQuery::valueUnlimited());

    Hence, I removed those lines and updated the code as following and it worked fine. 
    public void lookup()
    {
     QueryBuildRange         validFromRange;
     QueryBuildRange         validToRange;
     utcdatetime             now = DateTimeUtil::utcNow();
     Query query = new Query();
     QueryBuildDataSource queryBuildDataSource;
    QueryBuildRange queryBuildRange;
    SysTableLookup sysTableLookup;
    
    QueryBuildDataSource queryBuildDataSource1;
    RecId _LegalEntity;
    ;
    
    //Create an instance of SysTableLookup with the form control passed in
    sysTableLookup = SysTableLookup::newParameters(tablenum(HcmWorker), this);
    
    //Add the fields to be shown in the lookup form
    sysTableLookup.addLookupfield(fieldnum(HcmWorker, PersonnelNumber), false);
    sysTableLookup.addLookupMethod(‘Name’);
    
    //create the query datasource
    queryBuildDataSource = query.addDataSource(tablenum(HcmWorker));
    
    queryBuildDataSource1 = queryBuildDataSource.addDataSource(tableNum(HcmEmployment));
    queryBuildDataSource1.joinMode(JoinMode::ExistsJoin);
    queryBuildDataSource1.relations(true);
    queryBuildDataSource1.addRange(fieldNum(HcmEmployment, LegalEntity))
    .value(SysQuery::value(CompanyInfo::findDataArea(curExt()).recId));
     
    //add the query to the lookup form
    sysTableLookup.parmQuery(query);
    
    // Perform lookup
    sysTableLookup.performFormLookup();
    
    }

     
  • Verified answer
    Waed Ayyad Profile Picture
    9,089 Super User 2026 Season 1 on at
    Hi,
     
    I'm glad your issue is resolved, try again and see if you can verify my answer, but I have one note why you remove this line?
    query.validTimeStateDateTimeRange(now, now); As now your code is correct, but you are returning all workers who have employments regardless of if they have active or expired employments.
     
    Thanks,
    Waed Ayyad
    If this helped, please mark it as "Verified" for others facing the same issue
     
     
     

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!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

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

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 616

#2
André Arnaud de Calavon Profile Picture

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

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 331 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans