Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Active worker through x++

(0) ShareShare
ReportReport
Posted on by 1,215

Hi all,

I want to get active worker in job.

I found in HcmWorkerLookup::newActiveWorkers(); and i have a 25 active worker.

Could you please give me some example how to show active worker in job info ?

Thanks   

  • Riyas ahamed F Profile Picture
    1,215 on at
    RE: Active worker through x++

    Thank you very much your time sir.

  • Verified answer
    ergun sahin Profile Picture
    8,816 Moderator on at
    RE: Active worker through x++

    I will not comment on the codes. My structure uses standard query directly. You wrote similar things, it is normal that the result does not change.

    I think we overlooked the form itself. Different filters may be working when they come from different forms. I think you need to go into debug and have a look.

    Alternatively, you can look at the difference of 26 records from other records at the table, or you can add the AOT query to a view  and see the difference of 26 records from other records.

  • Riyas ahamed F Profile Picture
    1,215 on at
    RE: Active worker through x++

    Thanks for your answer sir,

    I took it your new class and its interesting its also show in 500 records.

    Note :The same standard class replicate it only. no any changes in our custom class.

    I used two code.

    HcmWorkerLookup query Same i did. its not show any info.

             HcmWorker worker;
            DirPerson person;
            HcmPersonImage personImage;
            HcmEmployment employment;
            HcmEmployment activeEmployment;
            DirPersonUser dirPersonUser;
    
            while select worker
                join person where person.RecId == worker.Person
                outer join personImage where personImage.Person == person.RecId
                exists join  employment where employment.Worker == worker.RecId
                && !employment.ValidFrom
                && !employment.ValidTo
                && !employment.EmploymentType
                && !employment.LegalEntity
                && !employment.Worker
                notexists join activeEmployment where activeEmployment.Worker == worker.RecId
                && !activeEmployment.ValidFrom
                && !activeEmployment.ValidTo
                exists join dirPersonUser where dirPersonUser.PersonParty == worker.Person
            {
                info(strFmt("%1,%2", worker.PersonnelNumber, person.Name));
            }

    2.Query class.

    // I want aslo dirperson table to join here 
    
    QueryBuildRange         legalEntityRange;
            QueryBuildRange         validFromRange;
            QueryBuildRange         validToRange;
            QueryBuildRange         workerRange;
            QueryBuildRange         employmentTypeRange;
            QueryBuildDataSource    qbdsEmployment, qbdsworker;
            utcdatetime             now = DateTimeUtil::utcNow();
            Query query = new Query();
            QueryRun queryRun;
            HcmWorker worker;
            HcmEmployment empl;
    
               
            qbdsEmployment = query.addDataSource(tableNum(HcmEmployment));
            qbdsworker   = qbdsEmployment.addDataSource(tableNum(HcmWorker));
            qbdsworker.relations(true);
            qbdsworker.joinMode(JoinMode::InnerJoin);
            workerRange     = qbdsEmployment.addRange(fieldNum(HcmEmployment, Worker));
            workerRange.value(SysQuery::valueUnlimited());
            employmentTypeRange = qbdsEmployment.addRange(fieldNum(HcmEmployment, EmploymentType));
            employmentTypeRange.value(SysQuery::valueUnlimited());
            legalEntityRange   = qbdsEmployment.addRange(fieldNum(HcmEmployment, LegalEntity));
            legalEntityRange.value(SysQuery::valueUnlimited());
            query.validTimeStateDateTimeRange(now, now);
            validFromRange = qbdsEmployment.addRange(fieldNum(HcmEmployment, ValidFrom));
            validFromRange.value(SysQuery::valueUnlimited());
            validToRange  = qbdsEmployment.addRange(fieldNum(HcmEmployment, ValidTo));
            queryRun = new QueryRun(query);
    
            while(queryRun.next())
            {
                worker = queryRun.get(tableNum(HcmWorker));
                empl = queryRun.get(tableNum(HcmEmployment));
    
                info(strFmt("%1", worker.PersonnelNumber));
            }

    As i mentioned query class its show also 500 records.

    My goal only 26 workers sir why its not show 26.

    Please as i mentioned code what am i doing wrong? 

    Thanks 

  • Verified answer
    ergun sahin Profile Picture
    8,816 Moderator on at
    RE: Active worker through x++

    I was curious about the query and I wrote a new class from the HcmWorkerLookup's methods to see it.
    While I was at this point, I added a queryRun and made it so you can use it.
    If the number of records here does not match the one on the other side, you need to examine the custom codes (I copied the methods from the standard class)

    class EsCheckTestJob
    {
        Query                   query;
        boolean                 includeEmployees;
        boolean                 includeContractors;
        boolean                 includeActive;
        boolean                 includePending;
        boolean                 includeTerminated;
        boolean                 includeOnlyCurrentLegalEntity;
        boolean                 lockWorkerTypeFilters;
        boolean                 lockWorkerStatusFilters;
        boolean                 lockLegalEntityFilters;
        boolean                 requireUserRelation;
        boolean                 enableVirtualCompany;
        boolean                 restrictToDirects;
        Set                     workerList;
    
        public static void main(Args _args)
        {
           
    
            EsCheckTestJob checkTestJob = new EsCheckTestJob(true, true, true, false, false, false, false, true, false,false);
            checkTestJob.getQueryValues();
          
        }
    
        protected void new(
            boolean _includeEmployees,
            boolean _includeContractors,
            boolean _includeActive,
            boolean _includePending,
            boolean _includeTerminated,
            boolean _includeOnlyCurrentLegalEntity,
            boolean _lockWorkerTypeFilters,
            boolean _lockWorkerStatusFilters,
            boolean _lockLegalEntityFilters,
            boolean _requireUserRelation,
            boolean _enableVirtualCompany = false,
            boolean _restrictToDirects = false
        )
        {
            query = new Query(queryStr(HcmWorkerLookup));
            includeEmployees = _includeEmployees;
            includeContractors = _includeContractors;
            includeActive = _includeActive;
            includePending = _includePending;
            includeTerminated = _includeTerminated;
            includeOnlyCurrentLegalEntity = _includeOnlyCurrentLegalEntity;
            lockWorkerTypeFilters = _lockWorkerTypeFilters;
            lockWorkerStatusFilters = _lockWorkerStatusFilters;
            lockLegalEntityFilters = _lockLegalEntityFilters;
            requireUserRelation = _requireUserRelation;
            enableVirtualCompany = _enableVirtualCompany;
            restrictToDirects = _restrictToDirects;
        }
    
        public boolean getQueryValues()
        {
            HcmWorker               hcmWorker;
            QueryRun                queryRun;
            boolean                 ret = true;
    
    
            query = this.updateQuery();
            info(query.getSQLStatement());
            queryRun = new QueryRun(query);
            //info(strfmt("Total Records in Query %1", SysQuery::countTotal(queryRun)));
            info(strfmt("Total Records in Query %1", SysQuery::countLoops(queryRun)));
    
            While(queryRun.Next())
            {
                hcmWorker = queryRun.get(tableNum(HcmWorker));
                //-------------------------------------
                //------You can add info code here-----
                info(hcmWorker.name());
                //-------------------------------------
                ret = true;
            }
    
            return ret;
        }
    
        public Query updateQuery(HcmWorkerRecId _workerRecId = 0, Query _queryToUpdate = query)
        {
            QueryBuildRange         legalEntityRange;
            QueryBuildRange         validFromRange;
            QueryBuildRange         validToRange;
            QueryBuildRange         workerRange;
            QueryBuildRange         employmentValidFromRange;
            QueryBuildRange         employmentValidToRange;
            QueryBuildRange         employmentTypeRange;
            QueryBuildDataSource    qbdsEmployment;
            QueryBuildDataSource    qbdsDirPersonUser;
            QueryBuildDataSource    qbdsActiveEmployments;
            QueryBuildDataSource    qbdsWorker;
            utcdatetime             now = DateTimeUtil::utcNow();
    
            qbdsWorker              = _queryToUpdate.dataSourceTable(tableNum(HcmWorker));
            qbdsEmployment          = _queryToUpdate.dataSourceName('HcmEmployment');
            qbdsActiveEmployments   = _queryToUpdate.dataSourceName('ActiveEmployments');
            qbdsDirPersonUser       = _queryToUpdate.dataSourceTable(tableNum(DirPersonUser));
    
            validFromRange          = qbdsEmployment.findRange(fieldNum(HcmEmployment, ValidFrom));
            validToRange            = qbdsEmployment.findRange(fieldNum(HcmEmployment, ValidTo));
            legalEntityRange        = qbdsEmployment.findRange(fieldNum(HcmEmployment, LegalEntity));
            employmentTypeRange     = qbdsEmployment.findRange(fieldNum(HcmEmployment, EmploymentType));
            workerRange             = qbdsEmployment.findRange(fieldNum(HcmEmployment, Worker));
    
            employmentValidFromRange    = qbdsActiveEmployments.findRange(fieldNum(HcmEmployment, ValidFrom));
            employmentValidToRange      = qbdsActiveEmployments.findRange(fieldNum(HcmEmployment, ValidTo));
    
            // disable this query data source, and re-enable conditionally when only showing pending/terminated workers
            qbdsActiveEmployments.enabled(false);
    
            // disable this query data source, and re-enable conditionally only if user relation required
            qbdsDirPersonUser.enabled(false);
    
            if (_workerRecId != 0)
            {
                // Filter the HcmWorker table on the passed worker rec id
                workerRange.value(queryValue(_workerRecId));
            }
            else
            {
                // Include all workers
                workerRange.value(SysQuery::valueUnlimited());
            }
    
            if (includeOnlyCurrentLegalEntity == false
                || (_workerRecId != 0 && lockLegalEntityFilters == false))
            {
                // Include all companies
                legalEntityRange.value(SysQuery::valueUnlimited());
            }
            else
            {
                if (enableVirtualCompany && isVirtualCompany())
                {
                    // Set to the virtual company list
                    legalEntityRange.value(con2StrUnlimited(CompanyInfo::getVirtualCompanyList()));
                }
                else
                {
                    // Limit to the current company
                    legalEntityRange.value(queryValue(CompanyInfo::find().RecId));
                }
            }
    
            if (includeEmployees && includeContractors
                || (_workerRecId != 0 && lockWorkerTypeFilters == false))
            {
                // Do not filter by based on employee or contractor employment
                employmentTypeRange.value(SysQuery::valueUnlimited());
            }
            else if (includeEmployees)
            {
                employmentTypeRange.value(queryValue(HcmEmploymentType::Employee));
            }
            else if (includeContractors)
            {
                employmentTypeRange.value(queryValue(HcmEmploymentType::Contractor));
            }
    
            if (includeActive && includePending && includeTerminated
                || (_workerRecId != 0 && lockWorkerStatusFilters == false))
            {
                _queryToUpdate.validTimeStateDateTimeRange(DateTimeUtil::minValue(), DateTimeUtil::maxValue());
                validFromRange.value(SysQuery::valueUnlimited());
                validToRange.value(SysQuery::valueUnlimited());
            }
            else if (includeActive && includePending)
            {
                _queryToUpdate.validTimeStateDateTimeRange(now, DateTimeUtil::maxValue());
                validFromRange.value(SysQuery::valueUnlimited());
                validToRange.value(SysQuery::valueUnlimited());
            }
            else if (includeTerminated && includeActive)
            {
                _queryToUpdate.validTimeStateDateTimeRange(DateTimeUtil::minValue(), now);
                validFromRange.value(SysQuery::valueUnlimited());
                validToRange.value(SysQuery::valueUnlimited());
            }
            else if (includeActive)
            {
                _queryToUpdate.validTimeStateDateTimeRange(now, now);
                validFromRange.value(SysQuery::valueUnlimited());
                validToRange.value(SysQuery::valueUnlimited());
            }
            else if (includeTerminated)
            {
                _queryToUpdate.validTimeStateDateTimeRange(DateTimeUtil::minValue(), DateTimeUtil::utcNow());
                validFromRange.value(SysQuery::valueUnlimited());
                validToRange.value(SysQueryRangeUtil::lessThanUtcNow());
    
                // don't include active employments
                employmentValidFromRange.value(SysQueryRangeUtil::lessThanUtcNow());
                employmentValidToRange.value(SysQueryRangeUtil::greaterThanUtcNow());
                qbdsActiveEmployments.enabled(true);
            }
            else if (includePending)
            {
                _queryToUpdate.validTimeStateDateTimeRange(HcmDateTimeUtil::startOfCurrentDay(), DateTimeUtil::maxValue());
                validFromRange.value(SysQueryRangeUtil::greaterThanUtcNow());
                validToRange.value(SysQuery::valueUnlimited());
    
                // don't include active employments
                employmentValidFromRange.value(SysQueryRangeUtil::lessThanUtcNow());
                employmentValidToRange.value(SysQueryRangeUtil::greaterThanUtcNow());
                qbdsActiveEmployments.enabled(true);
            }
    
            if (requireUserRelation)
            {
                qbdsDirPersonUser.enabled(true);
            }
    
            if (restrictToDirects)
            {
                //this.restrictingForManagerDirectReports(qbdsWorker);
            }
    
            return _queryToUpdate;
        }
    
    }

  • Verified answer
    ergun sahin Profile Picture
    8,816 Moderator on at
    RE: Active worker through x++

    if you are using newCustomOptions;

    HcmWorkerLookup::newCustomOptions(true, true, true, false, false, false, false, true, false);
        

    then only four value is YES

    pastedimage1628524391806v3.png

    If we goto updateQuery method with this info we have 6 range (but one condition);

    workerRange.value(SysQuery::valueUnlimited());
    legalEntityRange.value(SysQuery::valueUnlimited());
    employmentTypeRange.value(SysQuery::valueUnlimited());
    _queryToUpdate.validTimeStateDateTimeRange(now, now);
    validFromRange.value(SysQuery::valueUnlimited());
    validToRange.value(SysQuery::valueUnlimited());

    Looking at the Query, it doesn't look much different from the first one I shared. It may be necessary to join the Dirperson table as well.

    Are you using something out of the standard?

  • Riyas ahamed F Profile Picture
    1,215 on at
    RE: Active worker through x++

    No need that legal entity sir, i found it 

    public void init()
        {
            super();
            workerLookup = HcmWorkerLookup::newCustomOptions(true, true, true, false, false, false, false, true, false);
        }

    As i mentioned init code That worker based on newCustomOptions parameter based come in worker.

    I checked demo different Boolean in same parameter its come 12 workers only.

    Note: But ,the table level its show 500 workers.

    Form level only 26.

    I hope its clear for you now.

    Now, its possible to in job show that 26 workers ? please give some example.

    Thanks

  • ergun sahin Profile Picture
    8,816 Moderator on at
    RE: Active worker through x++

    You can add LegalEntity and EmploymentType fields

  • Riyas ahamed F Profile Picture
    1,215 on at
    RE: Active worker through x++

    Thanks for your valuable suggestion.

    Sir I tried in your code  its show in the info msg 500 workers. total i have 938.

    Sorry i think the thing is not active worker related I misled you.

    Its my bad. because what I thought its active worker  

    Now, as i mentioned above lookup in UI its show 26 worker number and name. how ?

    My goal that 26 workers only. let me know sir how it show ? do you have a any idea ?

    Please forgive my ignorance and thank you so much for the time you are taking to help.

  • Suggested answer
    ergun sahin Profile Picture
    8,816 Moderator on at
    RE: Active worker through x++

    I don't know why you are stuck with HcmWorkerLookup, but if you want to solve it like there, you can examine the HcmWorkerLookup query and the updateQuery method of the HcmWorkerLookup class.

    Or you can simply look at the isActive method of HcmEmployment

    public static boolean isActive(HcmEmploymentRecId _recId)
    {
        HcmEmployment hcmEmployment;
        utcdatetime now = DateTimeUtil::utcNow();
    
        return _recId && (select ValidTimeState(now) RecId from hcmEmployment
                    where hcmEmployment.RecId == _recId).RecId!= 0;
    }

    If you discard the while condition and join the worker table, it will give you active workers.

    HcmEmployment hcmEmployment;
    utcdatetime now = DateTimeUtil::utcNow();
    HcmWorker worker;
    
    while select ValidTimeState(now) hcmEmployment
    join worker
    where worker.RecId == hcmEmployment.Worker
    {
    //..info etc.
    }

    Note:I wrote the code in the editor, changes may be needed.

  • Riyas ahamed F Profile Picture
    1,215 on at
    RE: Active worker through x++

    Thanks for your replay ergun sir,

    In my custom form used worker lookup reference method.

    Private HcmWorkerLookup workerLookup;
     
    public void init()
    {
        super();
        workerLookup = HcmWorkerLookup::newCustomOptions(true, true, true, false, false, false, false, true, false);
    }
     
    *******Worker field dataSource lookup reference method***********
     public Common lookupReference(FormReferenceControl _formReferenceControl)
    {
        return workerLookup.lookupWorkerTeam(_formReferenceControl);
    }

    As i mentioned above code its show in UI 26 worker no and name.

    The same process how can i show in job ? give me some example for that sir.

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

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

#1
Martin Dráb Profile Picture

Martin Dráb 601 Most Valuable Professional

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 416

#3
Adis Profile Picture

Adis 384 Super User 2025 Season 1

Product updates

Dynamics 365 release plans