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 the value NULL into column 'Name', table 'TableName'; the column does not allow NULLs. UPDATE/INSERT fails", it means that no corresponding record was found in the joined table, so a null value was fetched and tried to be inserted into the table's column. 

It can happen if values are obtained from outer-joined data sources when there is no corresponding record in the outer-joined table, and thus a null value is retrieved.

Another case is when values are obtained from views, and those views contain calculated columns whose values can be "empty".

The solution can be using "inner join" with update_recordset and insert_recordset commands, or by using a standard "while select" clause with a view that has calculated columns.

For example, if the view is used and the code below throws the mentioned error: "Cannot insert the value NULL into column 'Name', table 'TableName'; the column does not allow NULLs. UPDATE fails"

TableForecast     tableForecast;

OperationSumView  operationSumView;

update_recordset tableForecast

    setting ItemId    = operationSumView.ItemId,
            ItemName  = operationSumView.ItemName
join ItemId, ItemName from operationSumView
    where operationSumView.RefRecId == tableForecast.RecId;

It is an example of how it can be changed to avoid the error.

while select forupdate tableForecast

join ItemId, ItemName from operationSumView
   where operationSumView.RefRecId == tableForecast.RecId
{
   tableForecast.ItemId    = operationSumView.ItemId;
   tableForecast.ItemName  = operationSumView.ItemName;
   tableForecast.update();
}

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...