D365 Finance and Operations. How to get the RecId sequence of the table via X++.

In D365 RecID there is a SQL system sequence now. SQL itself maintains the IDs. I found a post on this topic. 

Using SQL commands described there, I created an X++ method in order to get the sequence name of the table.

It can be useful if you develop a data upgrade script and it is needed to align the sequence when data is moved ‘as is’ to the target D365 table.

public str getSequenceProcedureName(TableName   _tableName)

{
   str         result;
   str         ret;
   TableId     tableId;

 

   if (this.isTablePresent(_tableName))
   {
       str sqlQuery = strFmt(@"
                SELECT ID from TABLEIDTABLE
                    JOIN sys.sequences ON sys.sequences.name = CONCAT('SEQ_', TABLEIDTABLE.ID)
                    WHERE TABLEIDTABLE.NAME = '%1'",
                    _tableName);

 

       result  =  ReleaseUpdateDB::statementExeQuery(sqlQuery);

       if (result)

       {
          tableId = str2Int(result);

 

          if (tableId)
          {
             ret = strFmt('SEQ_%1',tableId);
          }
       }
    }
 
    return ret;

}

No comments:

Set-based operations in Dynamics 365 Dynamics 365 Finance and Operations. Cannot insert the value NULL into column 'Name', table 'TableName'; column does not allow nulls. UPDATE/INSERT fails.

When a set-based operation is used (e.g., update_recordset or insert_recordset), and an error occurs with the message " Cannot insert t...