
There are several different ways to create records using X++.
Insert Types
Single Insert Pattern
This is an example of a single insert pattern in a loop; open tran, set values, call insert(), close tran, loop
for(int i = 0; i <= this.parmRecordCount(); i++)
{
ttsbegin;
SalesPool.SalesPoolId = int2Str(i);
SalesPool.Name = int2Str(i);
SalesPool.insert();
ttscommit;
}
Multiple Insert Pattern
This is an example of a multi insert pattern with a loop; open tran, set values, call insert(), loop, close tran
ttsbegin;
for(int i = 0; i <= this.parmRecordCount(); i++)
{
SalesPool.SalesPoolId = int2Str(i);
SalesPool.Name = int2Str(i);
SalesPool.insert();
}
ttscommit;
Insert_RecordSet Pattern
This in an example of a Insert_RecordSet pattern; insert into target from select from source
ttsbegin;
insert_recordset SalesPool (SalesPoolId, Name)
select SalesPoolId, Name from tmpSalesPool;
ttscommit;
Query Insert_RecordSet Pattern
This is an example of a query based Insert_RecordSet pattern; create query, map fields, insert into target from select from source. This is a variation of the previous pattern is mostly here for completeness.
query = new Query();
qbds_tmpSalesPool = query.addDataSource(tableNum(tmpSalesPool));
fldList_tmpSalesPool = qbds_tmpSalesPool.fields();
fldList_tmpSalesPool.addField(fieldNum(tmpSalesPool, SalesPoolId));
fldList_tmpSalesPool.addField(fieldNum(tmpSalesPool, Name));
fieldMapping = new Map(Types::String, Types::Container);
fieldMapping.insert(fieldStr(SalesPool, SalesPoolId), [qbds_tmpSalesPool.uniqueId(), fieldStr(tmpSalesPool, SalesPoolId)]);
fieldMapping.insert(fieldStr(SalesPool, Name), [qbds_tmpSalesPool.uniqueId(), fieldStr(tmpSalesPool, Name)]);
ttsbegin;
query::insert_recordset(SalesPool, fieldMapping, query);
ttscommit;
RecordInsertList Pattern
This is an example of using a RecordInsertList object to insert records; declare, instantiate, open tran, set values, add, insert collection, close tran
RecordInsertList SalesPoolList;
SalesPoolList = new RecordInsertList(tableNum(SalesOrderPoolEntity));
SalesOrderPoolEntity SalesOrderPoolEntity;
ttsbegin;
for(int i = 0; i <= this.parmRecordCount(); i++)
{
SalesOrderPoolEntity.clear();
SalesOrderPoolEntity.initValue();
SalesOrderPoolEntity.PoolId = int2Str(i);
SalesOrderPoolEntity.PoolName = int2Str(i);
SalesPoolList.add(SalesOrderPoolEntity);
}
SalesPoolList.insertDatabase();
ttscommit;
RecordSortedList Pattern
This is an example of using a RecordSortedList object to insert records; declare, instantiate, open tran, set values, ins, insert collection, close tran
RecordSortedList SalesPoolList;
SalesPoolList = new RecordSortedList(tableNum(SalesPool));
SalesPoolList.sortOrder(fieldName2Id(tableNum(SalesPool), 'SalesPoolId'));
SalesPool SalesPool;
ttsbegin;
for(int i = 0; i <= this.parmRecordCount(); i++)
{
SalesPool.SalesPoolId = int2Str(i);
SalesPool.Name = int2Str(i);
SalesPoolList.ins(SalesPool);
}
SalesPoolList.insertDatabase(con);
ttscommit;
SysDAInsertObject Pattern
This is an example of using a ysDAInsertObject object to insert records; declare, instantiate, add fields, add projection, add query, open tran, exec statement, close trans
var insertObject = new SysDaInsertObject(SalesPool);
insertObject.fields()
.add(fieldStr(SalesPool, SalesPoolId))
.add(fieldStr(SalesPool, Name));
tmpSalesPool source;
var qe = new SysDaQueryObject(source);
var s1 = qe.projection()
.Add(fieldStr(tmpSalesPool, SalesPoolId))
.Add(fieldStr(tmpSalesPool, Name));
insertObject.query(qe);
var insertStmt = new SysDaInsertStatement();
this.startTimer();
ttsbegin;
insertStmt.executeQuery(insertObject);
ttscommit;
Leave a comment