You need to sign in to do that
Don't have an account?

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 "
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 : Trigger.new) {
//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 (
(lead.company != null) &&
(Trigger.isInsert ||
(lead.company != Trigger.oldMap.get(lead.Id).company))
)
{
// Make sure another new lead isn't also a duplicate
if (leadMap.containsKey(lead.company)) {
lead.company.addError('Another new lead has the '
+ 'same company name.');
} else {
leadMap.put(lead.company , lead);
LeadstoProcess.add(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()]) {
ExistingCompanies.add(l.Company);
}
//Now loop through leads to process, since we should only loop if matches
for (Lead l : LeadstoProcess) {
if (ExistingCompanies.contains(l.company) ) {
l.company.addError('A lead with this company '
+ 'name already exists.');
}
}
}
Hi
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
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_select_comparisonoperators.htm
-ಸಮಿರ್
Thank you for your swift response, where about do i put that wildcard into my apex ?
Hi
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 :
Hope this helps!
-ಸಮಿರ್
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
Hi,
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!
-ಸಮಿರ್