Friday, June 15, 2012

HcmWorkerImportService to create/import worker into AX 2012

Hi All,
         Today we will be having a look at the AIFDocumentServices to create Worker (Contractor/Employee) into AX 2012 using HcmWorkerImportService.

Pre-requisites:
1) AX 2012 with CU2
2) Demo Data
3) Visual Studio Dev Tools

Scenario:
Wants to create/import worker into AX 2012 from an external application.

Solution:

  1. Create a Service Group in AOT
  2. Rename it to HcmWorkerImportDemo
  3. Drag the "HcmWorkerImportService" into your newly created service group.
  4. Right Click on the "HcmWorkerImportDemo" & click on Deploy Service Group.
  5. Go to System Administration -> Setup -> Services & Application Integration Framework -> Inbound Ports
  6. Find your "HcmWorkerImportDemo" Inbound port & Copy the WSDL Address.
  7. Create a C# Console Application.
  8. Right click on your solution -> add Service Reference -> Paste the WSDL Address
  9. Type the Reference Name like "ABC"
  10. Include ABC as a namespace.
  11. Modify the main() method with the below code :
            HcmWorkerImportServiceClient proxy = new HcmWorkerImportServiceClient();
            CallContext context = new CallContext();
            context.Company = "ceu"; //Company Name

            AxdHcmWorkerImport worker = new AxdHcmWorkerImport();
            AxdEntity_HcmWorker hcmWorkerTable = new AxdEntity_HcmWorker();

            AxdEntity_DirPerson_DirPerson party = new AxdEntity_DirPerson_DirPerson();
            party.NameAlias = "Kuldeep";
            party.Gender = AxdEnum_Gender.Male;
            party.MaritalStatus = AxdEnum_DirPersonMaritalStatus.Married;

            AxdEntity_DirPersonName personName = new AxdEntity_DirPersonName();
            personName.FirstName = "Kuldeep";
            personName.MiddleName = "Singh ";
            personName.LastName = "Godara";
            party.DirPersonName = personName;

            hcmWorkerTable.DirPerson = party;

            AxdEntity_HcmEmployment personEmployment = new AxdEntity_HcmEmployment();
            personEmployment.EmploymentType = AxdEnum_HcmEmploymentType.Contractor;
            personEmployment.LegalEntity = "ceu"; //Company Name

            hcmWorkerTable.HcmEmployment = new AxdEntity_HcmEmployment[1] { personEmployment };

            worker.HcmWorker = new AxdEntity_HcmWorker[1] { hcmWorkerTable };

            try
            {
                proxy.create(context, worker);
                Console.WriteLine("Success");
                Console.ReadLine();
            }
            catch
            {
                throw;
            }
12. Run the Application.
13. Cross Check your worker into AX 2012.


Enjoy DAX !!!

5 comments:

  1. Hi,

    Its working fine,But i am getting following issue

    When i am trying to add employee first time it was inserted successfully.second time i am trying to insert another record its showing already record exist.
    what is the problem..
    Thanks inadvance...

    regards,
    somu

    ReplyDelete
  2. Hi,
    Use with different GUID then it should work fine.

    ReplyDelete
  3. Hi Kuldeep,
    is it possible to add a old sales transaction done in 2008 into AX 2012 using WCF service?
    After this import, this transaction should reflect in the sales achieved.

    Regards
    Akash

    ReplyDelete
  4. If you get the employee exists, its because there's an entry in the HCMEMPLOYMENTEMPLOYEE table where its set the record to 0 for the Employment column. This table is used for storing info like pension start dates etc, if you dont need it then you can comment out these lines in the class AxdHCMWorkerImport class, method prepareForSaveExtended

    // Ensure HcmEmployment record is saved
    case AxdRecordProcessingContext::AfterAllChildRecordsProcessed:
    if (axHcmEmployment.parmEmploymentType() == HcmEmploymentType::Employee)
    {
    // hcmEmploymentEmployee.ValidFrom = axHcmEmployment.parmValidFrom();
    // hcmEmploymentEmployee.ValidTo = axHcmEmployment.parmValidTo();
    // hcmEmploymentEmployee.Employment = axHcmEmployment.parmRecId();
    // hcmEmploymentEmployee.insert();
    }
    else if (axHcmEmployment.parmEmploymentType() == HcmEmploymentType::Contractor)
    {
    // hcmEmploymentContractor.ValidFrom = axHcmEmployment.parmValidFrom();
    // hcmEmploymentContractor.ValidTo = axHcmEmployment.parmValidTo();
    // hcmEmploymentContractor.Employment = axHcmEmployment.parmRecId();
    // hcmEmploymentContractor.insert();
    }


    Save and compile and reload the service.

    ReplyDelete
  5. How to do update as dirpersonname has date-effective-data field? The C# for update is throwing 'Invalid conversion' error

    KeyField DirPersonNameKeyField1 = new KeyField { Field = "RecId", Value = "52565428785" };
    EntityKey DirPersonNameEntityKey = new EntityKey() { KeyData = new[] { DirPersonNameKeyField1 } };
    EntityKey[] DirPersonNamesEntityKeys = new EntityKey[1] { DirPersonNameEntityKey };
    var pre = client.read(ctx, DirPersonNamesEntityKeys);
    var lastLine = pre.HcmWorker[0].DirPerson.DirPersonName;
    var DirPName= new AxdEntity_DirPersonName()
    {
    RecId = lastLine.RecId,
    RecIdSpecified = true,
    RecVersion = lastLine.RecVersion,
    ValidFrom=lastLine.ValidFrom,
    LastName="new",
    action = AxdEnum_AxdEntityAction.update,
    actionSpecified = true,
    };

    var HWorker=new AxdEntity_HcmWorker()
    {
    _DocumentHash=pre.HcmWorker[0]._DocumentHash,
    RecId=pre.HcmWorker[0].RecId,
    RecIdSpecified=true,
    RecVersion=1,
    RecVersionSpecified=true,
    action = AxdEnum_AxdEntityAction.update,
    actionSpecified = true,

    DirPerson = new[] {to(DirPName)}, // error is thrown here
    };

    ReplyDelete

Thanks for your time reviewing this blog.