機械翻訳について

パラメータ化された名前を使用したフィールド値の操作

再利用可能なコードを記述するときに、オブジェクト関数で異なるフィールドに対して同じ操作を実行する必要がある場合は、フィールド名をパラメータ化できます。

String型の関数パラメータを定義することから始めます。実行時の値は、現在のオブジェクトのフィールドの名前です。 次に、コードでパラメータ化されたフィールドの値にアクセスする必要がある場合、getAttribute(fieldNameParam)をコールします。 そのフィールドに新しい値を割り当てるには、setAttribute(fieldNameParam,newValue)をコールします。 いずれの場合も、渡されたフィールド名パラメータの値が現在のオブジェクトの一部のフィールドの名前と一致しない場合、エラーを示すためにNoDefExceptionがスローされます。

フィールドの値が渡された最大値よりも小さい場合にのみ名前が渡される数値フィールドの値を増分する、conditionalIncrement()という名前のオブジェクト関数の次の例について考えてみます:

// Object function: void conditionalIncrement(fieldName String, maxValue Long)
// ---------------
def fieldValue = getAttribute(fieldName)
if (fieldValue < maxValue) {
  setAttribute(fieldName, fieldValue + 1)
} 

最初の行では、名前が渡されるフィールドの値を格納するfieldValue変数を定義します。 その値がmaxValueより小さい場合、3行目では、現在の値より1大きい新しい値がフィールドに割り当てられます。 conditionalIncrement()などのオブジェクト関数を定義すると、同じオブジェクト上のすべてのGroovyスクリプトがそれを起動して、適切な引数値を渡すことができます。 たとえば、あるスクリプトでは、値が500未満の場合はUsageCountというフィールドの値を増分する必要があるとします:

// Increment the usage count if it is less than 500
conditionalIncrement('UsageCount', 500)

別のスクリプトでは、値が1000未満の場合は、DocumentVersionNumberフィールドの値を増分する必要があるとします。 同じオブジェクト関数を使用できます : フィールド名と最大値パラメータに異なる値を渡すだけです:

// Increment the document version number if it is less than 1000
conditionalIncrement('DocumentVersionNumber', 1000)

もちろん、getAttribute()関数およびsetAttribute()関数は最初の引数としてリテラルString値を受け入れることもできるため、次のような条件ロジックを理論的に記述できます:

// Ensure document is not locked before updating request-for-approval date
// NOTE: more verbose get/setAttribute() approach
if (getAttribute('DocumentStatus') != 'LOCKED') {
  setAttribute('RequestForApprovalDate', today())
}

ただし、前述の例では、評価されて割り当てられるフィールドの名前がパラメータ変数またはローカル変数から取得されない場合、かわりに次の同等のコードを記述する方が簡単で読みやすくなります:

// Ensure document is not locked before updating request-for-approval date
// NOTE: More terse, elegant direct field name access
if (DocumentStatus != 'LOCKED') {
  RequestForApprovalDate = today()
}

getAttribute()およびsetAttribute()関数は、単独で起動されると、現在のオブジェクトに対して動作します。 ただし、ビジネス・オブジェクトRowを操作しているスクリプト・コード内の任意の場所では、オブジェクト関数の次の例に示すように、その特定の行でこれらの関数をコールすることもできます。 また、newView()関数に渡されるオブジェクトの名前もパラメータ化されます:

// Object function: String getRowDescription(objectName String, displayFieldName String, id Long)
// ---------------
// Create a new view object to work with the business object whose name is
// passed in the objectName parameter
def view = newView(objectName)
// Find the row in that view whose key is given by the value of the id parameter
def rows = view.findByKey(key(id),1)
// If we found exactly one row, return the value of the display field name on
// that row, whose field name is given by the value in the displayFieldName parameter
return rows.size() == 1 ? return rows[0].getAttribute(displayFieldName) : null

このような関数を定義すると、オブジェクト内の任意のスクリプトからその関数を起動して、作業が必要な様々なオブジェクトの表示フィールド値にアクセスできます:

// Get RecordName of the Task object with key 123456
def taskName = getRowDescription('Task','RecordName',123456)
// Get the Name of the Territory object with key 987654
def optyName = getRowDescription('Territory','Name',987654)

getAttribute()またはsetAttribute()を使用して関連オブジェクトのフィールド値にアクセスする場合、最初の引数は、それを起動するオブジェクトの単一フィールドの名前を表す必要があることに注意してください。 たとえば、TroubleTicket?.Statusは現在のActivityオブジェクトの単一フィールドの名前ではないため、setAttribute()関数を使用して、アクティビティの親TroubleTicketオブジェクトのStatusフィールドを設定する正しい方法ではありません:

// Assume script runs in context of an Activity object (child of TroubleTicket)
// INCORRECT way to set a parent field's value using setAttribute()
setAttribute('TroubleTicket?.Status', 'Open')

かわりに、まず関連オブジェクトにアクセスし、ローカル変数に格納してください。 その後、次のように関連オブジェクトにフィールドを割り当てることができます:

// Assume script runs in context of an Activity object (child object TroubleTicket)
// First access the parent object
def parentTicket = TroubleTicket
// Then call the setAttribute on that parent object
parentTicket?.setAttribute('Status', 'Open')