How to use SOQl contains?

172    Asked by ChristianParsons in Salesforce , Asked on Jun 5, 2023
My SOQL Query
string str = '%'+email.subject.trim()+'%'; GMC__c gmcList = [select id,name,Case_Number__c from GMC__c where name like :str limit 1];

GMC__c Name is an autonumber field which will be unique Now my actual requirement is when a user sends a mail Subject with only Name it works perfectly. In the other case if he says suppose This is regarding GMCName then we need to Query that contains GMCName. What would be the best approach for this

Answered by Darsh K

What I understood from your requirement to use soql contains is, Subjects sent by User can be dynamic. Sometimes it's only GMCName and sometimes it is included in some sentence, as you have mentioned This is regarding GMCName.

And your GMC__c Name is an autonumber field. So it must have some particular format like Q{00000} i.e A-{0000} etc. Having fixed length and fixed format. You must know the starting characters of your GMC Name.
You can do following :
Integer index = email.subject.indexOf(--Starting Characters of GMC Name--);
//As you know the length of your GMC name(as it is Autonumber), so that length to the Integer index obtained above.
String gmcName = email.subject.substring(index,index+lengthOfAutonumber)
In this way you can obtain GMCName

Your Answer


Parent Categories