Set Field Values in Bulk
Wherever possible in your code, for best performance set the values of all fields in a row in a single call to the setAttributeValuesFromMap() function.
Using these bulk-assignment functions saves processing time and can eliminate avoidable queries related to your Dynamic Choice List and Fixed Choice List attribute validation when compared to the equivalent job performed one field at a time.
For example, the following code examples sets the values of five fields of an existing staff
member row. The code is using the
queryRow()
helper function described in
Simplifying Business Logic Queries to find the
row by employee
id// Find an existing staff member by the indexed primary key field EmployeeId
// Then bulk-assign 5 field values whose names are also included in the
// view object's select list to avoid unnecessary "fault-in" queries.
def emp = adf.util.queryRow(
select:'EmployeeId,Email,CarMake,CarModel,Vacation,AccrualDate',
from: 'StaffMember',
where: 'EmployeeId = :id',
binds: [id: 123456789])
if (emp) {
emp.setAttributeValuesFromMap(
Email: emp.Email.replace('old.org','new.org'),
CarMake: 'VW',
CarModel: 'GLF',
Vacation: 160,
AccrualDate: today())
}
When creating a new row, you can accomplish the same bulk assignment task using
the createAndInitRowFromMap()
function. The following example creates a new
staff member assigning all fields in
bulk:def emps = newView('StaffMember')
// Insert a new staff member, setting all necessary fields in bulk
emps.insertRow(emps.createAndInitRowFromMap(
Email: 'jane.barnes@example.org',
CarMake: 'AUD',
CarModel: 'A8',
Vacation: 200,
AccrualDate: today()))
Both examples in this section illustrate Groovy's support for removing the square brackets
around a literal Map
passed inline to a function with a leading
Map
argument. To learn more about how your own functions can leverage this
feature, see Using Optional, Named Method Arguments.
When writing generic helper code, if you find it more convenient to process the field
names to assign and corresponding values to assign in separate lists, then
consider using the
setAttributeValues()
function. The example below shows how
it may fit your situation better than setAttributeValuesFromMap()
. This
alternative function accomplishes the same performance
improvement.// void doBulkAssignment(Object row, List fieldNames, List fieldValues)
// accepting row to assign, field names and field values as separate Lists
if (fieldNames.size() == fieldValues.size()) {
row.setAttributeValues(fieldNames, fieldValues)
}
else {
adf.util.error("Must supply same number of fields and values to assign!")
}