I need to create a findAll
criteria in Grails, but I have 4 fields to search. I just need to search by those fields if a specific variable is not null.
For example, if all variables are not null, I'll search by all the fields like the following:
list = MyObject.findAll {
'in'("job", jobList)
'in'("businessUnit", businessUnitList)
'in'("company", companyList)
eq("regularTime", params.regularTime)
}
What I want to do is something like this:
regularTimeList = RegularTime.findAll {
if(params.job != null) 'in'("job", jobList)
if(params.businessUnit != null) 'in'("businessUnit", businessUnitList)
if(params.company != null) 'in'("company", companyList)
if(params.regularTime != null) eq("regularTime", params.domainClassSearchFilters.regularTime)
}
It's not working so I'd like to know if there is a way to do that because right now I had to create a lot of ifs to verify them... I need one if to each scenario... Like:
If job
is not null and company
is not null, one findAll. If company
is not null and regularTime is not null, another findAll
.
I don't know what happened, but the way Above didn't work for me... I solved it using criteria.
list= MyObject.createCriteria().list {
if(params.job != null && "" != params.domainClassSearchFilters.job )
'in'("job", jobList)
if(params.businessUnit != null && "" != params.businessUnit)
'in'("businessUnit", businessUnitList)
if(params.company != null && "" != params.company)
'in'("company", companyList)
if(params.regularTime != null && "" != params.regularTime && params.regularTime.isDouble())
eq("regularTime", Double.parseDouble(params.regularTime))
}