function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
hoomelhoomel 

Wildcard for ID in SOQL Query

Hello,

I am currently trying to build a quite big soql query.

This query should result in a list of custom objects which are filtered by values entered by a user via a form i built.

There are two lookup fields in this form where the user can select the correlating account and one other custom object.

I am getting the IDs from the fields as ID type so that there is no problem with 15 vs 18-character IDs.

The problem is now that the form should be exclusively searching for values in those fields that were entered by the user.

 

Right now, if i don't select a value in both fields, the query results in an error.

Here is my method executing the query:

 

public PT_System__c ptsystem;
public Rental__c rental;
public String accString; public Id searchAccount; public String sysString; public Id searchSystem;

public List<Rental__c> getFilteredAnlagen(){
           if(rental.PT_System__c != null){
               sysString = rental.PT_System__c;
               searchSystem = (Id)sysString;
               }else{searchSystem = '%';}
           if(rental.account__c != null){
               accString = rental.account__c;
               searchAccount = (Id)accString;
           }else{searchAccount = '%';}
           if(String.valueOf(rental.Start__c) != '' || rental.Start__c != null){
               searchStart = String.valueOf(rental.Start__c);
           }else{searchStart = '%'+String.valueOf(rental.Start__c);}
                  return [select Id, Name
                          FROM Rental__c
                          WHERE PT_System__r.RecordType.Name = 'Rental System'
                          AND PT_System__r.Id = :searchSystem
                          AND Account__c = :searchAccount
                          order by name];
       }

 As you can see, i used the '%' as a wildcard like i would use it for a string, which obviously is not working.

How can i check, if the related fields are selected and only search for those entries in the query when they are.

Or is there any other wildcard character that can be used for an ID?

The problem is, that there are a couple more fields in the form that should be able to be searched in without having to select an account or System.

 

I would appreciate any help with this, as i am totally stuck with it right now.

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

All Answers

bob_buzzardbob_buzzard

Could you not just remove the Account__c criteria from the SOQL query if the user hasn't specified a value?

 

When I've done these in the past, I've constructed the query dynamically based on the information the user has supplied - if they have left a field blank I don't use the criteria (which effectively includes all records).

hoomelhoomel

Could you explain how you did that exactly?

I did think about that but I couldn't figure out how to achieve that.

bob_buzzardbob_buzzard
This was selected as the best answer