行のオブジェクト・タイプの決定
グローバル・ヘルパー関数を記述する場合、ビジネス・オブジェクト・データ行を引数として受け入れると便利です。
関数の内部で、渡された行のオブジェクト・タイプを決定する必要がある場合は、objectName()
関数を使用します。
たとえば、既存の行から選択したデータをコピーして、渡された行と同じタイプの新規行を作成する必要がある場合に便利です。
// Global Function:
// ---------------
// Object duplicateRow(existingRow Object,
// fieldNamesToCopy List)
// Find the name of the object for the existingRow
def rowObjectName = objectName(existingRow)
// In order to create a new row of the same type, we
// need a new view object for the right business object
// whose name we just determined above.
def vo = newView(rowObjectName)
// Setup a map to hold any attribute names whose values
// we want to copy from the existing ro
def attrs = [:]
// If any fieldNamesToCopy were specified, prepare a
// map containing the field names and values from the
// existing row to use while creating the new row.
// Groovy elegantly handles the case where the
// fieldNamesToCopy might is null, keep code simple.
for (fieldName in fieldNamesToCopy)
{
attrs[fieldName] = existingRow[fieldName]
}
// Now create and return a new row using that view object
// passing in the map of fields whose values we've been
// asked to copy from the existing row.
return vo.createAndInitRowFromMap(attrs)
duplicateRow()
グローバル関数を定義すると、既存の行を複製する必要があるアプリケーション内の任意の場所で使用でき、オプションで既存の行から1つ以上の属性をコピーできます。 たとえば、次に示すように、既存のOrder_c
オブジェクトを複製するOrder_c
カスタム・オブジェクトの「更新前」トリガーにコードを記述し、フィールドCustomerId_c
およびPrimaryShippingAddress_c
のみを新しく作成した順序にコピーできます:
// Before Update trigger on Order_c
// Call an object function to determine any backordered items
def backorderedItems = determineBackorderedItems()
if (backorderedItems)
{
// Use adf.source to pass this current Order_c object
def newOrder = adf.util.duplicateRow(adf.source,
['CustomerId_c',
'PrimaryShippingAddress_c'])
// Call an object function to add backordered items
// to the newly-created order.
newOrder.addItems(backorderedItems)
// Call an object function to remove backordered items
// from the current order
removeItems(backorderedItems)
}
もちろん、最初の引数として他のOrder_c
行を渡すこともできます。 次の例では、queryRow()
グローバル・ヘルパー関数を使用して、特定の製品IDを含む現在の顧客の最新の出荷済オーダーを検索し、見つかった場合はそのオーダーをduplicateRow()
関数に渡しました。
// Assume custId variable holds current customer id
// and prodId variable holds current product id
def recentOrder = adf.util.queryRow(
select: 'CustomerId_c,PrimaryShippingAddress_c',
from: 'Order_c',
where: """
CustomerId_c = :CurCust
and OrderStatus_c = 'SHIPPED'
and OrderLinesCollection_c.ProductId_c = :CurProd
""",
orderBy: 'OrderDate_c desc',
binds: [CurCust: custId, CurProd: prodId])
if (recentOrder) {
// Pass the recent order we found above into duplicateRow()
def newOrder = adf.util.duplicateRow(recentOrder,
['CustomerId_c',
'PrimaryShippingAddress_c'])
// etc.
}