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

Adding Wildcard to Apex


Hi guys 


I need some help I have created the following apex trigger below, However the trigger currently looks at exact matches on the company name

Is there any way that i could incorporate some kind of wildcard, so the search isn’t exact ?


From reading the forums, it looks like i need to go along the lines of "


String myValue = '%' + s + '%'
List<Lead> = new List<Lead>([Select Id, companyName from leads where customField like :myvalue]);
Can anyone point me in the correct direction ?

Trigger DuplicateLeadPreventer on Lead

                               (before insert, before update) {

//Get map of record types we care about from Custom Setting

 Map<String, Manage_Lead_Dupes_C__c> leadrtmap = Manage_Lead_Dupes_C__c.getAll();

//Since only certain leads will match, put them in a separate list

 List<Lead> LeadstoProcess = new List<Lead> ();


 //Company to Lead Map

 Map<String, Lead> leadMap = new Map<String, Lead>();

     for (Lead lead : {


     //Only process for Leads in our RecordTypeMap

         if (leadrtmap.keyset().contains(lead.RecordTypeId) ) {


        // Make sure we don't treat an Company name that

       // isn't changing during an update as a duplicate.


              if (

                 ( != null) &&

                 (Trigger.isInsert ||

                 ( != Trigger.oldMap.get(lead.Id).company))




                    // Make sure another new lead isn't also a duplicate


                        if (leadMap.containsKey( {

                  'Another new lead has the '

                                            + 'same company name.');

                        } else {

                            leadMap.put( , lead);




    } //end RT If Check

    } //End Loop



     Using a single database query, find all the leads in

     the database that have the same company address as any

     of the leads being inserted or updated.




    Set<String> ExistingCompanies = new Set<String> ();


            for (Lead l: [Select Id, Company from Lead WHERE Company IN :leadMap.keyset()

                             AND RecordTypeId IN :leadrtmap.keyset()]) {




    //Now loop through leads to process, since we should only loop if matches

    for (Lead l : LeadstoProcess) {

        if (ExistingCompanies.contains( ) {

   'A lead with this company '

                               + 'name already exists.');







You can use wildcard in SOQL as well something like this:


list<account> acc = [SELECT name FROM account WHERE name like '%hello%'];


You can find more info here



HI jungleee

Thank you for your swift response, where about do i put that wildcard into my apex ?



I tried this but salesforce wouldn't allow us to that:


list<account> = [select name from account where name LIKE ('%Hello%','%hi%')];


So ,I have just tweaked the last part of the code. Not sure if this is what you were looking for :



 Set<String> ExistingCompanies = new Set<String> ();
			//getting all the leads which matches the recordType.
            for (Lead l: [Select Id, Company from Lead WHERE RecordTypeId IN :leadrtmap.keyset()]) {
	for(lead l: LeadstoProcess){
		for(string s: existingCompanies){
		//Using the contains string method we will check if the comapny name is present in the list of existing companies.
			if(s.contains({'A lead with this company '
                               + 'name already exists.');


Hope this helps!







Hi Junglee

Thank you for your help.
where in the coding have you stated that a wildcard is needed ?

Sorry i am very new to this language and just trying to grasp the concepts



The logic I employed is,


In the 1st for loop, get all the lead's comapny name in a set.


And then for every lead(leadToProcess List) , iterate thru the set of strings(existingCompanies) and check if the value in of the string is there in the lead's company name using the contains method. I am not using any wildcard as such. 


So lets say you're inserting a lead with the name 'opqr' but there's a existing lead with the company name as 'mnopqr', then the error is thrown


I hope I am clear!





Jake BackuesJake Backues
You can use an array of strings with wildcards in SOQL
String s = 'myName';
String[] Names = new String[]{'%' + s + '%'};
List<Lead> = new List<Lead>([Select Id, companyName from leads where customField like :Names]);