Terminating an employee

Hi,

I have a written a class where upon terminating an employee, the date has to be updated in own table(CaeEmplTable)


public static void caeWorkerTerminationInsert(HcmWorker hcmworker)
{
     CAEEmplTable caeEmplTable;
    HcmEmployment hcmemployment;
    hcmemployment = hcmemployment ::find(hcmworker.RecId);
     ttsBegin;
    caeEmplTable = caeEmplTable::find(hcmEmployment.Worker,true);
    if(caeEmplTable.RecId)
    {

     caeEmplTable.Termination = HcmReasonCode::find(HcmEmploymentDetail::findByEmployment(hcmEmployment.RecId).TransitionReasonCode).Description;
 
        caeEmplTable.update();
    }
    else
    {

         caeEmplTable.Termination = HcmReasonCode::find(HcmEmploymentDetail::findByEmployment(hcmEmployment.RecId).TransitionReasonCode).Description;

        caeEmplTable.insert();

    }
    ttsCommit;
}

But now i had add an logic that on terminating an employee it has to check the legal entity of that particular employee and update the end date in caeempltable.

HcmEmployment and CaeEmplTable has to 2 rows for one worker.

It’s actually transfer of employee


public static void caeWorkerTerminationInsert(HcmWorker hcmworker)
{
     CAEEmplTable caeEmplTable;
    HcmEmployment hcmemployment;
    hcmemployment = hcmemployment ::find(hcmworker.RecId);
    hcmemployment = hcmemployment::findByWorkerLegalEntity(hcmworker.RecId,CompanyInfo::current());
     ttsBegin;

    while select caeEmplTable
    where caeEmplTable.dataAreaId == CompanyInfo::findByDataAreaID(CompanyInfo::current()).dataAreaId && caeEmplTable.Worker == hcmEmployment.Worker


    if(caeEmplTable.RecId)
    {

     caeEmplTable.Termination = HcmReasonCode::find(HcmEmploymentDetail::findByEmployment(hcmEmployment.RecId).TransitionReasonCode).Description;

        caeEmplTable.update();
    }
    else
    {

         caeEmplTable.Termination = HcmReasonCode::find(HcmEmploymentDetail::findByEmployment(hcmEmployment.RecId).TransitionReasonCode).Description;

        caeEmplTable.insert();

    }
       
    ttsCommit;
}

from one company to another company. . So the value exists twice with different startdate and End date in both the company.

The above code of the employee exists only in one company but if its multiple company ,then the enddate gets update for both the company. it has to update only for selected company select eid…

hcmemployment = hcmemployment::findByWorkerLegalEntity(hcmworker.RecId,CompanyInfo::current());

while select caeEmplTable
where caeEmplTable.dataAreaId == CompanyInfo::findByDataAreaID(CompanyInfo::current()).dataAreaId && caeEmplTable.Worker == hcmEmployment.Worker

I wrote the above code to get the hcmemployment and caeempltable by legal entity wise. But when i debug it shows null

Thanks,

The if and else in your while statement doesn’t make any sense, it will go into while if there is a record, satisfying the given conditions.

While doing the insert operation, i think you may also have to initialize other fields. Example - caeEmplTable.Worker

caeEmplTable - is this table company specific or global ? Check the saveDataPerCompany property on the table?

if this is set to No, then caeEmplTable.dataAreaId == CompanyInfo::findByDataAreaID(CompanyInfo::current()).dataAreaId is not true.

If saveDataPerCompany is set to yes, then the select statement by default will give you the current company data as you are not using the cross company query and you don’t need the condition on dataAreaId in your select statement.

Thanks for the response

caeEmplTable is a company specific table. SaveDataPerCompany is set to yes.

Please ignore the first post .I found where is the issue. We have 2 company .From one company ,i am transfering an employee to another company.

Company 1 - E008888

HcmEmploment table

Valid From: 2017-12-31 23:59:59.000

Valid To : 2018-09-29 23:59:59.000

Company 2 - E008888

HcmEmploment table

Valid From: 2018-09-30 23:59:59.000

Valid To : 2154-12-31 23:59:59.000

Currently that employee is in company 2 and Valid to is open

Again i am terminating in company 2 for E008888

Company 2 - E008888

HcmEmploment table

Valid From: 2018-09-30 23:59:59.000

Valid To : 2018-10-02 23:59:59.000

Now my issue is i am always updating Valid to and valid from in to the CaeEmplTable. It works fine. But since the employee is transfered, Hcmemployment has to rows for the same EID with different legal entity.

So when hcmemployment table gets updated when employee terminated,automatically caeempltable termination date also to be changed and equal to hcmemployment valid to.

Below code is from hcmemployment table

public void update()
{
    caeEmplTable  caeEmplTable;
    ttsbegin;

    if (this.RecId
        && (this.ValidFrom != this.orig().ValidFrom || this.ValidTo != this.orig().ValidTo))
    {
        WrkCtrTable::adjustWorkerResourceGroupMembership(this.Worker, this.LegalEntity, this.orig().ValidFrom, this.orig().ValidTo, this.ValidFrom, this.ValidTo);
    }

    super();

    ttscommit;


    ttsbegin;
    caeEmplTable = caeEmplTable::find(HcmWorker::find(this.Worker).RecId,true);
 //(CompanyInfo::findByDataAreaID(this.LegalEntity).dataAreaId == caeEmplTable.dataAreaId)
   //    hcmemployment = hcmemployment::findByWorkerLegalEntity(hcmworker.RecId,CompanyInfo::current());
    if(caeEmplTable.Worker)
    {

         caeEmplTable.StartDate = this.ValidFrom;
       // caeEmplTable.StartDate1 = this.ValidFrom;
       //  caeEmplTable.EndDate1 = this.ValidTo ;
        caeEmplTable.EndDate = this.ValidTo ;
         if(caeEmplTable.EndDate >= (DateTimeUtil::addYears(DateTimeUtil::maxValue(),-1)))
         {
            caeEmplTable.Status = "Employed";
         }
        else
         {
        caeEmplTable.Status = "Terminated";
        }

     caeEmplTable.update();
    }


    ttsCommit;

}


Why cannot move your code to \Classes\HcmWorkerTransition\endAllHcmEmployments? (this method is triggered by \Classes\HcmWorkerTransition\terminateHcmWorker)