Monday, September 30, 2013

Employee Master import from CSV using X++ programming

public class EmployeeMasterImport extends RunBase
{
    CommaIo                     csvFile;
    Filename                    filename;
    DialogField                 dialogFilename;
    #define.CurrentVersion(2)
    #localmacro.CurrentList
    filename,
    insertIncorrectRecords,
    #endmacro
    #localmacro.ListVersion1
    filename,
    insertIncorrectRecords
    #endmacro
    #File
}
public Object dialog()
{
    DialogRunbase       dialog = super();
    ;
    dialogFilename  = dialog.addField(extendedTypeStr(FilenameOpen));
    dialogFilename.value(filename);
    dialog.filenameLookupFilter(["All files", #AllFiles]);
    return dialog;
}

public boolean getFromDialog()
{
    filename                = dialogFilename.value();
    return true;
}
void run()
{
    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;
    axHcmWorkerTitle                        axHcmWorkerTitle;
    AxLogisticsPostalAddress                axLogisticsPostalAddress = new AxLogisticsPostalAddress();
    AxLogisticsLocation                     axLogisticsLocation;
    LogisticsAddressStateID                 stateId,countryid;
    DirParty Dirparty;
    DirPartyContactInfoView                 contactView;
    container                               readCon;
    counter                                 icount,inserted;
    str                                     birthdate,StartDate,firstname,middlename,lastname,EmplName,gender,city,street,mailid;
    TransDate                               StDate;
    ;
    csvFile = new CommaIO(filename, 'r');
    try
    {
        if (csvFile)
        {
            ttsbegin;
            while (csvFile.status() == IO_Status::OK)
            {
                    readCon = csvFile.read();
                    icount++;
                if (readCon  )//dont insert first record of file : header
                {
                    companyInfo = companyInfo::find();
                    EmplName = conpeek(readcon, 1);
                    firstname = conpeek(readcon, 3);
                    middlename = conpeek(readcon, 4);
                    lastname =conpeek(readcon, 5);
                    birthdate = conpeek(readcon, 6);
                    Gender = conpeek(readcon, 7);
                    stateId = conPeek(readcon,9);
                    city  = conPeek(readcon,10);
                    Street  = conPeek(readcon,11);
                    countryid = conpeek(readcon, 12);
                    StartDate = conpeek(readcon, 13);
                    mailid = conpeek(readcon, 14);
                    //employmentStartDateTime = datetobeginUtcDateTime(HrmImportEmployeeMasterdata::convDate(StartDate), DateTimeUtil::getUserPreferredTimeZone());
                    //employmentEndDateTime   = DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::maxValue(), DateTimeUtil::getUserPreferredTimeZone());
                    dirPersonName.FirstName     = firstname;
                    dirPersonName.MiddleName    = middlename;
                    dirPersonName.LastName      = lastname;
                    newHcmWorker = HcmWorker::find(HcmWorkerTransition::newCreateHcmWorker(dirPersonName
                                                                                           , EmplName
                                                                                           , 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(birthdate);
                    hcmPersonPrivateDetails.Gender      = HrmImportEmployeeMasterdata::convGender(gender);
                    hcmPersonPrivateDetails.insert();
                    dirPerson                   = dirPerson::find(dirPersonName.Person, true);
                    DirParty = DirParty::constructFromPartyRecId(dirPerson.recid);
                    contactView.LocationName = "Delivery email";
                    contactView.Locator      = strLRTrim(mailid);
                    contactView.Type         = LogisticsElectronicAddressMethodType::Email;
                    contactView.Party        = dirPerson.recid; //partyRecId;
                    contactView.IsPrimary    = NoYes::Yes;
                    dirParty.createOrUpdateContactInfo(contactView);
                    //dirPerson.Initials          = conpeek(readcon, #Initials);
                   // dirPerson.ProfessionalTitle = conpeek(readcon, 8);
                   // dirPerson.update();
                    /*if(stateId)
                    {
                        stateId = subStr(stateId,1,10);
                    }*/
                    //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(countryid) ; //HrmImportEmployeeMasterdata::defaultCountryRegionId());
                    axLogisticsPostalAddress.parmState(stateId);
                    //axLogisticsPostalAddress.parmZipCode(conpeek(readcon, #PostalCode));
                    //axLogisticsPostalAddress.parmZipCodeRecId(LogisticsAddressZipCode::find(conpeek(readcon, #PostalCode)).RecId);
                    axLogisticsPostalAddress.parmStreet(street);
                    axLogisticsPostalAddress.parmCity(city);   //LogisticsAddressZipCode::find(conpeek(readcon, #PostalCode)).City);
                   // axLogisticsPostalAddress.parmCityRecId(LogisticsAddressZipCode::find(conpeek(readcon, #PostalCode)).CityRecId);
                    axLogisticsPostalAddress.save();
                    DirParty::addLocation(dirPersonName.Person, axLogisticsLocation.parmRecId(), true, true, true);
                }
            }
            ttsCommit;
        }
        icount--;//Remove header recount from total record count
    }
    catch(Exception::Error)
    {
        info(strFmt("%1 %2",Exception::Error,icount));
    }
}

static void main(Args  args)
{
    EmployeeMasterImport        EmployeeMasterImport;
    ;
    EmployeeMasterImport =  new EmployeeMasterImport();
    if(EmployeeMasterImport.prompt())
    {
        EmployeeMasterImport.run();
    }
}

class HrmImportEmployeeMasterdata
{
    container _c1;
}

public static date convDate(str _date)
{
    Date ret;
    ;

    ret = str2Date(_date, 213);

    return ret;
}

public Static HcmPersonGender convGender(str _gender)
{
    HcmPersonGender ret;
    #Define.Female("F")
    #Define.Male("M")
    ;
    switch (_gender)
    {
        case #Female:
            ret = HcmPersonGender::Female;
            break;

        case #Male:
            ret = HcmPersonGender::Male;
            break;

        default:
            ret = HcmPersonGender::None;
    }

    return ret;
}

static public Description createDescription(DirPersonName _dirPersonName)
{
    #Define.Space(" ")
    return _dirPersonName.FirstName
        + (_dirPersonName.MiddleName ? #Space + _dirPersonName.MiddleName : "")
        + (_dirPersonName.LastName   ? #Space + _dirPersonName.LastName   : "");
}
 

2 comments:

  1. What field is there in Conpeek(2)?

    ReplyDelete
  2. What was the format of the "date" fields in CSV file? I try DD\MM\YYYY, DD/MM/YYYY, DD.MM.YYYY and the reader fails in each of these cases.

    ReplyDelete