AX 2012 – RecordInsertList (basic sample)

Sample code….

Bad Practice:

RandomTable randomTableBuffer;

// Imagine this loop returns 10000 records.
while select randomTableBuffer where randomTableBuffer.RandomField1 == "1"
{
randomTableBuffer.RandomField2 == "2";
randomTableBuffer.insert();
}

This would insert a record in the database 10000 times. Of course this would work just perfectly fine but would give you horrible performance.

To avoid performance issues you can use a list of records and insert the records all at once.

Best Practice:

RandomTable randomTableBuffer;
RecordInsertList recordList = new RecordInsertList(tableNum(RandomTable));

// Imagine this loop returns 10000 records.
while select randomTableBuffer where randomTableBuffer.RandomField1 == "1"
{
randomTableBuffer.RandomField2 == "2";
recordList.add(randomTableBuffer); // Here you add the record currently in the tablebuffer into the array
}
recordList.InsertDatabase(); //Insert the records all at once.

To read it in more detail a nice blog post by Sávio Mendes de Figueiredo can be found here.

Leave a Reply