Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Unanswered

How to update records and insert new records into a ValidateTimeState Table

(1) ShareShare
ReportReport
Posted on by 6
I have a requirement to insert records into a validTimestate table if the records don't exist and update the existing record's ValidFrom field using X++
 
I am having the below error message

"Stopped (error): Cannot create a record in Employee Benefits (Go2PayEmplBenefit). Insert not supported with the values specified for 'Effective' and 'Expiration'. New record overlaps with multiple existing records."
 
private void updateEmployeeBenefits(HRServiceRequest _serviceRequest, List _benefits, EmplId _emplId)
 {               
     EmplBenefit emplBenefit, emplBenefitNew;
     EmplGroupBenefit emplGroupBenefit;
     HRMPayrollLine payrolling;
     TransDate transDate, validFrom, validTo, startDate, endDate;
     ListEnumerator enumerator = _benefits.getEnumerator();

     transDate = systemDateGet();

     
     while(enumerator.moveNext())
     {

         str currentBenefitType = enumerator.current();

         select validtimestate(transDate) payrollLine
             where payrollLine.EmplId == _emplId;

         if(!payrollLine.HrmPayrollCategoryId)
         {
             return;
         }
         
         startDate = payrollLine.ValidFrom;
         endDate = payrollLine.ValidTo;

         while select  validtimestate(startDate, endDate) emplGroupBenefit
                 where emplGroupBenefit.BenefitType == currentBenefitType
                 && emplGroupBenefit.HRMPayrollCategoryId == payrollLine.HrmPayrollCategoryId
         {

             if(emplGroupBenefit)
             {
                 validFrom = emplGroupBenefit.ValidFrom;
                 validTo = emplGroupBenefit.ValidTo;
                 

                 select forupdate validtimestate(validTo) emplBenefit
                     where emplBenefit.EmplId == _emplId
                     && emplBenefit.BenefitType == emplGroupBenefit.BenefitType;

                 if(emplBenefit)
                 {
                     ttsbegin;

                     emplBenefit.EntitledBenefitsAmount = emplGroupBenefit.Amount;
                     emplBenefit.BenefitsTaken = emplGroupBenefit.Amount;
                     emplBenefit.Monetize = _serviceRequest.Monetize ? NoYes::Yes : NoYes::No;
                     emplBenefit.ValidFrom = _serviceRequest.RequiredDate;
                     emplBenefit.validTimeStateUpdateMode(ValidTimeStateUpdate::Correction);
                     emplBenefit.update();
                     
                     ttscommit;

                     continue;
                 }

                 
                 select validtimestate(validFrom, validTo) emplBenefit
                     order by validFrom
                     where emplBenefit.EmplId == _emplId
                     && emplBenefit.BenefitType == emplGroupBenefit.BenefitType;

                 emplBenefitNew.clear();
                 emplBenefitNew.BenefitType = emplGroupBenefit.BenefitType;
                 emplBenefitNew.EntitledBenefitsAmount = emplGroupBenefit.Amount;
                 emplBenefitNew.BenefitsTaken = emplGroupBenefit.Amount;
                 emplBenefitNew.Monetize = _serviceRequest.Monetize ? NoYes::Yes : NoYes::No;

                 if (emplBenefit.RecId)
                 {
                     // Handle overlapping records
                     emplBenefitNew.ValidFrom = _serviceRequest.RequiredDate;
                     emplBenefitNew.ValidTo = emplGroupBenefit.ValidTo - 1;
                 }
                 else
                 {
                     emplBenefitNew.ValidFrom = _serviceRequest.RequiredDate;
                     emplBenefitNew.ValidTo = emplGroupBenefit.ValidTo;
                 }
                 emplBenefitNew.validTimeStateUpdateMode(ValidTimeStateUpdate::CreateNewTimePeriod);
                 emplBenefitNew.insert();                                        
             }
         }
     }
 }


 
Categories:
  • Martin Dráb Profile Picture
    Martin Dráb 230,503 Most Valuable Professional on at
    How to update records and insert new records into a ValidateTimeState Table
    You set both records with the same ValidFrom value, therefore they're guaranteed to overlap. It's not clear what you intended to do, but this is clearly wrong.
     
    By the way, you shouldn't need the update at all. The date-effective framework can update the previous record for you.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,431 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,503 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans