Search code examples
grailsdynamic-finders

Conditional Dynamic finder in grails


so i require something like the dynamic finder should change as per the condition, let me explain by code what i mean

the below code find all employee by status and lastdateattended

    def employee = Employee.findAllByStatusAndLastDateAttended("INA",dateObject)

now i have two fields in Employee LastDateAttended and LastDateSigned , and now i want that if a record does not have LastDateAttended, then it should find by LastDateSigned , otherwise LastDateAttended, so another condition is like

      def employee = Employee.findAllByStatusAndLastDateAttended("INA",dateObject)

and i somehow wants to join and use both the query based on a condition , can it be achieved ?, if possible please help


Solution

  • Employee.list().findAll { emp-> 
         emp.status == "INA" && ( emp.lastDateAttended!=null?
             emp.lastDateAttended.equals(dateObject):emp.lastDateSigned.equals(dateObject)
           ) 
    }