Skip to content

View Helpers

includedInObject()

includedInObject() — returns boolean

Available in: controller Category: Form Association Functions

Used as a shortcut to check if the specified IDs are a part of the main form object. This method should only be used for hasMany associations.

NameTypeRequiredDefaultDescription
objectNamestringyes—Name of the variable containing the parent object to represent with this form field.
associationstringyes—Name of the association set in the parent object to represent with this form field.
keysstringyes—Primary keys associated with this form field. Note that these keys should be listed in the order that they appear in the database table.
// 1. Check whether a customer is already subscribed to a particular publication via a hasMany join
// (Note: keys should be listed in the order they appear in the join table columns)
if (includedInObject(objectName="customer", association="subscriptions", keys="#customer.key()#,#swimsuitEdition.id#")) {
    writeOutput("Already subscribed.");
} else {
    writeOutput("Not yet subscribed.");
}

// 2. Use the return value to find the position of the associated object in the array
position = includedInObject(objectName="order", association="lineItems", keys="#lineItem.key()#");
// Returns false when not found, or the 1-based index position when found
// position -> 3 (the associated lineItem is at index 3 in order.lineItems)

// 3. Guard against adding duplicate associations before creating a new join record
if (!includedInObject(objectName="student", association="courses", keys="#course.key()#")) {
    student.courses = ArrayAppend(student.courses, course);
}