Updating the Next Rec ID Sequence in Dynamics AX
Recently I had to create database records outside of Dynamics AX. I used SQL to move some records from ISV tables to new standard tables within AX. There was a ton of data, so it was much faster to insert these records using SQL. Initially, I overlooked one important factor. When I created these records outside of the AX environment, the SystemSequences table was not updated.
The SystemSequences table stores the next RecID by table in the AX environment: https://msdn.microsoft.com/en-us/library/systemsequences.aspx
If this table is not updated properly, the users will see duplicate record errors that may not make sense to you. The users had sent me this error below. When I looked at the data, the Load ID did not exist. It didn’t throw an error about Rec IDs, so it’s a bit misleading.
The issue was that I had created records in the WHSLoadTable outside of AX. The SystemSequences table wasn’t updated, so the next value of the Rec ID was incorrect and throwing an error. In this situation, you must update the SystemSequences table. You’ll need to find the max value for the table and update to the max value + 1.
Here is a portion of the SQL used to fix this issue:
DECLARE @MaxRecID BIGINT DECLARE @NextVal BIGINT SELECT @MaxRecID = MAX(RECID) FROM WHSLoadTable SELECT @NextVal = NEXTVAL FROM SYSTEMSEQUENCES INNER JOIN SQLDICTIONARY ON SQLDICTIONARY.FIELDID = 0 AND SQLDICTIONARY.name = 'WHSLoadTable' AND SQLDICTIONARY.TABLEID = SYSTEMSEQUENCES.TABID IF (@NextVal > @MaxRecID) BEGIN PRINT 'WHSLoadTable did not need to be updated.' END ELSE BEGIN PRINT 'Updated WHSLoadTable from ' + CONVERT(VARCHAR(MAX), @NextVal) + '' to '' + CONVERT(VARCHAR(MAX), @MaxRecID + 1) UPDATE SYSTEMSEQUENCES SET NEXTVAL = @MaxRecID + 1 FROM SYSTEMSEQUENCES INNER JOIN SQLDICTIONARY ON SQLDICTIONARY.FIELDID = 0 AND SQLDICTIONARY.name = 'WHSLoadTable' AND SQLDICTIONARY.TABLEID = SYSTEMSEQUENCES.TABID END
I hope this helps you out!
Under the terms of this license, you are authorized to share and redistribute the content across various mediums, subject to adherence to the specified conditions: you must provide proper attribution to Stoneridge as the original creator in a manner that does not imply their endorsement of your use, the material is to be utilized solely for non-commercial purposes, and alterations, modifications, or derivative works based on the original material are strictly prohibited.
Responsibility rests with the licensee to ensure that their use of the material does not violate any other rights.