Skip to main content

Create AxTableContext from IAxaptaRecordAdapter or from a RecId

Somtimes we need to create a record context from a custom record but not based on a record from a data set. It is a bit tricky to create a menu item record context fram an IAxaptaRecordAdapter or a RecId.

Here is an example


// Get the recid for which record the context should be fore
// where record is any record from any given table in Ax
Int64 recId = record.GetField("RecId”);                    

int tableId = TableMetadata.TableNum(this.AxSession, "[TABLENAME]"); 

// Create a dict table
Proxy.DictTable dictTable = new Proxy.DictTable(AxSession.AxaptaAdapter, tableId);

// Get the field id
int fieldId_RecId = TableDataFieldMetadata.FieldNum(this.AxSession, dictTable.name(), "RecId");

// Create the table metadata
TableMetadata tblMetadata = MetadataCache.GetTableMetadata(AxSession, tableId);

// Create the dictinary object
Dictionary<int, object> dictRecId = new Dictionary<int, object>();
dictRecId.Add(fieldId_RecId, recId);

// Create the tablekey
AxTableDataKey tableKey = AxTableDataKey.Create(tblMetadata, dictRecId, null);

// Finally create the context and add it to tne menu item
AxTableContext context = AxTableContext.Create(tableKey);
AxUrlMenuItem menuItem = new AxUrlMenuItem("[NAME_URL_MENU_ITEM] ");
menuItem.MenuItemContext = context;

Comments