I was struggling to update Date Effective Table in Dynamics Ax 2012 R3. At least one hour of struggle, finally I update the required record.
Only good post which helps is by Agnela GoldBrg.
http://ibisinc.com/blog/updating-date-effective-tables-in-ax-2012/
One line tip from this post is on Date Effective table, The active record reference can be get with of
DateTimeUtil::utcNow()
Its really small tip, yes learning and solving problem at hand waste hours.
Lets take a real world example We have to update Job detail with X++.
For HCMJobDetail table following way works for me.
hcmJobDetail= HcmJobDetail::findByJob(hcmJob.RecId,DateTimeUtil::utcNow(),DateTimeUtil::utcNow(),true);
We can query on required table with following way
_validFrom = DateTimeUtil::utcNow();
_validTo =DateTimeUtil::utcNow();
select ValidTimeState(_validFrom, _validTo) hcmJobDetail
where hcmJobDetail.Job == _job;
The complete code snippet which works for me is here.
HcmJobDetail hcmJobDetail;
HcmJob hcmJob;
HCMJOBID _id=’XyZ’;
date dateToday,
hcmJob = HcmJob::findByJob(_id);
_validFrom = DateTimeUtil::utcNow();
_validTo =DateTimeUtil::utcNow();
select ValidTimeState(_validFrom, _validTo) hcmJobDetail
where hcmJobDetail.Job == _job;
hcmJobDetail= HcmJobDetail::findByJob(hcmJob.RecId,DateTimeUtil::utcNow(),DateTimeUtil::utcNow(),true);
if (hcmJobDetail!=null)
{
try
{
ttsBegin;
hcmJobDetail.validTimeStateUpdateMode(ValidTimeStateUpdate::Correction);
hcmJobDetail.SL_TempEndDate = today();
hcmJobDetail.SL_TempStartDate = today();
hcmJobDetail.Note = ” test and test”;
hcmJobDetail.update();
ttsCommit;
}
catch
{
ttsAbort;
HcmJob::CheckFailed(‘ Unable to update job’);
// status = SL_EntityStatus::Error;
}
}
}
}