web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

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

(2) 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:
I have the same question (0)
  • Martin Dráb Profile Picture
    239,680 Most Valuable Professional on at
    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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 658 Super User 2026 Season 1

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 632 Super User 2026 Season 1

#3
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 570

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans