2

I am writing a trigger for a custom object Data_Vault__c where data such as Company Name and Postcode are entered, this data is then compared to corresponding fields on Lead and Opportunity, if a match is found a field on the Data_Vault__c record is changed to 'Match Found'.

Here is some trimmed down code of my trigger so far:

List<Lead> leadCompanyList = new List<Lead>([SELECT ID, Company 
                                             FROM Lead 
                                             WHERE isConverted = FALSE]);

List<Opportunity> oppCompanyList = new List<Opportunity>([SELECT ID, Name
                                                          FROM Opportunity
                                                          WHERE StageName IN ('To Call Back','Quote Given','On Stop','In Contract - Diarised') ]);

Set<String> leadCompanySet = new Set<String>();
Set<String> oppCompanySet = new Set<String>();

for(Lead lComp : leadCompanyList){
        IF(lComp.Company != null){    
            leadCompanySet.add( lComp.Company.toLowerCase() );
        }        
}

for(Opportunity oComp : oppCompanyList){  
        IF(oComp.Name != null){
            oppCompanySet.add( oComp.Name.toLowerCase() );
        }
}

for(Data_Vault__c d : Trigger.New){

        if(leadCompanySet.contains( d.Company_Name__c.toLowerCase() ) || oppCompanySet.contains( d.Company_Name__c.toLowerCase() )  ){

            d.Company_Match__c  =   'Company Match Found';

        }
}

This is working fine for me but I would like to also add the Lead/Opportunity Id to another field on the Data_Vault__c record when a match is found but I am not sure of the best way to go about it. I have created a map that contains the Id and Company values:

Map<Id, Lead> mapCompanies = new Map<Id, Lead>([SELECT Company FROM Lead 
                                                WHERE isConverted = FALSE]);

system.debug('map: ' +mapCompanies);

This map contains the Id and Company values that I need but I am not sure how to best use the data in this map to both compare the Company value and enter the ID of the lead to the Data_Vault__c record if a match is found.

Hopefully my requirement makes sense, any guidance on this would be much appreciated!

1
  • (For Lead) Instead if using a set : leadCompanySet , use a map. Store company name as key and the Id as the value (concatenate the Ids delimited by a comma/semicolon). Then instead of doing: leadCompanySet.contains, do a map.get() and if the value is not null, store the value 'Company Match Found' and the value of the map. I hope I make sense – human Jun 19 '15 at 11:28
2

Something like this will work. It's important to note though this doesn't handle a situation where there are multiple leads or opps that match the Company name of your custom object. The code below will only find a single match. If there are multiple that match, it will only set the Id field on your custom object to one of the matches

map<String,Id> lead_opp_map = new map<String,Id>();

for(Lead l : [Select Id, Company From Lead Where isConverted = FALSE and Company != null]){
    lead_opp_map.put(l.Company.toLowerCase(), l.Id);
}

for(Opportunity o : [Select Id, Name From Opportunity Where StageName IN ('To Call Back','Quote Given','On Stop','In Contract - Diarised')]){
    lead_opp_map.put(o.Name.toLowerCase(), o.Id);
}

for(Data_Vault__c d : Trigger.New){
    If(lead_opp_map.containsKey(d.Company_Name__c.toLowerCase())){
        d.Company_Match__c = 'Company Match Found';
        d.Matching_Lead_or_Opp_Id__c = lead_opp_map.get(d.Company_Name__c.toLowerCase());
        continue;
    }
}
3
  • This is looking great so far, I still have a lot to figure out but this is looking like a perfect starting point. Thanks for the reply, I knew I would have to utilize a map in some way just couldn't figure out how to do it exactly. – Liam Jun 19 '15 at 14:46
  • No worries. Come back with more questions if you run into any issues building this out further. – Chris Duncombe Jun 19 '15 at 14:50
  • I've been trying to figure out how to get all of the related Id's when matches are found rather than just a single match but having trouble figuring it out. Would it be an easy amendment to the code or would it change quite a lot? – Liam Jun 22 '15 at 8:54
1

Instead of having a conditional using the contains method, you can have another 'for's iterating over the first lists you declared, and then you would have a condition (if the names match) to make what you want to do. This way, you will have not only the Company_Match__c field, but also the IDs of both objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.