2

I am trying to search using dynamic finders by two fields which are status and OpenOn(date). This query is working well and fine:

 render(view:'list', model:[incidentInstanceList:Incident.findAllByStatusIlikeAndOpenOnGreaterThan("closed",new Date()-1,[sort:"id",order:"desc"])])

But now I'm trying to search with dynamic finders by three fields, i.e. UserId,status and OpenOn(date):

 render(view:'list', model:[incidentInstanceList:Incident.findAllByStatusIlikeuserIdIlikeAndOpenOnGreaterThan("closed","${session.user.userId}",new Date()-1,[sort:"id",order:"desc"])])

This query does not work properly and shows an error:

No signature of method: app.Incident.findAllByStatusIlikeuserIdIlikeAndOpenOnGreaterThan() is applicable for argument types: (java.lang.String, org.codehaus.groovy.runtime.GStringImpl, java.util.Date, java.util.LinkedHashMap) values: [closed, tt10004, Wed Aug 24 15:12:21 IST 2011, [sort:id, order:desc]] Possible solutions: findAllByStatusIlikeuserIdIlikeAndCreatedOnGreaterThan(java.util.List) 

Please guide me to solve this problem.

bezmax
  • 25,562
  • 10
  • 53
  • 84
manu
  • 313
  • 5
  • 13

4 Answers4

7
def incidentInstanceList = {

    def criteria = Incident.createCriteria()

    def results = criteria {
        and {
            user {
                like('userId', "${session.user.userId}").toString()
            }
            like('status',  "Closed" ) 
            gt('closedOn',new Date()-1)
            sort("id", "desc")
        }  

    } 

    render(view:'list', model:[ incidentInstanceList: results, incidentInstanceTotal: Incident.count()])        
}
Charles Wood
  • 864
  • 8
  • 23
manu
  • 313
  • 5
  • 13
4

At the moment, you can only use dynamic finders with a maximum of two criteria. If you need to use more, you should consider using either Criteria or the HQL.

This answer goes into more detail on your question.

Community
  • 1
  • 1
gotomanners
  • 7,808
  • 1
  • 24
  • 39
1

Probably something like this, which, by the way, seems more legible for me:

def incidentInstanceList = 
    Incident.createCriteria().list
    {
        eq(status, 'closed')
        ilike(userId, "${session.user.userId}")
        gt(openOn, new Date()-1)
        sort("id", "desc")
    }
Deigote
  • 1,721
  • 14
  • 15
0

Besides i think there is no need to put second parameter "" just do session.user.userId (i assume u don`t need userId to be String here) createCriteria is what u need :)

Maciej
  • 45
  • 6