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
Shweta SoparkarShweta Soparkar 

SOQL query for matching a column with a string

I am writing a trigger for updating the product according to the product name that matchs the name in a custom setting
Below is the query which I wrote, But do not understand why it is not retriving any data.

[select Trademark__c,Trademark_Word__c  FROM Trademark2__c WHERE Name LIKE %p.Name%]

Here my Name =  3010 and  p.Name=  I3010 18X12 5E/5E 0500+-002/DI

But this is not working as 3010 is not matching the p.Name.

I know this is something simple ,  But I am not able to figure this out.

Any help is highly appreciated
Best Answer chosen by Shweta Soparkar
Shweta SoparkarShweta Soparkar
Got the solution!!
Thank you all. It was such a simple solution , wasted like 4 hours on this!

String temp= p.Name;
List <Trademark__c>  trademarkList  = new List<Trademark__c>();
String myTradeMark = 'NotFound';
String myTradeMarkWord = 'NotFound';
trademarkList   = [select Name, Trademark__c,Trademark_Word__c  FROM Trademark__c];
             
            System.debug('is empty  '+trademarkList.isEmpty());
            
            if(!trademarkList.isEmpty())
            {
                for(Trademark__c t:trademarkList)
                {
                    if(temp.contains(t.name)) 
                    {
                    System.debug('Name'+t.Name);
                    System.debug('Trademark'+t.Trademark__c );
                    System.debug('Trademark word'+t.Trademark_Word__c);
                    myTradeMark= t.Trademark__c;
                    myTradeMarkWord = t.Trademark_Word__c;
                    break;
                    }
                }
               
            }

All Answers

Rakesh51Rakesh51
Try this Name LIKE '%p.name%'
Shivram SainiShivram Saini
Hi Shweta,

Try this:
[select Trademark__c,Trademark_Word__c  FROM Trademark2__c WHERE Name LIKE: '%'+p.Name+'%']

Happy Coding
 
Shweta SoparkarShweta Soparkar
@Shivram Saini: No it doesnt work and the Name is '3010' and p.Name is 'I3010 18X12 5E/5E 0500+-002/DI'.

Like is not able to match this as p.Name is not a substring of Name.

I want to match a smaller length string to a longer string as in Name is a substring of p.Name
Shweta SoparkarShweta Soparkar
Got the solution!!
Thank you all. It was such a simple solution , wasted like 4 hours on this!

String temp= p.Name;
List <Trademark__c>  trademarkList  = new List<Trademark__c>();
String myTradeMark = 'NotFound';
String myTradeMarkWord = 'NotFound';
trademarkList   = [select Name, Trademark__c,Trademark_Word__c  FROM Trademark__c];
             
            System.debug('is empty  '+trademarkList.isEmpty());
            
            if(!trademarkList.isEmpty())
            {
                for(Trademark__c t:trademarkList)
                {
                    if(temp.contains(t.name)) 
                    {
                    System.debug('Name'+t.Name);
                    System.debug('Trademark'+t.Trademark__c );
                    System.debug('Trademark word'+t.Trademark_Word__c);
                    myTradeMark= t.Trademark__c;
                    myTradeMarkWord = t.Trademark_Word__c;
                    break;
                    }
                }
               
            }
This was selected as the best answer