Hello DAX DEV,
Here’s a tool to automatically create the Find method on tables based on the Primary Unique index, it appears in the context menu of the code editor in Ax 2012.
This is how it works, first, you have to make sure your table has a unique index:
Then you create a new method on the table, select all the text and execute the function FindByPrimaryIndex in the Context menu.
This new tool will pop-up dialogs, as the images below, to each unique index in a loop in case there’s more than one until the developer chooses one of the indexes [By pressing “Yes” on the dialog].
If there are no unique indexes, or the user rejects all the possible indexes, the solution will propose to create the find by the RecId field.
1.
2.
3.
I’ve generated the three possibilities of Find methods for this scenario and the results are:
find by index CarIdx_UniqueId
/// <summary> | |
/// Finds the specified record in the <c>CarTable</c> table. | |
/// </summary> | |
/// <param name="_carId"> | |
/// The CarId of the <c>CarTable</c> record to find. | |
/// </param> | |
/// <param name="_forupdate"> | |
/// A Boolean value that indicates whether to read the record for update; optional. | |
/// </param> | |
/// <param name="_concurrencyModel"> | |
/// The table selection ConcurrencyModel; optional. | |
/// </param> | |
/// <returns> | |
/// A record in the <c>CarTable</c> table; otherwise, an empty record. | |
/// </returns> | |
// Begin : Axaptahut, DevTools | |
public static CarTable find(AccountNum _carId, boolean _forupdate = false, | |
ConcurrencyModel _concurrencyModel = ConcurrencyModel::Auto) | |
{ | |
CarTable carTable; | |
carTable.selectForUpdate(_forupdate); | |
if (_forupdate && _concurrencyModel != ConcurrencyModel::Auto) | |
{ | |
carTable.concurrencyModel(_concurrencyModel); | |
} | |
select firstonly carTable | |
where carTable.CarId == _carId; | |
return carTable; | |
} | |
// End: Axaptahut, DevTools |
find by index CarIdx_UniqueCombination
/// <summary> | |
/// Finds the specified record in the <c>CarTable</c> table. | |
/// </summary> | |
/// <param name="_carId"> | |
/// The CarId of the <c>CarTable</c> record to find. | |
/// </param> | |
/// <param name="_brand"> | |
/// The Brand of the <c>CarTable</c> record to find. | |
/// </param> | |
/// <param name="_forupdate"> | |
/// A Boolean value that indicates whether to read the record for update; optional. | |
/// </param> | |
/// <param name="_concurrencyModel"> | |
/// The table selection ConcurrencyModel; optional. | |
/// </param> | |
/// <returns> | |
/// A record in the <c>CarTable</c> table; otherwise, an empty record. | |
/// </returns> | |
// Begin : Axaptahut, DevTools | |
public static CarTable find_Combination(AccountNum _carId, | |
EcoResAttributeTypeName _brand, boolean _forupdate = false, | |
ConcurrencyModel _concurrencyModel = ConcurrencyModel::Auto) | |
{ | |
CarTable carTable; | |
carTable.selectForUpdate(_forupdate); | |
if (_forupdate && _concurrencyModel != ConcurrencyModel::Auto) | |
{ | |
carTable.concurrencyModel(_concurrencyModel); | |
} | |
select firstonly carTable | |
where carTable.CarId == _carId | |
&& carTable.Brand == _brand; | |
return carTable; | |
} | |
// End: Axaptahut, DevTools |
find by RecId
/// <summary> | |
/// Finds the specified record in the <c>CarTable</c> table. | |
/// </summary> | |
/// <param name="_recId"> | |
/// The RecId of the <c>CarTable</c> record to find. | |
/// </param> | |
/// <param name="_forupdate"> | |
/// A Boolean value that indicates whether to read the record for update; optional. | |
/// </param> | |
/// <param name="_concurrencyModel"> | |
/// The table selection ConcurrencyModel; optional. | |
/// </param> | |
/// <returns> | |
/// A record in the <c>CarTable</c> table; otherwise, an empty record. | |
/// </returns> | |
// Begin : Axaptahut, DevTools | |
public static CarTable findRecId(RecId _recId, boolean _forupdate = false, | |
ConcurrencyModel _concurrencyModel = ConcurrencyModel::Auto) | |
{ | |
CarTable carTable; | |
carTable.selectForUpdate(_forupdate); | |
if (_forupdate && _concurrencyModel != ConcurrencyModel::Auto) | |
{ | |
carTable.concurrencyModel(_concurrencyModel); | |
} | |
select firstonly carTable | |
where carTable.RecId == _recId; | |
return carTable; | |
} | |
// End: Axaptahut, DevTools |
To add this functionality the following method must be placed in the Editor Scripts class.
Follow us and get exclusive AX DEV content weekly
Hope it might be helpful,
Thanks,
Felipe Nogueira