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
Sangeet kaseraSangeet kasera 

How to extract list of values from comma seperated <apex:input> type values using SOQL??

I Have one  <apex:inputText value="{!searchKey}" html-placeholder="Enter Text to search"/>

User-added image


Where User Enter's Comma Seperated Values like Record ID and Name(Ex:- 01I2w000000eRuaEAE, Raj) both are different User Id and Name, I want to extract both user's data using SOQL.

Below is my Apex code-

 public List<Subscription__c> subsD {get;set;}
    public String searchKey {get;set;}
    //List<String> searchData = searchKey.split(',');
    public Id paramValue3 {get;set;} 
    public boolean paramValue4 {get;set;}
    
    public void perform(){
        List<String> searchData = new List<String>();
        searchData = searchKey.split(',');
        string searchquery='SELECT id, SubscribedBy__c, ObjectSubscription__c,Record_Id__c,Activated__c FROM Subscription__c where SubscribedBy__r.Name like \'%'+ searchData +'%\' OR SubscribedBy__r.Email like \'%'+searchData+'%\' Limit 10';
        subsD= Database.query(searchquery);
    }

I am getting query in debug like this-

14:35:38:021 SOQL_EXECUTE_BEGIN [13]|Aggregations:0|SELECT id, SubscribedBy__c, ObjectSubscription__c,Record_Id__c,Activated__c FROM Subscription__c where SubscribedBy__r.Name like '%xxx, yyy%' OR SubscribedBy__r.Email like '%(xxx,  yyy)%' Limit 10

Any one Please Help me to get the correect query by which I can Extract data for both
 
Best Answer chosen by Sangeet kasera
David Zhu 🔥David Zhu 🔥
You may try the code below:

 searchData = searchKey.split(',');
string searchquery='SELECT id, SubscribedBy__c, ObjectSubscription__c,Record_Id__c,Activated__c FROM Subscription__c';
string whereClause = ' where ';
for (string s : searchData)
{  
   if (whereClause == ' where ')
  {
     whereClause += ' SubscribedBy__r.Name like \'%'+ s+'%\' OR SubscribedBy__r.Email like \'%'+s+'%\';
   }
   else
   {
     whereClause += ' or SubscribedBy__r.Name like \'%'+ s+'%\' OR SubscribedBy__r.Email like \'%'+s+'%\';
   } 
}
searchquery += whereClause + ' Limit 10';
 subsD= Database.query(searchquery);
 

All Answers

David Zhu 🔥David Zhu 🔥
You may try the code below:

 searchData = searchKey.split(',');
string searchquery='SELECT id, SubscribedBy__c, ObjectSubscription__c,Record_Id__c,Activated__c FROM Subscription__c';
string whereClause = ' where ';
for (string s : searchData)
{  
   if (whereClause == ' where ')
  {
     whereClause += ' SubscribedBy__r.Name like \'%'+ s+'%\' OR SubscribedBy__r.Email like \'%'+s+'%\';
   }
   else
   {
     whereClause += ' or SubscribedBy__r.Name like \'%'+ s+'%\' OR SubscribedBy__r.Email like \'%'+s+'%\';
   } 
}
searchquery += whereClause + ' Limit 10';
 subsD= Database.query(searchquery);
 
This was selected as the best answer
Sangeet kaseraSangeet kasera
Hi @David Zhu,

Thanks For Reply, It is working fine in some cases and fail in some case I have some query regarding this---

Please find below Screenshot for my query-
User-added image


Also I want to know that If we don't use comma at end like xxxxx@gmail.com, Raj it doesn't give correct data this gives only first user data, 
If we use single name or email with comma and space this give all data present in Subscription__c Object,
Also want to add a validation for this if someone use any other symbol except comma then this should show Error.

Few Same Query is attached in ScreenShot

I want to prevent all these issues Will you please solve my query regarding this.

Regards,
Sangeet
 
David Zhu 🔥David Zhu 🔥
1. You can trim the space before adding to whereClause string.

for (string s : searchData)
{  
   s = s.trim();
   if (s=='')
   {
        continue;
   }


2. You can use regEx to filter out special symbols.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_pattern_and_matcher_using.htm
Sangeet kaseraSangeet kasera
Hi @David, 

I have used above trim function but it gives some error, I also used removeEnd function but still i didn't get the correct data when i am using Sangeet.kasera@grazitti.com, Madhup. Without comma and space at end It gives only first user data. Will you please check my code and make me understand how this will be done.

Below my code is present-

 public void perform(){
        List<String> searchData = new List<String>();
        searchData = searchKey.split(',');
        string searchquery='SELECT id, SubscribedBy__c, ObjectSubscription__c,Record_Id__c,Activated__c FROM Subscription__c';
        string whereClause = ' where ';
        if(searchKey == ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter UserDetail'));
        }
        else{
            for (string s : searchData)
            {  
              //  String ss = s.trim('');
              //  if(ss=''){
                if (whereClause == ' where ')
                {
                    whereClause += ' SubscribedBy__r.Name like \'%'+ s +'%\' OR SubscribedBy__r.Email like \'%'+ s +'%\' ';
                }
                else
                {
                    whereClause += ' or SubscribedBy__r.Name like \'%'+ s +'%\' OR SubscribedBy__r.Email like \'%'+s+'%\' ';
                } 
               // }
            }
            searchquery += whereClause + ' Limit 10';
            String s2 = searchquery.removeEnd(',');
            subsD= Database.query(searchquery);
        }
        
    }