giovedì 17 dicembre 2020

D365FFO - Creare un nuovo worker ed associarlo ad uno user

 In questo post vediamo come creare un worker da associare ad uno user. Ho preso spunto da questo post per AX 2012 adattandolo alle nuove classi aggiungendo la parte che creal'associazione User -> Worker:

https://community.dynamics.com/ax/f/microsoft-dynamics-ax-forum/155550/create-worker-through-code/704763

 public static void CreateEmployeeAndAssignTouser(UserId _userId,  
                  FirstName _firstName,  
                  MiddleName _middleName,  
                  LastName _lastName,  
                  str _email,  
                  HcmPersonnelNumberId _employeeId,  
                  date _joiningDate,  
                  HcmTitleId _title,  
                  SelectableDataArea _ReleaseInCompany  
 )  
   {  
     DirPersonName            dirPersonName;  
     HcmWorker              newHcmWorker;  
     HcmWorkerTitle           hcmWorkerTitle;  
     LogisticsLocation          logisticsLocation;  
     DirPartyContactInfoView       dirPartyContactInfoView;  
     DirParty              dirParty;  
     ValidFromDateTime          employmentStartDateTime;  
     ValidToDateTime           employmentEndDateTime;  
     str                 employeeEmailAdress;  
     HcmPersonnelNumberId        employeeid;  
     RecId                compayRecId;  
     HcmCreateWorkerContract       createWorkerParams;  
     ;  
   
     dirPersonName.FirstName   = _firstName;  
     dirPersonName.MiddleName  = _middleName;  
     dirPersonName.LastName   = _lastName;  
     employeeEmailAdress     = _email;  
     employeeid         = _employeeId;  
     compayRecId         = CompanyInfo::findDataArea(_ReleaseInCompany).RecId;  
   
     employmentStartDateTime = DateTimeUtil::newDateTime(_joiningDate,0);  
     employmentEndDateTime  = DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::maxValue(), DateTimeUtil::getUserPreferredTimeZone());  
   
     if(!HcmWorker::findByPersonnelNumber(employeeid))  
     {  
       createWorkerParams = HcmCreateWorkerContract::construct();  
   
       createWorkerParams.parmDirPersonName(dirPersonName);  
       createWorkerParams.parmPersonnelNumber(employeeid);  
       createWorkerParams.parmLegalEntityRecId(compayRecId);  
       createWorkerParams.parmEmploymentType(HcmEmploymentType::Employee);  
       createWorkerParams.parmEmploymentValidFrom(employmentStartDateTime);  
       createWorkerParams.parmEmploymentValidTo(employmentEndDateTime);  
   
       newHcmWorker = HcmWorker::find(HcmWorkerTransition::newCreateHcmWorkerV2(createWorkerParams));  
   
       dirParty = new DirParty(DirPerson::find(dirPersonName.Person));  
   
       if(employeeEmailAdress != '' && newHcmWorker.Person != 0)  
       {  
         logisticsLocation.clear();  
         logisticsLocation  = LogisticsLocation::create("@SYS5845", NoYes::No);  
   
         dirPartyContactInfoView.LocationName        = "@SYS5845";  
         dirPartyContactInfoView.Locator           = employeeEmailAdress;  
         dirPartyContactInfoView.Type            = LogisticsElectronicAddressMethodType::Email;  
         dirPartyContactInfoView.Party            = DirPerson::find(newHcmWorker.Person).RecId;  
         dirPartyContactInfoView.IsPrimary          = NoYes::Yes;  
           
         dirParty.createOrUpdateContactInfo(dirPartyContactInfoView);  
       }  
   
       if (newHcmWorker.RecId != 0)  
       {  
         ttsBegin;  
         hcmWorkerTitle.clear();  
         hcmWorkerTitle.Worker    = newHcmWorker.RecId;  
         hcmWorkerTitle.ValidFrom  = DateTimeUtil::newDateTime(_joiningDate,0);  
         hcmWorkerTitle.ValidTo   = employmentEndDateTime;  
   
         if(_title)  
         {  
           hcmWorkerTitle.Title = HcmTitle::findByTitle(_title).RecId;  
         }  
   
         hcmWorkerTitle.insert();  
         ttsCommit;  
       }  
   
                //assegna il worker all'utente  
       HcmWorker hcmWorker = HcmWorker::findByPersonnelNumber(employeeid);  
       DirPersonUser::createDirPersonUser(_userId, hcmWorker.Person);  
     }  
   }  

Dopo aver lanciato il job possiamo vedere il risultato:





Nessun commento:

Posta un commento