Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class assignLeads {
- public void assignRelatedAccount(){
- Set<String> domains = new Set<String>();
- Set<String> ldNames = new Set<String>();
- Set<String> ldWebs = new Set<String>();
- List<Lead> updatelds = new List<Lead>();
- List<Lead> roundRobinLds = new List<Lead>();
- Set<ID> leadsWithAccounts = new Set<ID>();
- Map<String, Account> accDomMap = new Map<String, Account>();
- Map<String, Contact> conDomMap = new Map<String,Contact>();
- Map<String, Account> accWebMap = new Map<String, Account>();
- Map<String, Contact> conWebMap = new Map<String, Contact>();
- Map<String,Account> accNameMap = new Map<String, Account>();
- Set<String> salesRoles = new Set<String>{'Large Accounts', 'Strategic Accounts', 'SDR', 'Services', 'CSM'};
- Set<String> personalDomains = new Set<String>();
- for(Personal_domain__c pd :[SELECT domain_name__c FROM Personal_Domain__c]){
- personalDomains.add(pd.domain_Name__c);
- }
- Boolean found = false;
- for(Lead ld : (List<Lead>)trigger.new){
- domains.add(ld.domainName__c);
- ldNames.add(ld.company);
- ldWebs.add(ld.website);
- }
- //Gather all accounts that have domain names we pulled from the leads. This is how we will match records
- //Will iterate over a list and store values in a map
- for(Account acc : [SELECT id, name, owner.name, numberOfEmployees, billingState, strategic_account__c, target_account__c, domainName__c, website, master_record__c, owner.UserRole.Name
- FROM Account WHERE ((domainName__c IN :domains AND domainName__c !=null) OR (website IN :ldWebs AND website != null) OR name IN :ldNames) AND domainName__c NOT IN :personalDomains AND domainName__c != 'gmail.com' AND domainName__c != 'yahoo.com'
- ORDER BY LastActivityDate DESC LIMIT 50000]){
- system.debug('Account Name: '+acc.name+'. Account Domain: '+acc.domainName__c+'. Account website: '+acc.website+'. Master Record? '+acc.master_record__c);
- //if domainName isn't null, populate domain key map
- if(acc.domainName__c != null){
- accDomMap.put(acc.domainName__c, acc);
- }
- //if website isn't null populate website key map
- if(acc.website != null){
- system.debug('Website: '+acc.website);
- accWebMap.put(acc.website, acc);
- }
- //Name is a system required field so we always populate the name key map
- accNameMap.put(acc.name, acc);
- }
- for(Lead ld : (List<Lead>)trigger.new){
- system.debug('Lead Company: '+ld.company+'. Lead Domain: '+ld.domainName__c+'. Lead website: '+ld.website);
- //preset each check box to true and then flag false if match is found
- ld.no_matches_found_sales_queue__c = true;
- //Make sure we have accounts to look at
- //if not break the loop
- if(accDomMap.isEmpty() && accNameMap.isEmpty() && accWebMap.isEmpty()){
- break;
- }
- //first things first we check if there is an exact domain match and exact company match, and see if we have a master record...
- if(accDomMap.containsKey(ld.domainName__c) && accNameMap.containsKey(ld.company) && accDomMap.get(ld.domainName__c).master_record__c == true && accDomMap.get(ld.domainName__c).id == accNameMap.get(ld.company).id){
- //next make sure the account owner is a sales/csm user before assigning lead
- //to the account owner. Then relate the account to the lead even though
- //it will still go through round robin territory assignment
- if(salesRoles.contains(accDomMap.get(ld.domainName__c).owner.UserRole.name)){
- ld.ownerId = accDomMap.get(ld.domainName__c).ownerId;
- ld.no_matches_found_sales_queue__c = false;
- system.debug('Assigned Lead in 1');
- }
- //if lead state/number of employees is blank and account state/number of employees isn't, then populate the lead
- if(ld.state == null && accDomMap.get(ld.domainName__c).billingState!= null){
- ld.state = accDomMap.get(ld.domainName__c).billingState;
- }
- if(ld.numberOfEmployees == null && accDomMap.get(ld.domainName__c).numberOfEmployees != null){
- ld.numberOfEMployees = accDomMap.get(ld.domainName__c).numberOfEmployees;
- }
- //relate account to lead
- ld.related_account__c = accDomMap.get(ld.domainName__c).id;
- }else if(accWebMap.containsKey(ld.website) && accNameMap.containsKey(ld.company) && accWebMap.get(ld.website).id == accNameMap.get(ld.company).id && accNameMap.get(ld.company).master_record__c == true){
- if(salesRoles.contains(accNameMap.get(ld.company).owner.UserRole.name)){
- ld.ownerId = accNameMap.get(ld.company).ownerId;
- ld.no_matches_found_sales_queue__c = false;
- system.debug('Assigned Lead in 2');
- }
- if(ld.state == null && accNameMap.get(ld.domaiNname__c).billingState != null){
- ld.state = accNameMap.get(ld.domainName__c).billingState;
- }
- if(ld.numberOfEmployees == null && accNameMap.get(ld.domainName__c).numberOfEMployees != null){
- ld.numberOfEMployees = accNameMap.get(ld.domainName__c).numberOfEmployees;
- }
- ld.related_account__c = accNameMap.get(ld.company).id;
- }else if(accDomMap.containsKey(ld.domainName__c) && accDomMap.get(ld.domainName__c).master_record__c == true){
- If(salesRoles.contains(accDomMap.get(ld.domainName__c).owner.UserRole.name)){
- ld.ownerId = accDomMap.get(ld.domainName__c).ownerid;
- ld.no_matches_found_sales_queue__c = false;
- system.debug('Assigned Lead in 3');
- }
- if(ld.state == null && accDomMap.get(ld.domainName__c).billingState!= null){
- ld.state = accDomMap.get(ld.domainName__c).billingState;
- }
- if(ld.numberOfEmployees == null && accDomMap.get(ld.domainName__c).numberOfEmployees != null){
- ld.numberOfEMployees = accDomMap.get(ld.domainName__c).numberOfEmployees;
- }
- ld.related_account__c = accDomMap.get(ld.domainName__c).id;
- }else If(accNameMap.containsKey(ld.company) && accNameMap.get(ld.company).master_record__c == true){
- if(salesRoles.contains(accNameMap.get(ld.company).owner.UserRole.name)){
- ld.ownerId = accNameMap.get(ld.company).ownerId;
- ld.no_matches_found_sales_queue__c = false;
- system.debug('Assigned Lead in 4');
- }
- if(ld.state == null && accNameMap.get(ld.domaiNname__c).billingState != null){
- ld.state = accNameMap.get(ld.domainName__c).billingState;
- }
- if(ld.numberOfEmployees == null && accNameMap.get(ld.domainName__c).numberOfEMployees != null){
- ld.numberOfEMployees = accNameMap.get(ld.domainName__c).numberOfEmployees;
- }
- ld.related_account__c = accNameMap.get(ld.company).id;
- }else if(accWebMap.containsKey(ld.website) && accWebMap.get(ld.website).master_record__c == true){
- If(salesRoles.contains(accWebMap.get(ld.website).owner.UserRole.name)){
- ld.ownerId = accWebMap.get(ld.website).ownerid;
- ld.no_matches_found_sales_queue__c = false;
- system.debug('Assigned Lead in 5');
- }
- if(ld.state == null && accWebMap.get(ld.domaiNname__c).billingState != null){
- ld.state = accWebMap.get(ld.domainName__c).billingState;
- }
- if(ld.numberOfEmployees == null && accWebMap.get(ld.domainName__c).numberOfEMployees != null){
- ld.numberOfEMployees = accWebMap.get(ld.domainName__c).numberOfEmployees;
- }
- ld.related_account__c = accWebMap.get(ld.website).id;
- }else if(accDomMap.containsKey(ld.domainName__c) && accWebMap.containsKey(ld.website) && accNameMap.containsKey(ld.company) && accDomMap.get(ld.domainName__c).id == accNameMap.get(ld.company).id && accDomMap.get(ld.domainName__c).id == accWebMap.get(ld.website).id){
- If(salesRoles.contains(accWebMap.get(ld.website).owner.UserRole.name)){
- ld.ownerId = accWebMap.get(ld.website).ownerid;
- ld.no_matches_found_sales_queue__c = false;
- system.debug('Assigned Lead in 6');
- }
- if(ld.state == null && accWebMap.get(ld.domaiNname__c).billingState != null){
- ld.state = accWebMap.get(ld.domainName__c).billingState;
- }
- if(ld.numberOfEmployees == null && accWebMap.get(ld.domainName__c).numberOfEMployees != null){
- ld.numberOfEMployees = accWebMap.get(ld.domainName__c).numberOfEmployees;
- }
- ld.related_account__c = accWebMap.get(ld.website).id;
- }else if(accDomMap.containsKey(ld.domainName__c) && accNameMap.containsKey(ld.company) && accDomMap.get(ld.domainName__c).id == accNameMap.get(ld.company).id){
- if(salesRoles.contains(accNameMap.get(ld.company).owner.UserRole.name)){
- ld.ownerId = accNameMap.get(ld.company).ownerid;
- ld.no_matches_found_sales_queue__c = false;
- system.debug('Assigned Lead in 7');
- }
- if(ld.state == null && accNameMap.get(ld.domaiNname__c).billingState != null){
- ld.state = accNameMap.get(ld.domainName__c).billingState;
- }
- if(ld.numberOfEmployees == null && accNameMap.get(ld.domainName__c).numberOfEMployees != null){
- ld.numberOfEMployees = accNameMap.get(ld.domainName__c).numberOfEmployees;
- }
- ld.related_account__c = accNameMap.get(ld.company).id;
- }else if(accWebMap.containsKey(ld.website) && accNameMap.containsKey(ld.company) && accWebMap.get(ld.website).id == accNameMap.get(ld.company).id){
- if(salesRoles.contains(accNameMap.get(ld.company).owner.UserRole.name)){
- ld.ownerId = accNameMap.get(ld.company).ownerid;
- ld.no_matches_found_sales_queue__c = false;
- system.debug('Assigned Lead in 8');
- }
- if(ld.state == null && accNameMap.get(ld.domaiNname__c).billingState != null){
- ld.state = accNameMap.get(ld.domainName__c).billingState;
- }
- if(ld.numberOfEmployees == null && accNameMap.get(ld.domainName__c).numberOfEMployees != null){
- ld.numberOfEMployees = accNameMap.get(ld.domainName__c).numberOfEmployees;
- }
- ld.related_account__c = accNameMap.get(ld.company).id;
- }else if(accDomMap.containsKey(ld.domainName__c) && accDomMap.get(ld.domainName__c).billingState != null && accDomMap.get(ld.domainName__c).numberOfEmployees != null){
- if(salesRoles.contains(accDomMap.get(ld.domainName__c).owner.UserRole.name)){
- ld.ownerId = accDomMap.get(ld.domainName__c).ownerId;
- ld.no_matches_found_sales_queue__c = false;
- system.debug('Assigned Lead in 9');
- }
- if(ld.state == null && accDomMap.get(ld.domainName__c).billingState!= null){
- ld.state = accDomMap.get(ld.domainName__c).billingState;
- }
- if(ld.numberOfEmployees == null && accDomMap.get(ld.domainName__c).numberOfEmployees != null){
- ld.numberOfEMployees = accDomMap.get(ld.domainName__c).numberOfEmployees;
- }
- ld.related_account__c = accDomMap.get(ld.domainName__c).id;
- }else if(accNameMap.containsKey(ld.company) && accNameMap.get(ld.company).billingState != null && accNameMap.get(ld.company).numberOfEmployees != null){
- if(salesRoles.contains(accNameMap.get(ld.company).owner.UserRole.name)){
- ld.ownerId = accNameMap.get(ld.company).ownerId;
- ld.no_matches_found_sales_queue__c = false;
- system.debug('Assigned Lead in 10');
- }
- if(ld.state == null && accNameMap.get(ld.domaiNname__c).billingState != null){
- ld.state = accNameMap.get(ld.domainName__c).billingState;
- }
- if(ld.numberOfEmployees == null && accNameMap.get(ld.domainName__c).numberOfEMployees != null){
- ld.numberOfEMployees = accNameMap.get(ld.domainName__c).numberOfEmployees;
- }
- ld.related_account__c = accNameMap.get(ld.company).id;
- }else if(accWebMap.containsKey(ld.website) && accNameMap.containsKey(ld.company) && accWebMap.get(ld.website).id == accNameMap.get(ld.company).id){
- if(SalesRoles.contains(accDomMap.get(ld.domainName__c).owner.userRole.name)){
- ld.ownerId = accNameMap.get(ld.company).ownerId;
- ld.no_matches_found_sales_queue__c = false;
- system.debug('Assigned Lead in 11');
- }
- if(ld.state == null && accNameMap.get(ld.domaiNname__c).billingState != null){
- ld.state = accNameMap.get(ld.domainName__c).billingState;
- }
- if(ld.numberOfEmployees == null && accNameMap.get(ld.domainName__c).numberOfEMployees != null){
- ld.numberOfEMployees = accNameMap.get(ld.domainName__c).numberOfEmployees;
- }
- ld.related_account__c = accNameMap.get(ld.company).id;
- }else if(accDomMap.containsKey(ld.domainName__c)){
- if(SalesRoles.contains(accDomMap.get(ld.domainName__c).owner.userRole.name)){
- ld.ownerId = accDomMap.get(ld.domainName__c).ownerId;
- ld.no_matches_found_sales_queue__c = false;
- system.debug('Assigned Lead in 12');
- }
- if(ld.state == null && accDomMap.get(ld.domainName__c).billingState!= null){
- ld.state = accDomMap.get(ld.domainName__c).billingState;
- }
- if(ld.numberOfEmployees == null && accDomMap.get(ld.domainName__c).numberOfEmployees != null){
- ld.numberOfEMployees = accDomMap.get(ld.domainName__c).numberOfEmployees;
- }
- ld.related_account__c = accDomMap.get(ld.domainName__c).id;
- }
- }
- }
- public void territoryQueueAssignment(){
- //create sets for each territory to compare against state field on lead
- Set<String> NE = new Set<String>{'CT','MA','ME', 'NH','NY','RI','VT'};
- Set<String> MDA = new Set<String>{'DC','DE','MD','NJ','PA'};
- Set<String> SE = new Set<String>{'FL','GA','NC','PR','SC','VA','WV'};
- Set<String> MDW = new Set<String>{'IA','IL','IN','KY','MI','MN','OH','TN','WI'};
- Set<String> SO = new Set<String>{'AL','AR','KS','LA','MO','MS','ND','NE','OK','SD','TX'};
- Set<String> NW = new Set<String>{'OR','WA','WY','AK','ID','MT'};
- Set<String> SW = new Set<String>{'AZ','CO','HI','NM','NV','UT'};
- Set<String> CA = new Set<String>{'CA'};
- //create maps for storing previous and current lead data
- Map<Id, Lead> oldMap = (Map<Id,Lead>)trigger.oldMap;
- Map<Id, Lead> newMap = (Map<Id, Lead>)trigger.newMap;
- for(Lead ld : (List<Lead>)trigger.new){
- //make sure this is a lead we want to be assigning by check to see if criteria are met to skip lead routing
- if(ld.no_matches_found_sales_queue__c == false || ld.ByPass_Lead_Assignment__c == true || ld.leadSource == 'zoomInfo' || ld.leadSource == 'Contractor Research'){
- continue;
- }
- //if lead is being manually reassigned, skip the lead routing
- if(oldMap != null){
- if(oldMap.get(ld.id).ownerId != ld.ownerId /*|| (oldMap.get(ld.id).related_account__c != null && ld.related_account__c == null)*/){
- continue;
- }
- }
- //if we have number of employees and territory we have everything we need to route the leads correctly
- //otherwise we will route them later based on lack of information
- if(ld.numberOfEmployees >99 && ld.numberOfEmployees < 1000){//criteria for MM AE leads
- if(ld.territory2__c == 'NW' || ld.territory2__c == 'SW' || ld.territory2__c == 'CA' || CA.contains(ld.state)||SW.contains(ld.state)||NW.contains(ld.state)){//MM AE West region territories
- //Assign to pro West queue
- ld.ownerId = '00G34000003zRIf';
- ld.geo_assigned__c = true;
- }else if(ld.territory2__c == 'SO' || ld.territory2__c == 'MDW' || SO.contains(ld.state) || MDW.contains(ld.state)){//MM AE Central Region territores
- //assign to Pro Central queue
- ld.ownerId = '00G34000003zRIV';
- ld.geo_assigned__c = true;
- }else if(ld.territory2__c == 'MDA' || ld.territory2__c == 'NE' || ld.territory2__c == 'SE' || MDA.contains(ld.state) || NE.contains(ld.state)|| SE.contains(ld.state)){//MM AE East region territory
- //assign to Pro East Queue
- ld.ownerId = '00G34000003zRIG';
- ld.geo_assigned__c = true;
- }else if((ld.state == null || ld.territory2__c == 'International' || (!NE.contains(ld.state)&&!MDA.contains(ld.state)&&!SE.contains(ld.state)&&!MDW.contains(ld.state)&&!SO.contains(ld.state)&&!NW.contains(ld.state)&&!SW.contains(ld.state)&&!CA.contains(ld.state))) && (ld.numberOfEmployees >99 && ld.numberOfEmployees <1000)){
- //round robin leads with 100-999 employees among all MM AE reps if state is blank or if state is not found in our territory arrays because this should indicate the lead is international
- //assign to ALL MM AE queue
- ld.ownerId = '00G34000003c6tf';
- ld.geo_assigned__c = true;
- }
- }else if(ld.numberOfEmployees >= 1000 || ld.numberOfEmployees <100){//Criteria for SDR leads
- if(ld.territory2__c == 'NW' || ld.territory2__c == 'SW' || ld.territory2__c == 'CA' || NW.contains(ld.state) || SW.contains(ld.state)||CA.contains(ld.state)){//SDR west region territories
- //SDR West queue
- ld.ownerId = '00G340000032077';
- ld.geo_assigned__c = true;
- }else if(ld.territory2__c == 'MDW' || ld.territory2__c == 'SO' || MDW.contains(ld.state)||SO.contains(ld.state)){//SDR Central Region Territories
- //SDR central queue
- ld.ownerId = '00G34000003206x';
- ld.geo_assigned__c = true;
- }else if(ld.territory2__c == 'MDA' || ld.territory2__c == 'NE' || ld.territory2__c == 'SE' || MDA.contains(ld.state)||NE.contains(ld.state)|| SE.contains(ld.state)){//SDR East Region territories
- //SDR East queue
- ld.ownerId = '00G34000003206s';
- ld.geo_assigned__c = true;
- }else if((ld.state == null || ld.territory2__c == 'International' || (!NE.contains(ld.state)&& !MDA.contains(ld.state)&&!SE.contains(ld.state)&& !MDW.contains(ld.state)&& !SO.contains(ld.state)&& !NW.contains(ld.state)&& !SW.contains(ld.state) && !CA.contains(ld.state))) && (ld.numberOfEmployees <100 || ld.numberOfEmployees >= 1000)){
- //round robin leads with less than 100 or more than 999 among all SDR reps if state is blank or if state is not found in our territory arrays because this should indicate the lead is international
- //assign to ALL SDR queue
- ld.ownerId = '00G34000003c6ta';
- ld.geo_assigned__c = true;
- }
- }else if(ld.numberOfEmployees == null && ld.state != null){//if we don't have number of employees then we route to SDRs by region
- if(ld.territory2__c == 'NW' || ld.territory2__c == 'SW' || ld.territory2__c == 'CA' || NW.contains(ld.state) || SW.contains(ld.state)||CA.contains(ld.state)){//SDR west region territories
- //SDR West queue
- ld.ownerId = '00G340000032077';
- ld.geo_assigned__c = true;
- }else if(ld.territory2__c == 'MDW' || ld.territory2__c == 'SO' || MDW.contains(ld.state)||SO.contains(ld.state)){//SDR Central Region Territories
- //SDR central queue
- ld.ownerId = '00G34000003206x';
- ld.geo_assigned__c = true;
- }else if(ld.territory2__c == 'MDA' || ld.territory2__c == 'NE' || ld.territory2__c == 'SE' || MDA.contains(ld.state)||NE.contains(ld.state)|| SE.contains(ld.state)){//SDR East Region territories
- //SDR East queue
- ld.ownerId = '00G34000003206s';
- ld.geo_assigned__c = true;
- }
- }else if (ld.state == null && ld.numberOfEmployees == null){
- //if we don't have any routing data we need the SDRs to be calling and doing some research so we will round robin between all SDRs
- //assign to ALL SDR queue
- ld.ownerId = '00G34000003c6ta';
- ld.geo_assigned__c = true;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement