Search This Blog

Monday, April 2, 2012

AX2012 Create Employee or Worker using HCMWorkerImport Service


In my previous post I used to create the worker by inserting into table directly. In this example I am using HCMWorkerImport Service for create the employee from CSV file. Before using this code make sure to change the fields positions using your file. and make sure you service (HcmWorkerImportService) is active.

Before writing the code please see the query that used for service AxdHcmWorkerImport, how the datasources relations are mapped in query, same way we should add the data to the entity objects.



  private void  krishh_employeeCreateUsingService(container _c)
        {
            HcmWorkerImportService                      hcmWorkerImportService;
            HcmWorkerImport                             hcmWorkerImport;
            HcmWorkerImport_HcmWorker                   hcmWorkerImport_HcmWorker;
            HcmWorkerImport_DirPerson_DirPerson         hcmWorkerImport_DirPerson_DirPerson;
            HcmWorkerImport_HcmPersonPrivateDetails     hcmWorkerImport_HcmPersonPrivateDetails;
            HcmWorkerImport_DirPersonName               hcmWorkerImport_DirPersonName;
            HcmWorkerImport_DirPartyPostalAddressVie    hcmWorkerImport_DirPartyPostalAddressVie;
     
            HcmWorkerImport_HcmEmployment               hcmWorkerImport_HcmEmployment;
            HcmWorkerImport_HcmEmploymentDetail         hcmWorkerImport_HcmEmploymentDetail;
            HcmWorkerImport_HcmWorkerTitle              hcmWorkerImport_HcmWorkerTitle;
            ;
        // creating the service object
            hcmWorkerImportService          = HcmWorkerImportService::construct();
            hcmWorkerImport                 = new HcmWorkerImport();
     
            hcmWorkerImport_HcmWorker       = hcmWorkerImport.createHcmWorker().addNew();
            hcmWorkerImport_HcmWorker.parmPersonnelNumber(conpeek(_c, EmployeeNumber));
     
            hcmWorkerImport_DirPerson_DirPerson = hcmWorkerImport_HcmWorker.createDirPerson().addNew();
            hcmWorkerImport_DirPerson_DirPerson.parmInitials(conpeek(_c, Initials));
            hcmWorkerImport_DirPerson_DirPerson.parmProfessionalTitle(conpeek(_c, Position));
     
            hcmWorkerImport_DirPersonName       = hcmWorkerImport_DirPerson_DirPerson.createDirPersonName().addNew();
            hcmWorkerImport_DirPersonName.parmFirstName(conpeek(_c, FirstName));
            hcmWorkerImport_DirPersonName.parmMiddleName(conpeek(_c, MiddleName));
            hcmWorkerImport_DirPersonName.parmLastName(conpeek(_c, LastName));
     
            hcmWorkerImport_HcmPersonPrivateDetails = hcmWorkerImport_DirPerson_DirPerson.createHcmPersonPrivateDetails().addNew();
            hcmWorkerImport_HcmPersonPrivateDetails.parmBirthDate(HrmImportEmployeeMasterdata::convDate(conpeek(_c, BirthDate)));
     
            hcmWorkerImport_HcmPersonPrivateDetails.parmGender(HrmImportEmployeeMasterdata::convGender(conpeek(_c, Gender)));
     
            hcmWorkerImport_DirPartyPostalAddressVie = hcmWorkerImport_DirPerson_DirPerson.createDirPartyPostalAddressView().addNew();
            hcmWorkerImport_DirPartyPostalAddressVie.parmCountryRegionId(HrmImportEmployeeMasterdata::defaultCountryRegionId());
            hcmWorkerImport_DirPartyPostalAddressVie.parmRoles(HrmImportEmployeeMasterdata::defaultRole());
            hcmWorkerImport_DirPartyPostalAddressVie.parmStreet(conpeek(_c, Address));
          hcmWorkerImport_DirPartyPostalAddressVie.parmStreetNumber(conpeek(_c, streetnum)));
            hcmWorkerImport_DirPartyPostalAddressVie.parmZipCode(conpeek(_c, PostalCode));
     
            hcmWorkerImport_HcmEmployment   = hcmWorkerImport_HcmWorker.createHcmEmployment().addNew();
            hcmWorkerImport_HcmEmployment.parmLegalEntity(curext());
     
            hcmWorkerImport_HcmEmploymentDetail = hcmWorkerImport_HcmEmployment.createHcmEmploymentDetail().addNew();
            hcmWorkerImport_HcmEmploymentDetail.parmWorkerStartDate(
                datetobeginUtcDateTime(HrmImportEmployeeMasterdata::convDate(conpeek(_c, DateOfHiring)), DateTimeUtil::getUserPreferredTimeZone()));
     
            hcmWorkerImportService.create(hcmWorkerImport);
     
        }

11 comments:

  1. Hi krishna,

    Your blogs are very useful.
    Please let know ,how to insert the Position,startdate details of employee\worker using HCMWorkerimportservice using .net console application.


    regards,
    nivas

    ReplyDelete
  2. Hi Krishna,

    Is there a possibility to update the data using the service HCMWorkerImport?

    Thanks!

    ReplyDelete
    Replies
    1. Hi,
      In Standard this service was not provided to update the Employee......its was only for creation...

      Regards,
      krishna.

      Delete
    2. Hi Krishna,
      i have used your method on the create/update for workers, with some modification and that works perfect, but do you know if it's possible to change this service so you can also use it for update.. i want to use it in AIF, I am already trying, but it doesn't seem to be that easy, also i am missing the defaultdimension in the service ;-) do you know if there's i fix for that ?

      Delete
    3. Hi Alex,
      For this in standard update action was not available, If we are trying to expose this action, Its not supporting, We will get the runtime exception.

      Regards,
      krishna.

      Delete
    4. Alex Schreurs would you share with us your update code

      Delete
  3. Hi can you please provide me some information or code snippet to insert free text invoice using cust free text invoice service.
    Thanks
    Daws

    ReplyDelete
  4. Hi,
    I am trying to import HCMworker using above code but i am getting this error,
    can you please help me on this

    error is
    "Line=1, Pos=642, Xpath=/HcmWorkerImport/HcmWorker[1]/DirPerson[1]/DirPartyPostalAddressView[1]
    The field Type is blank or the relation record is missing"


    ReplyDelete
    Replies
    1. Hi Akshay,
      I think your csv file is missing the value of ZipCode,I think ZipCode is mandatory for address.

      Regards,
      krishna.

      Delete
    2. Hey all,

      I am sending out a note to those of you that have taken advantage of my Blog(http://krishhdax.blogspot.com),through skype,Chat..etc, that I have been making over the past couple of years, asking for a favor.

      It is that time of year 2013, and we are now announcing the nomination process for The Most Influential People in Microsoft Dynamics for 2013. I would really like to make the list this year, and I hope that I have influenced you all in my own little way.

      If you feel inclined, you can nominate me by sending an email with a persuasive explanation for the nomination to top100@dynamicsworld.co.uk or through Twitter using the #dwtop100 tag.

      You can find more on the nomination process can be found here: http://www.dynamicsworld.co.uk/top100nominationprocess/

      Thanks in advance.

      Krishna Reddy Modugula | CGI |AX Technical Manager | Microsoft Solutions
      Lautrupvang 10, DK-2750 Ballerup | Denmark
      Phone: +45 44 78 40 00 | Mobile: +45 41 88 23 35
      www.cgi.com | www.cgi.dk
      http://krishhdax.blogspot.com
      http://www.linkedin.com/in/krishhprofile

      Delete
    3. I was getting the same error. The problem was on the "parmRoles" line - you need to specify a text value that matches the "Type" field of a record in LogisticsLocationRole (ie "Home", "Invoice", etc)

      Delete

Thanks for visiting my blog,
I will reply for your comment within 48 hours.

Thanks,
krishna.