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
KNKKNK 

Wild Card Search in apex

// search query
    private transient boolean productdata_refreshed = false;
    private void refreshProductData()
    {
            if (productdata_refreshed==null || productdata_refreshed!=true) {
                cacheProductData = new List<ProductData>(); 
            
                //system.debug('--------------- refreshProductData reads data');
            
                Set<ID> exclude = new Set<ID>();
                if (options.excludeProducts!=null)
                    exclude.addAll(options.excludeProducts);
                
                string sexclude = null;
                if (exclude.size()>0)
                    sexclude = 'not in (' + Utils.qjoin(',', exclude) + ')';
                
                exclude = null;
                
                               
                    string soql =   'Select p.Name, p.Id, p.Family__c,p.APVMA_Approval_number__c, ' +
                                    'p.Price_per_L_per_Kg__c, p.Label__r.Country__c ' +
                                    'From Label_Part__c p ' +
                                    'Where ' +
                                    'p.Active__c = True '+
                                    'and (p.Label__r.Country__c = \'' + options.country + '\') ';
                             
                    if (sexclude!=null)
                        soql += 'and (p.Id '+sexclude +') ';
                                    
                    if (selectedProductFamily!=null)
                    {
                        soql += 'and (p.Family__c=\'' + selectedProductFamily + '\') ';
                        
                    }
                    else
                        soql += 'and (p.Family__c in ('+ Utils.qjoin(',', options.productFamilyFilter) +')) ';
    
    
                    if (selectedProduct != null && selectedProduct.trim()!='')
                        soql += 'and (p.Name like  \'%' + string.escapeSingleQuotes(selectedProduct.trim()) + '%\') ';

                    soql += 'order by p.Name limit 300';      
                    
                    for (Label_Part__c p : Database.query(soql))
                        cacheProductData.add(new ProductData(p, this));
                    
                productdata_refreshed = true;    
            }
    }

 

 

here is the my code i want to wild search with this

 

for eg : if provide a* it has to display the products with a only currently it is deplaying all products .

 

could you please help me here.

Best Answer chosen by Admin (Salesforce Developers) 
liron169liron169

At first you should seach with 'a*' , instead you can search only with 'a'.

 

if you want in this case to get only those that start with 'a', you should write:

 

soql += 'and (p.Name like 

 \'' + string.escapeSingleQuotes(selectedProduct.trim()) + '%\') ';

 

(without the % at start)

All Answers

liron169liron169

At first you should seach with 'a*' , instead you can search only with 'a'.

 

if you want in this case to get only those that start with 'a', you should write:

 

soql += 'and (p.Name like 

 \'' + string.escapeSingleQuotes(selectedProduct.trim()) + '%\') ';

 

(without the % at start)

This was selected as the best answer
KNKKNK

Thank you... :)