Wednesday, November 27, 2019

Update AX system fields either CreatedDateTime or ModifiedDateTime

Sometimes while creating a record if we need to update the created date time or modified date time system field in D365FO. Please find the below code to update the createddatetime field during the record creation.

This method will help you to update createDateTime field while inserting the record.

static void updateCreatedDateTime(Args _args)
{
CustGroup custGroup;

ttsBegin;
new OverwriteSystemfieldsPermission().assert();
custGroup.overwriteSystemfields(true);
custGroup.custGroupId= “Abcd”;
custGroup.Name= “Abcd”;
custGroup.(fieldnum(custGroup,CreatedDateTime))= str2datetime( “2019/11/28 05:30:00” ,321 );
custGroup.doInsert();
custGroup.overwriteSystemfields(false);
CodeAccessPermission::revertAssert();
ttsCommit;
}


static void updateCreatedDateTime(Args _args)
{
        CustGroup custGroup;

ttsBegin;
new OverwriteSystemfieldsPermission().assert();
custGroup.overwriteSystemfields(true);
custGroup = custGroup::find("Abcd");
custGroup.Name= “Abcd”;
custGroup.(fieldnum(custGroup,ModifiedDateTime))= str2datetime( “2019/11/28 06:30:00” ,321 );
custGroup.doUpdate();
custGroup.overwriteSystemfields(false);
CodeAccessPermission::revertAssert();
ttsCommit;
}

No comments:

Post a Comment