Updating Data in X++

In X++, you can update one or more records in a database table using SQL-like statements, either directly in code or through interactive execution.

update Method

The update() method is used to update the current buffer record. It also refreshes relevant system fields automatically. A where clause can optionally be included to limit updates to rows that meet specific criteria.

Example:
The below snippet updates a single row in the CustTable where the AccountNum is '4000', setting CreditMax to 5000.

				
					CustTable custTable;
ttsBegin;
    select forUpdate custTable
        where custTable.AccountNum == '4000';
    custTable.CreditMax = 5000;
    custTable.update();
ttsCommit;
console.log( 'Code is Poetry' );CustTable custTable;
ttsBegin;
    select forUpdate custTable
        where custTable.AccountNum == '4000';
    custTable.CreditMax = 5000;
    custTable.update();
ttsCommit;

				
			

doUpdate Method

The doUpdate() method is similar to update(), but it bypasses any custom logic, such as overridden update() methods, event handlers (onUpdating, onUpdated), and Chain-of-Command logic. Use this method cautiously—it’s not recommended for general use as it ignores business rules and database events.

Example:
Updates the CreditMax by adding 1000 for the record where CreditMax == 3000:

				
					CustTable custTable;
ttsBegin;
select forUpdate custTable
    where custTable.CreditMax == 3000;
if (custTable)
{
    custTable.CreditMax += 1000;
    custTable.doUpdate();
}
ttsCommit;

				
			

update_recordset Statement

The update_recordset keyword is designed for bulk updates. It modifies multiple records in a single database operation, reducing round-trips and improving performance. It’s similar to SQL’s UPDATE ... SET syntax and works server-side without loading individual records into memory.

Examples:Increment CreditMax by 1000 where CreditMax > 0
				
					CustTable custTable;
ttsBegin;
update_recordset custTable
    setting CreditMax = custTable.CreditMax + 1000
    where custTable.CreditMax > 0;
ttsCommit;

				
			
Update multiple fields
				
					CustTable custTable;
ttsBegin;
update_recordset custTable
    setting
        CreditMax = custTable.CreditMax + 1000,
        AccountStatement = CustAccountStatement::Always
    where custTable.CreditMax > 0;
ttsCommit;

				
			
Use joins to update based on data from related tables
				
					TableEmployee tabEmpl;
TableDepartment tabDept;
TableProject tabProj;

update_recordset tabEmpl
    setting
        currentStatusDescription = tabDept.DeptName + ", " + tabProj.ProjName
join tabDept
    where tabDept.DeptId == tabEmpl.DeptId
join tabProj
    where tabProj.ProjId == tabEmpl.ProjId;

				
			

This approach enables efficient and scalable updates in Dynamics 365 Finance and Operations using X++.

Sharing Is Caring:

Leave a Comment

Top 30 JavaScript Interview Questions and Answers for 2024 Top 5 Front-End Web Development