I have created a trigger on a custom object that is going to be used to store Lead data, when this information is initially inserted to the object the trigger creates a Map of current Lead and Opportunity information (Company Name, Postcode, Landline Phone). This information is then compared to the data being inserted to the custom object and if a Match is found I want the ID's for every match that is found from Leads and Opportunities to be inserted to a text field on the object.
Currently my code is only able to return a single ID when a match is found and I am having trouble figuring out a way to get ALL of the ID's matched. Here is my current code:
trigger LeadDuplicateChecker on Data_Vault__c (before insert, before update) {
//Map Company/Postcode/Phone to ID
Map<String,Id> comparisonMap = new Map<String,Id>();
//Company
for(Lead l : [Select Id, Company, Postcode__c, Landline_Phone__c From Lead Where isConverted = FALSE and Company != null]){
IF(l.Company != null){
comparisonMap.put(l.Company.toLowerCase().replace(' ','').replace('-','').replace('~','').replace('!','').replace('@','').replace('#','').replace('$','').replace('%','').replace('^','').replace('&','').replace('_',''), l.Id);
}
IF(l.Postcode__c != null){
comparisonMap.put(l.Postcode__c.toLowerCase().replace(' ','').replace('-','').replace('~','').replace('!','').replace('@','').replace('#','').replace('$','').replace('%','').replace('^','').replace('&','').replace('_',''), l.Id);
}
IF(l.Landline_Phone__c != null){
comparisonMap.put(l.Landline_Phone__c.toLowerCase().replace(' ','').replace('-','').replace('~','').replace('!','').replace('@','').replace('#','').replace('$','').replace('%','').replace('^','').replace('&','').replace('_',''), l.Id);
}
}
for(Opportunity o : [Select Id, Name, Postcode__c, Landline_Phone__c From Opportunity Where StageName IN ('To Call Back','Quote Given','On Stop','In Contract - Diarised') AND Name != null]){
IF(o.Name != null){
comparisonMap.put(o.Name.toLowerCase().replace(' ','').replace('-','').replace('~','').replace('!','').replace('@','').replace('#','').replace('$','').replace('%','').replace('^','').replace('&','').replace('_',''), o.Id);
}
IF(o.Postcode__c != null){
comparisonMap.put(o.Postcode__c.toLowerCase().replace(' ','').replace('-','').replace('~','').replace('!','').replace('@','').replace('#','').replace('$','').replace('%','').replace('^','').replace('&','').replace('_',''), o.Id);
}
IF(o.Landline_Phone__c != null){
comparisonMap.put(o.Landline_Phone__c.toLowerCase().replace(' ','').replace('-','').replace('~','').replace('!','').replace('@','').replace('#','').replace('$','').replace('%','').replace('^','').replace('&','').replace('_',''), o.Id);
}
}
system.debug('Map Keyset: ' +comparisonMap.keySet() );
//Comparison loop
for(Data_Vault__c d : Trigger.New){
//Set Matching Fields to blank before performing comparison
d.Company_Match__c = '';
d.Phone_Match__c = '';
d.Postcode_Match__c = '';
d.Record_Company_Match__c = '';
d.Record_Phone_Match__c = '';
d.Record_Postcode_Match__c = '';
//Company
IF(d.Company_Name__c != null){
If(comparisonMap.containsKey(d.Company_Name__c.toLowerCase().replace(' ','').replace('-','').replace('~','').replace('!','').replace('@','').replace('#','').replace('$','').replace('%','').replace('^','').replace('&','').replace('_','') )){
d.Company_Match__c = 'Company Match Found';
d.Record_Company_Match__c = comparisonMap.get(d.Company_Name__c.toLowerCase().replace(' ','').replace('-','').replace('~','').replace('!','').replace('@','').replace('#','').replace('$','').replace('%','').replace('^','').replace('&','').replace('_','') );
}
}
//Postcode
IF(d.Postcode__c != null){
If(comparisonMap.containsKey(d.Postcode__c.toLowerCase().replace(' ','').replace('-','').replace('~','').replace('!','').replace('@','').replace('#','').replace('$','').replace('%','').replace('^','').replace('&','').replace('_','') )){
d.Postcode_Match__c = 'Postcode Match Found';
d.Record_Postcode_Match__c = comparisonMap.get(d.Postcode__c.toLowerCase().replace(' ','').replace('-','').replace('~','').replace('!','').replace('@','').replace('#','').replace('$','').replace('%','').replace('^','').replace('&','').replace('_','') );
}
}
//Phone
IF(d.Phone__c != null && d.Phone__c != 'TPS'){
If(comparisonMap.containsKey(d.Phone__c.toLowerCase().replace(' ','').replace('-','').replace('~','').replace('!','').replace('@','').replace('#','').replace('$','').replace('%','').replace('^','').replace('&','').replace('_','') )){
d.Phone_Match__c = 'Phone Match Found';
d.Record_Phone_Match__c = comparisonMap.get(d.Phone__c.toLowerCase().replace(' ','').replace('-','').replace('~','').replace('!','').replace('@','').replace('#','').replace('$','').replace('%','').replace('^','').replace('&','').replace('_','') );
}
}
}
}
Would really appreciate any help/guidance on a solution to this!
comparisonMap
to store a List of Ids for every key? eg:Map<String, List<Id>>