• Rob Morris (bostonmorris)
  • NEWBIE
  • 0 Points
  • Member since 2016
  • Senior Director of Sales Development
  • Nanigans


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
Hey guys,

I need a APEX Trigger to link an Account to Lead in Related Field if Name or email domain match an Existing Account. 

I look at both the Company name on the lead and check that for a match with the account name or the email domain on the lead matches with the website domain.

I want to modify the below to work if the lead company name is even contained in the Account name or if the lead email domain is contained in the Website (or website domain)

What I have works if exact match but not if it is only part of the name or website domain.

ie: if lead company name is Salesforce and Account name is Salesforce Inc or lead email domain is: example.com and website domain is example.com/help it will not work. 


Here is what I have so far:

trigger addAccount on Lead (before Insert, before Update)
{


 List<string> companies=new list<string>();

 For (lead l:trigger.new){
   companies.add(l.company);
  }

 List<Account> leadAccountIds=[Select Id, Name FROM Account WHERE Name IN: companies];

 Map<String, Id> acctNameId=new Map<String, Id>();

 For (Account a:leadAccountIds){
   acctNameId.put(a.name,a.Id);
  }
 
 For (Lead l2:trigger.new){
  if(acctNameId.containsKey(l2.company)){
    l2.Lead_Account__c=acctNameId.get(l2.company);
   }

  }
 
  
 Map<String, Id> domains = new Map<String, Id>();
 for(Lead record: Trigger.new) {
    domains.put(record.Domain__c, null);
 }
 for(Account record: [SELECT Domain__c FROM Account WHERE Domain__c IN :domains.keySet()]) {
    domains.put(record.Domain__c, record.Id);
 }
 for(Lead record: Trigger.new) {
    if(domains.get(record.Domain__c) != null) {
        record.Lead_Account__c = domains.get(record.Domain__c);
    }
  }
}

Any help is appreciated.
Hey guys,

I need a APEX Trigger to link an Account to Lead in Related Field if Name or email domain match an Existing Account. 

I look at both the Company name on the lead and check that for a match with the account name or the email domain on the lead matches with the website domain.

I want to modify the below to work if the lead company name is even contained in the Account name or if the lead email domain is contained in the Website (or website domain)

What I have works if exact match but not if it is only part of the name or website domain.

ie: if lead company name is Salesforce and Account name is Salesforce Inc or lead email domain is: example.com and website domain is example.com/help it will not work. 


Here is what I have so far:

trigger addAccount on Lead (before Insert, before Update)
{


 List<string> companies=new list<string>();

 For (lead l:trigger.new){
   companies.add(l.company);
  }

 List<Account> leadAccountIds=[Select Id, Name FROM Account WHERE Name IN: companies];

 Map<String, Id> acctNameId=new Map<String, Id>();

 For (Account a:leadAccountIds){
   acctNameId.put(a.name,a.Id);
  }
 
 For (Lead l2:trigger.new){
  if(acctNameId.containsKey(l2.company)){
    l2.Lead_Account__c=acctNameId.get(l2.company);
   }

  }
 
  
 Map<String, Id> domains = new Map<String, Id>();
 for(Lead record: Trigger.new) {
    domains.put(record.Domain__c, null);
 }
 for(Account record: [SELECT Domain__c FROM Account WHERE Domain__c IN :domains.keySet()]) {
    domains.put(record.Domain__c, record.Id);
 }
 for(Lead record: Trigger.new) {
    if(domains.get(record.Domain__c) != null) {
        record.Lead_Account__c = domains.get(record.Domain__c);
    }
  }
}

Any help is appreciated.

Does anyone have any Apex code (or ideas) that could be run prior to the web-to-lead assignment rules that would: 

 

- Query for any accounts of the same name as the "Company Name" field in the lead  

 

- Determine if it is a "named" account based on an account field 

 

- Assign the lead to the account owner OR another designated user based on the account owner (like inside sales)

 

This would be really helpful for us in situations where we have named accounts that are outside the normal lead / territory assignments.  

 

Thanks for any code, help or ideas!

  • February 09, 2010
  • Like
  • 0