34
Views
5
Comments
SQL Query remove WHERE conditions based on values of structure attributes passed
Question

If I have an entity defined that has two columns

  • name (text)
  • url (text)

I have a server action defined called search. Search takes one input parameter, a structure with two attributes

  • name
  • url

I want to return all rows from the entity where the name and url match the passed structure attributes. However, if either field is empty, I do not want to use that field to filter the results.

If my entity has two rows

  • name = "Jim", Url="www.outsystems.com"
  • name="Frank", Url="www.google.com

The caller of my server action passes name = "Jim", Url="", I only want the Jim record returned. If they pass both as empty strings, I want to return both records.


What is the easiest way to accomplish this in Out systems? The number of structure attributes is not small, so would prefer a solution that uses Aggregates or SQL. I want a way if a structure attribute is not passed (empty), to remove that field from the WHERE clause.

2025-01-09 14-56-57
IQ78

hi you use expression with clause Like:

ContainerCollectionLocation.Location like % + Location_Input + % 

For complex one and performance sake, I'd rather use Advance query.

regards

2024-09-12 02-43-38
Deepsagar Dubey

Hi @Gregory Pierson
Good Day!!

Yes, you can use OS aggregate to perform this search, just go to filter and add these 2 filters.

Search.name = "" or YourEntityName.Name like "%"+Search.name+"%"

Search.url = "" or YourEntityName.Url like "%"+Search.url+"%"

So its check if anyone param is empty then no filter will be apply else it'll search by given input value.

I hope it'll help you.

Thanks
Deep

2023-04-16 15-25-31
Krishnanand Pathak

Hi Gregory,

Add the below filter to the aggregate

(Search.name = "" and Search.url = "") or Entity.Name  = Search.name or

Entity.Url = Search.url

Regards

Krishnanand Pathak 

2024-01-04 09-21-21
Venkatesaiya

HI Gregory,
Use this condition in you filter ,

(Entity.name like "%"+name(search name parameter)+"%" or name(search name parameter) =NullTextIdentifier() ) and 
(Entity.url like "%"+URL(search URL  parameter) +"%" or URL(search URL  parameter)=NullTextIdentifier() )

regards 
venkatesaiya


2025-10-18 11-13-53
Ramesh subramanian

Hi Gregory ,

Still can't able fix this issue? Deepsagar Dubey ,Krishnanand Pathak  and Venkatesaiya made correct way. Please check and and do it. still having issue. please share the OML. we are going to help you.

Thanks,

Ramesh

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.