Data entity: insert not allowed for field

Error: insert not allowed for field 'FieldName'
Error occurs because of properties Allow Edit and Allow Edit On Created are set to No at Data Entity field and staging table.
At Data Entity they also might be set to Auto. If original mapped field has those properties set to No then Auto means copy from original field.
Unfortunately if you have mapped field and it's Allow Edit properties set to No it is not enough to set relevant properties on Data Entity, it will not help you will still get the error.
The solution is to use Virtual Field, which is not mapped to original field directly.
Create Virtual Field (unmapped field of same type) and set Is Computed Field property to No
Overwrite mapEntityToDataSource method in following manned where you should move the value from Virtual Field to target field:
    public void mapEntityToDataSource(DataEntityRuntimeContext _entityCtx, DataEntityDataSourceRuntimeContext _dataSourceCtx)
    {
        InventTransferLine      inventTransferLine;

        switch (_dataSourceCtx.name())
        {
            case dataEntityDataSourceStr(FAMInventTransferOrderLineEntity, InventTransferLine):
                
                inventTransferLine = _dataSourceCtx.getBuffer();

                if (_entityCtx.getDatabaseOperation() == DataEntityDatabaseOperation::Insert)
                {
                    InventTransferTable transferTable = InventTransferTable::find(this.TransferId);
                    InventDim           inventDim;

                    inventTransferLine.initFromInventTransferTable(transferTable, true);

                    if (this.InventInventoryDimensionEntityFieldsMapping::isAnyInventDimFieldSet())
                    {
                        inventDim = inventTransferLine.inventDim();
                        this.InventInventoryDimensionEntityFieldsMapping::updateInventDim(inventDim);
                        inventTransferLine.InventDimId = InventDim::findOrCreate(inventDim).inventDimId;
                    }

                    inventTransferLine.ItemId           = this.ItemNumber;
                    inventTransferLine.modifiedField(fieldNum(InventTransferLine, ItemId));
                    inventTransferLine.QtyTransfer      = this.QtyTransfer;
                    inventTransferLine.modifiedField(fieldNum(InventTransferLine, QtyTransfer));
                    inventTransferLine.QtyRemainReceive = inventTransferLine.QtyTransfer;
                    inventTransferLine.QtyRemainShip    = inventTransferLine.QtyTransfer;
                    inventTransferLine.InventDimId      = inventDim.inventDimId;
                    inventTransferLine.LineNum          = this.LineNumber;
                    
                    _dataSourceCtx.setBuffer(inventTransferLine);
                }
                break;
        }

        super(_entityCtx, _dataSourceCtx);

        switch (_dataSourceCtx.name())
        {
            case dataEntityDataSourceStr(FAMInventTransferOrderLineEntity, InventDim):
                inventTransferLine = _entityCtx.getRuntimeContextByName(dataEntityDataSourceStr(FAMInventTransferOrderLineEntity, InventTransferLine)).getBuffer();
                _dataSourceCtx.setBuffer(InventDim::find(inventTransferLine.InventDimId));
                break;
        }
    }
In the example above the field is inventTransferLine.TransferId it is not set explicitlyin the code of mapEntityToDataSource, but it is setting in the inventTransferLine.initFromInventTransferTable method.
Make sure that Allow Edit and Allow Edit On Created are not both set to No at Data Entity and staging table.



 

Search

About

DaxOnline.org is free platform that allows you to quickly store and reuse snippets, notes, articles related to Dynamics AX.

Authors are allowed to set their own AdSense units.
Join us.

Blog Tags