I have an Apex application, that contains several drop down bars that allow users to select various values to query the attached database with. Basically the query is becoming huge due to the conditions placed within it. The conditions are such that it checks each value within all the dropdowns or whether they’re null etc. The question I have is there anyway of simplifying the query so that i dont have to write every possible combination of data being input. Like this field is null this one isnt, now this one is etc.
An example of how the conditions look –
OR
(plant_type.plant_type = :P1_PLANT_TYPE AND plant.plant_name = :P1_PLANT_NAME AND :P3_SOIL IS NULL AND :P3_SUNLIGHT IS NULL)
OR
(plant_type.plant_type = :P1_PLANT_TYPE AND :P1_PLANT_NAME IS NULL AND :P3_SOIL IS NULL AND :P3_SUNLIGHT IS NULL)
OR
(sunlight_conditions.condition_details = :P3_SUNLIGHT AND :P3_SOIL IS NULL AND plant_type.plant_type = :P1_PLANT_TYPE AND plant.plant_name = :P1_PLANT_NAME)
OR
(soil_conditions.soil_condition = :P3_SOIL AND :P3_SUNLIGHT IS NULL AND plant_type.plant_type = :P1_PLANT_TYPE AND plant.plant_name = :P1_PLANT_NAME)
A better way would be to include every query condition only once, like this:
Of course, it depends if you want an OR condition between them or and AND condition. Usually it’s AND and so my answer will help you.