Field paths contain more than one SearchableField. Use FieldPath to construct a field when the record does not have direct access to the field you want to search.
For example, if you want to search with the username of an appointment attendee, you cannot access the user directly from the appointment. The Appointment class contains the static ATTENDEES field. Once you have access to the attendees, you can access the static USER field with the AppointmentAttendee class. After you have access to the user, you can access the static USERNAME field with the User class. As a result, the FieldPath is (Appointment.ATTENDEES).add(AppointmentAttendee.USER).add(User.USERNAME);
Before you add the operator, the field path looks like the following code.
FieldPath fieldPath = new FieldPath(Appointment.ATTENDEES).add(AppointmentAttendee.USER).add(User.USERNAME); |
The following code includes an example of a search with two criteria that use field paths.
AppointmentService appointmentService = platform.getAppointmentService();
// Create a path to the Appointment's Project's Created On FieldPath projectCreatedOn = new FieldPath(Appointment.PROJECT).add(Project.CREATED_ON);
// Create a path to the Appointment's Attendee's Attendance Type FieldPath attendeeAttendanceType = new FieldPath(Appointment.ATTENDEES).add(AppointmentAttendee.ATTENDANCE_TYPE);
// Search for appointments related to projects created in the last thirty days RelativeDateCriterion relativeDateCriterion = new RelativeDateCriterion(projectCreatedOn).withinLast(30);
// Search for appointments that have attendees that will not attend EnumerationCriterion<AttendanceType> enumerationCriterion = new EnumerationCriterion<AttendanceType>(attendeeAttendanceType).equalTo(AttendanceType.WILL_NOT_ATTEND);
// Criterions constructed from field paths behave exactly like criterions constructed from fields, and can be combined just the same SearchCriteria criteria = new SearchCriteria(relativeDateCriterion, enumerationCriterion);
// Execute search List<Appointment> appointments = appointmentService.search(criteria); |
You can also use field paths with custom field searching, as shown in the following sample.
ProjectService projectService = platform.getProjectService(); CategoryService categoryService = platform.getCategoryService();
// Field Paths can also point to custom fields CustomField<String> customField = categoryService.getCustomField("CONT", "custom field"); FieldPath contactCustomField = new FieldPath(Project.CONTACT).add(customField);
// Search for projects based on contact custom field StringCriterion stringCriterion = new StringCriterion(contactCustomField).equalTo("value"); SearchCriteria criteria = new SearchCriteria(stringCriterion);
// Execute search List<Project> projects = projectService.search("DISP", criteria); |