This is the sample code for creating the Employee and if employee exists then updating the employee.
First build the class to read from CSV file and call this methods for creating/updateing the worker.
Please change the container column number values from your csv positions.
we can use HCMWorkerImportService for create, but this service did'nt provide the update functionality.
I will update the the next post using service to create/update the employee.
private void run()
{
if (!HcmWorker::findByPersonnelNumber(conPeek(_c, #EmployeeNumber)).RecId)
{//Update employee
this.employeeCreate(_c);
}
else
{//Insert employee
this.employeeUpdate(_c);
}
}
static public Description createDescription(DirPersonName _dirPersonName)
{
#Define.Space(" ")
return _dirPersonName.FirstName
+ (_dirPersonName.MiddleName ? #Space + _dirPersonName.MiddleName : "")
+ (_dirPersonName.LastName ? #Space + _dirPersonName.LastName : "");
}
public Static HcmPersonGender convGender(str _gender)
{
HcmPersonGender ret;
#Define.Female("Kvinde")
#Define.Male("Mand")
;
switch (_gender)
{
case #Female:
ret = HcmPersonGender::Female;
break;
case #Male:
ret = HcmPersonGender::Male;
break;
default:
ret = HcmPersonGender::None;
}
return ret;
}
public Static str defaultCountryRegionId()
{
str ret;
LogisticsPostalAddress logisticsPostalAddress_Default;
;
logisticsPostalAddress_Default.initValue();
ret = logisticsPostalAddress_Default.CountryRegionId;
return ret;
}
public static date convDate(str _date)
{
Date ret;
;
ret = str2Date(_date, 123);
return ret;
}
This method is used to create the employee.
Create
private void employeeCreate(container _c)
{
boolean ret = true;
CompanyInfo companyInfo;
HcmEmploymentRecId newEmploymentRecId;
ValidFromDateTime employmentStartDateTime;
ValidToDateTime employmentEndDateTime;
HcmWorker newHcmWorker;
DirPerson dirPerson;
DirPersonName dirPersonName;
HcmEmploymentType hcmEmploymentType = HcmEmploymentType::Employee;
NumberSeq numberSeqPersonnelNum;
HcmPersonPrivateDetails HcmPersonPrivateDetails;
AxLogisticsPostalAddress axLogisticsPostalAddress = new AxLogisticsPostalAddress();
AxLogisticsLocation axLogisticsLocation;
;
companyInfo = companyInfo::find();
employmentStartDateTime = datetobeginUtcDateTime(HrmImportEmployeeMasterdata::convDate(conpeek(_c, #DateOfHiring)), DateTimeUtil::getUserPreferredTimeZone());
employmentEndDateTime = DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::maxValue(), DateTimeUtil::getUserPreferredTimeZone());
dirPersonName.FirstName = conpeek(_c, #FirstName);
dirPersonName.MiddleName = conpeek(_c, #MiddleName);
dirPersonName.LastName = conpeek(_c, #LastName);
newHcmWorker = HcmWorker::find(HcmWorkerTransition::newCreateHcmWorker(dirPersonName
, conpeek(_c, #EmployeeNumber)
, companyInfo.RecId
, hcmEmploymentType
, employmentStartDateTime
, employmentEndDateTime));
if (newHcmWorker.RecId == 0)
{
ret = false;
}
if (newHcmWorker.RecId == 0)
{
// Updating an existing worker
// If there is no active employment for the worker
newEmploymentRecId = HcmWorkerTransition::newCreateHcmEmployment(newHcmWorker.RecId, companyInfo.RecId,
hcmEmploymentType, employmentStartDateTime, employmentEndDateTime);
if (newEmploymentRecId == 0)
{
ret = false;
}
}
if (ret)
{
if(numberSeqPersonnelNum)
{
// mark number sequence based ID consumed
numberSeqPersonnelNum.used();
}
}
else
{
if(numberSeqPersonnelNum)
{
numberSeqPersonnelNum.abort();
}
dirPerson.clear();
dirPersonName.Person = 0;
}
hcmPersonPrivateDetails.initValue();
hcmPersonPrivateDetails.Person = dirPersonName.Person;
hcmPersonPrivateDetails.BirthDate = HrmImportEmployeeMasterdata::convDate(conpeek(_c, #BirthDate));
hcmPersonPrivateDetails.Gender = HrmImportEmployeeMasterdata::convGender(conpeek(_c, #Gender));
hcmPersonPrivateDetails.insert();
dirPerson = dirPerson::find(dirPersonName.Person, true);
dirPerson.Initials = conpeek(_c, #Initials);
dirPerson.ProfessionalTitle = conpeek(_c, #Position);
dirPerson.update();
//Create address
axLogisticsLocation = new AxLogisticsLocation();
axLogisticsLocation.validateInput(true);
axLogisticsLocation.parmIsPostalAddress(NoYes::Yes);
axLogisticsLocation.parmDescription(HrmImportEmployeeMasterdata::createDescription(dirPersonName));
axLogisticsLocation.save();
axLogisticsPostalAddress = new AxLogisticsPostalAddress();
axLogisticsPostalAddress.parmLocation(axLogisticsLocation.parmRecId());
axLogisticsPostalAddress.validateInput(true);
axLogisticsPostalAddress.parmCountryRegionId(HrmImportEmployeeMasterdata::defaultCountryRegionId());
axLogisticsPostalAddress.parmZipCode(conpeek(_c, #PostalCode));
axLogisticsPostalAddress.parmZipCodeRecId(LogisticsAddressZipCode::find(conpeek(_c, #PostalCode)).RecId);
axLogisticsPostalAddress.parmStreet(conpeek(_c, #Address));
axLogisticsPostalAddress.parmCity(LogisticsAddressZipCode::find(conpeek(_c, #PostalCode)).City);
axLogisticsPostalAddress.parmCityRecId(LogisticsAddressZipCode::find(conpeek(_c, #PostalCode)).CityRecId);
axLogisticsPostalAddress.save();
DirParty::addLocation(dirPersonName.Person, axLogisticsLocation.parmRecId(), true, true, true);
}
This method is used to update the employee
Update
private void employeeUpdate(container _c)
{
boolean ret = true;
CompanyInfo companyInfo;
ValidFromDateTime employmentStartDateTime;
ValidToDateTime employmentEndDateTime;
HcmWorker hcmWorker;
DirPerson dirPerson;
DirPersonName dirPersonName;
HcmEmploymentType hcmEmploymentType = HcmEmploymentType::Employee;
HcmPersonPrivateDetails HcmPersonPrivateDetails;
AxLogisticsPostalAddress axLogisticsPostalAddress = new AxLogisticsPostalAddress();
AxLogisticsLocation axLogisticsLocation;
Description description_Old;
HcmEmployment hcmEmployment;
LogisticsLocation logisticsLocation;
LogisticsPostalAddress logisticsPostalAddress;
DirPartyLocation dirPartyLocation;
;
companyInfo = companyInfo::find();
hcmWorker = hcmWorker::findByPersonnelNumber(conpeek(_c, #EmployeeNumber));
//Update dirPersonName
dirPersonName = dirPersonName::find(hcmWorker.Person, true);
description_Old = HrmImportEmployeeMasterdata::createDescription(dirPersonName);
if ( dirPersonName.FirstName != conpeek(_c, #FirstName)
|| dirPersonName.MiddleName != conpeek(_c, #MiddleName)
|| dirPersonName.LastName != conpeek(_c, #LastName))
{
dirPersonName.validTimeStateUpdateMode(ValidTimeStateUpdate::Correction);
dirPersonName.FirstName = conpeek(_c, #FirstName);
dirPersonName.MiddleName = conpeek(_c, #MiddleName);
dirPersonName.LastName = conpeek(_c, #LastName);
dirPersonName.update();
}
//Update hiring
employmentStartDateTime = datetobeginUtcDateTime(HrmImportEmployeeMasterdata::convDate(conpeek(_c, #DateOfHiring)), DateTimeUtil::getUserPreferredTimeZone());
employmentEndDateTime = DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::maxValue(), DateTimeUtil::getUserPreferredTimeZone());
hcmWorker = HcmWorker::findByPersonnelNumber(conpeek(_c, #EmployeeNumber));
hcmEmployment = hcmEmployment::getActiveEmploymentsByWorker(hcmWorker.RecId, employmentStartDateTime, employmentEndDateTime, true);
if (hcmEmployment.RecId)
{
HcmWorkerTransition::newUpdateHcmEmployment(hcmEmployment, employmentStartDateTime, employmentEndDateTime);
}
else
{
hcmWorker = HcmWorker::find(HcmWorkerTransition::newCreateHcmWorker(dirPersonName
, conpeek(_c, #EmployeeNumber)
, companyInfo.RecId
, hcmEmploymentType
, employmentStartDateTime
, employmentEndDateTime));
}
hcmPersonPrivateDetails = HcmPersonPrivateDetails::findByPerson(hcmWorker.Person, true);
if (hcmPersonPrivateDetails)
{
if ( hcmPersonPrivateDetails.BirthDate != HrmImportEmployeeMasterdata::convDate(conpeek(_c, #BirthDate))
|| hcmPersonPrivateDetails.Gender != HrmImportEmployeeMasterdata::convGender(conpeek(_c, #Gender)))
{
hcmPersonPrivateDetails.BirthDate = HrmImportEmployeeMasterdata::convDate(conpeek(_c, #BirthDate));
hcmPersonPrivateDetails.Gender = HrmImportEmployeeMasterdata::convGender(conpeek(_c, #Gender));
hcmPersonPrivateDetails.update();
}
}
else
{
hcmPersonPrivateDetails.initValue();
hcmPersonPrivateDetails.Person = dirPersonName.Person;
hcmPersonPrivateDetails.BirthDate = HrmImportEmployeeMasterdata::convDate(conpeek(_c, #BirthDate));
hcmPersonPrivateDetails.Gender = HrmImportEmployeeMasterdata::convGender(conpeek(_c, #Gender));
hcmPersonPrivateDetails.insert();
}
dirPerson = dirPerson::find(dirPersonName.Person, true);
if ( dirPerson.Initials != conpeek(_c, #Initials)
|| dirPerson.ProfessionalTitle != conpeek(_c, #Position))
{
dirPerson.Initials = conpeek(_c, #Initials);
dirPerson.ProfessionalTitle = conpeek(_c, #Position);
dirPerson.update();
}
//Update or Create address
select firstOnly forUpdate logisticsLocation
where logisticsLocation.Description == description_Old
join dirPartyLocation
where dirPartyLocation.Location == logisticsLocation.RecId
&& dirPartyLocation.Party == dirPerson.RecId;
if (logisticsLocation)
{
axLogisticsLocation = AxInternalBase::construct(logisticsLocation);
}
else
{
axLogisticsLocation = new AxLogisticsLocation();
}
axLogisticsLocation.validateInput(true);
axLogisticsLocation.parmIsPostalAddress(NoYes::Yes);
axLogisticsLocation.parmDescription(HrmImportEmployeeMasterdata::createDescription(dirPersonName));
axLogisticsLocation.save();
//Update or Create postaladdress
select firstOnly forUpdate logisticsPostalAddress
where logisticsPostalAddress.Location == axLogisticsLocation.parmRecId();
if (logisticsPostalAddress)
{
axLogisticsPostalAddress = AxInternalBase::construct(logisticsPostalAddress);
}
else
{
axLogisticsPostalAddress = new AxLogisticsPostalAddress();
}
axLogisticsPostalAddress.parmLocation(axLogisticsLocation.parmRecId());
axLogisticsPostalAddress.validateInput(true);
axLogisticsPostalAddress.parmCountryRegionId(HrmImportEmployeeMasterdata::defaultCountryRegionId());
axLogisticsPostalAddress.parmZipCode(conpeek(_c, #PostalCode));
axLogisticsPostalAddress.parmZipCodeRecId(LogisticsAddressZipCode::find(conpeek(_c, #PostalCode)).RecId);
axLogisticsPostalAddress.parmStreet(conpeek(_c, #Address));
axLogisticsPostalAddress.parmCity(LogisticsAddressZipCode::find(conpeek(_c, #PostalCode)).City);
axLogisticsPostalAddress.parmCityRecId(LogisticsAddressZipCode::find(conpeek(_c, #PostalCode)).CityRecId);
axLogisticsPostalAddress.save();
DirParty::addLocation(dirPersonName.Person, axLogisticsLocation.parmRecId(), true, true, true);
}
First build the class to read from CSV file and call this methods for creating/updateing the worker.
Please change the container column number values from your csv positions.
we can use HCMWorkerImportService for create, but this service did'nt provide the update functionality.
I will update the the next post using service to create/update the employee.
private void run()
{
if (!HcmWorker::findByPersonnelNumber(conPeek(_c, #EmployeeNumber)).RecId)
{//Update employee
this.employeeCreate(_c);
}
else
{//Insert employee
this.employeeUpdate(_c);
}
}
static public Description createDescription(DirPersonName _dirPersonName)
{
#Define.Space(" ")
return _dirPersonName.FirstName
+ (_dirPersonName.MiddleName ? #Space + _dirPersonName.MiddleName : "")
+ (_dirPersonName.LastName ? #Space + _dirPersonName.LastName : "");
}
public Static HcmPersonGender convGender(str _gender)
{
HcmPersonGender ret;
#Define.Female("Kvinde")
#Define.Male("Mand")
;
switch (_gender)
{
case #Female:
ret = HcmPersonGender::Female;
break;
case #Male:
ret = HcmPersonGender::Male;
break;
default:
ret = HcmPersonGender::None;
}
return ret;
}
public Static str defaultCountryRegionId()
{
str ret;
LogisticsPostalAddress logisticsPostalAddress_Default;
;
logisticsPostalAddress_Default.initValue();
ret = logisticsPostalAddress_Default.CountryRegionId;
return ret;
}
public static date convDate(str _date)
{
Date ret;
;
ret = str2Date(_date, 123);
return ret;
}
This method is used to create the employee.
Create
private void employeeCreate(container _c)
{
boolean ret = true;
CompanyInfo companyInfo;
HcmEmploymentRecId newEmploymentRecId;
ValidFromDateTime employmentStartDateTime;
ValidToDateTime employmentEndDateTime;
HcmWorker newHcmWorker;
DirPerson dirPerson;
DirPersonName dirPersonName;
HcmEmploymentType hcmEmploymentType = HcmEmploymentType::Employee;
NumberSeq numberSeqPersonnelNum;
HcmPersonPrivateDetails HcmPersonPrivateDetails;
AxLogisticsPostalAddress axLogisticsPostalAddress = new AxLogisticsPostalAddress();
AxLogisticsLocation axLogisticsLocation;
;
companyInfo = companyInfo::find();
employmentStartDateTime = datetobeginUtcDateTime(HrmImportEmployeeMasterdata::convDate(conpeek(_c, #DateOfHiring)), DateTimeUtil::getUserPreferredTimeZone());
employmentEndDateTime = DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::maxValue(), DateTimeUtil::getUserPreferredTimeZone());
dirPersonName.FirstName = conpeek(_c, #FirstName);
dirPersonName.MiddleName = conpeek(_c, #MiddleName);
dirPersonName.LastName = conpeek(_c, #LastName);
newHcmWorker = HcmWorker::find(HcmWorkerTransition::newCreateHcmWorker(dirPersonName
, conpeek(_c, #EmployeeNumber)
, companyInfo.RecId
, hcmEmploymentType
, employmentStartDateTime
, employmentEndDateTime));
if (newHcmWorker.RecId == 0)
{
ret = false;
}
if (newHcmWorker.RecId == 0)
{
// Updating an existing worker
// If there is no active employment for the worker
newEmploymentRecId = HcmWorkerTransition::newCreateHcmEmployment(newHcmWorker.RecId, companyInfo.RecId,
hcmEmploymentType, employmentStartDateTime, employmentEndDateTime);
if (newEmploymentRecId == 0)
{
ret = false;
}
}
if (ret)
{
if(numberSeqPersonnelNum)
{
// mark number sequence based ID consumed
numberSeqPersonnelNum.used();
}
}
else
{
if(numberSeqPersonnelNum)
{
numberSeqPersonnelNum.abort();
}
dirPerson.clear();
dirPersonName.Person = 0;
}
hcmPersonPrivateDetails.initValue();
hcmPersonPrivateDetails.Person = dirPersonName.Person;
hcmPersonPrivateDetails.BirthDate = HrmImportEmployeeMasterdata::convDate(conpeek(_c, #BirthDate));
hcmPersonPrivateDetails.Gender = HrmImportEmployeeMasterdata::convGender(conpeek(_c, #Gender));
hcmPersonPrivateDetails.insert();
dirPerson = dirPerson::find(dirPersonName.Person, true);
dirPerson.Initials = conpeek(_c, #Initials);
dirPerson.ProfessionalTitle = conpeek(_c, #Position);
dirPerson.update();
//Create address
axLogisticsLocation = new AxLogisticsLocation();
axLogisticsLocation.validateInput(true);
axLogisticsLocation.parmIsPostalAddress(NoYes::Yes);
axLogisticsLocation.parmDescription(HrmImportEmployeeMasterdata::createDescription(dirPersonName));
axLogisticsLocation.save();
axLogisticsPostalAddress = new AxLogisticsPostalAddress();
axLogisticsPostalAddress.parmLocation(axLogisticsLocation.parmRecId());
axLogisticsPostalAddress.validateInput(true);
axLogisticsPostalAddress.parmCountryRegionId(HrmImportEmployeeMasterdata::defaultCountryRegionId());
axLogisticsPostalAddress.parmZipCode(conpeek(_c, #PostalCode));
axLogisticsPostalAddress.parmZipCodeRecId(LogisticsAddressZipCode::find(conpeek(_c, #PostalCode)).RecId);
axLogisticsPostalAddress.parmStreet(conpeek(_c, #Address));
axLogisticsPostalAddress.parmCity(LogisticsAddressZipCode::find(conpeek(_c, #PostalCode)).City);
axLogisticsPostalAddress.parmCityRecId(LogisticsAddressZipCode::find(conpeek(_c, #PostalCode)).CityRecId);
axLogisticsPostalAddress.save();
DirParty::addLocation(dirPersonName.Person, axLogisticsLocation.parmRecId(), true, true, true);
}
This method is used to update the employee
Update
private void employeeUpdate(container _c)
{
boolean ret = true;
CompanyInfo companyInfo;
ValidFromDateTime employmentStartDateTime;
ValidToDateTime employmentEndDateTime;
HcmWorker hcmWorker;
DirPerson dirPerson;
DirPersonName dirPersonName;
HcmEmploymentType hcmEmploymentType = HcmEmploymentType::Employee;
HcmPersonPrivateDetails HcmPersonPrivateDetails;
AxLogisticsPostalAddress axLogisticsPostalAddress = new AxLogisticsPostalAddress();
AxLogisticsLocation axLogisticsLocation;
Description description_Old;
HcmEmployment hcmEmployment;
LogisticsLocation logisticsLocation;
LogisticsPostalAddress logisticsPostalAddress;
DirPartyLocation dirPartyLocation;
;
companyInfo = companyInfo::find();
hcmWorker = hcmWorker::findByPersonnelNumber(conpeek(_c, #EmployeeNumber));
//Update dirPersonName
dirPersonName = dirPersonName::find(hcmWorker.Person, true);
description_Old = HrmImportEmployeeMasterdata::createDescription(dirPersonName);
if ( dirPersonName.FirstName != conpeek(_c, #FirstName)
|| dirPersonName.MiddleName != conpeek(_c, #MiddleName)
|| dirPersonName.LastName != conpeek(_c, #LastName))
{
dirPersonName.validTimeStateUpdateMode(ValidTimeStateUpdate::Correction);
dirPersonName.FirstName = conpeek(_c, #FirstName);
dirPersonName.MiddleName = conpeek(_c, #MiddleName);
dirPersonName.LastName = conpeek(_c, #LastName);
dirPersonName.update();
}
//Update hiring
employmentStartDateTime = datetobeginUtcDateTime(HrmImportEmployeeMasterdata::convDate(conpeek(_c, #DateOfHiring)), DateTimeUtil::getUserPreferredTimeZone());
employmentEndDateTime = DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::maxValue(), DateTimeUtil::getUserPreferredTimeZone());
hcmWorker = HcmWorker::findByPersonnelNumber(conpeek(_c, #EmployeeNumber));
hcmEmployment = hcmEmployment::getActiveEmploymentsByWorker(hcmWorker.RecId, employmentStartDateTime, employmentEndDateTime, true);
if (hcmEmployment.RecId)
{
HcmWorkerTransition::newUpdateHcmEmployment(hcmEmployment, employmentStartDateTime, employmentEndDateTime);
}
else
{
hcmWorker = HcmWorker::find(HcmWorkerTransition::newCreateHcmWorker(dirPersonName
, conpeek(_c, #EmployeeNumber)
, companyInfo.RecId
, hcmEmploymentType
, employmentStartDateTime
, employmentEndDateTime));
}
hcmPersonPrivateDetails = HcmPersonPrivateDetails::findByPerson(hcmWorker.Person, true);
if (hcmPersonPrivateDetails)
{
if ( hcmPersonPrivateDetails.BirthDate != HrmImportEmployeeMasterdata::convDate(conpeek(_c, #BirthDate))
|| hcmPersonPrivateDetails.Gender != HrmImportEmployeeMasterdata::convGender(conpeek(_c, #Gender)))
{
hcmPersonPrivateDetails.BirthDate = HrmImportEmployeeMasterdata::convDate(conpeek(_c, #BirthDate));
hcmPersonPrivateDetails.Gender = HrmImportEmployeeMasterdata::convGender(conpeek(_c, #Gender));
hcmPersonPrivateDetails.update();
}
}
else
{
hcmPersonPrivateDetails.initValue();
hcmPersonPrivateDetails.Person = dirPersonName.Person;
hcmPersonPrivateDetails.BirthDate = HrmImportEmployeeMasterdata::convDate(conpeek(_c, #BirthDate));
hcmPersonPrivateDetails.Gender = HrmImportEmployeeMasterdata::convGender(conpeek(_c, #Gender));
hcmPersonPrivateDetails.insert();
}
dirPerson = dirPerson::find(dirPersonName.Person, true);
if ( dirPerson.Initials != conpeek(_c, #Initials)
|| dirPerson.ProfessionalTitle != conpeek(_c, #Position))
{
dirPerson.Initials = conpeek(_c, #Initials);
dirPerson.ProfessionalTitle = conpeek(_c, #Position);
dirPerson.update();
}
//Update or Create address
select firstOnly forUpdate logisticsLocation
where logisticsLocation.Description == description_Old
join dirPartyLocation
where dirPartyLocation.Location == logisticsLocation.RecId
&& dirPartyLocation.Party == dirPerson.RecId;
if (logisticsLocation)
{
axLogisticsLocation = AxInternalBase::construct(logisticsLocation);
}
else
{
axLogisticsLocation = new AxLogisticsLocation();
}
axLogisticsLocation.validateInput(true);
axLogisticsLocation.parmIsPostalAddress(NoYes::Yes);
axLogisticsLocation.parmDescription(HrmImportEmployeeMasterdata::createDescription(dirPersonName));
axLogisticsLocation.save();
//Update or Create postaladdress
select firstOnly forUpdate logisticsPostalAddress
where logisticsPostalAddress.Location == axLogisticsLocation.parmRecId();
if (logisticsPostalAddress)
{
axLogisticsPostalAddress = AxInternalBase::construct(logisticsPostalAddress);
}
else
{
axLogisticsPostalAddress = new AxLogisticsPostalAddress();
}
axLogisticsPostalAddress.parmLocation(axLogisticsLocation.parmRecId());
axLogisticsPostalAddress.validateInput(true);
axLogisticsPostalAddress.parmCountryRegionId(HrmImportEmployeeMasterdata::defaultCountryRegionId());
axLogisticsPostalAddress.parmZipCode(conpeek(_c, #PostalCode));
axLogisticsPostalAddress.parmZipCodeRecId(LogisticsAddressZipCode::find(conpeek(_c, #PostalCode)).RecId);
axLogisticsPostalAddress.parmStreet(conpeek(_c, #Address));
axLogisticsPostalAddress.parmCity(LogisticsAddressZipCode::find(conpeek(_c, #PostalCode)).City);
axLogisticsPostalAddress.parmCityRecId(LogisticsAddressZipCode::find(conpeek(_c, #PostalCode)).CityRecId);
axLogisticsPostalAddress.save();
DirParty::addLocation(dirPersonName.Person, axLogisticsLocation.parmRecId(), true, true, true);
}