Advertisement
Guest User

assign lead

a guest
Oct 16th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 22.67 KB | None | 0 0
  1. public class assignLeads {
  2.  
  3.    
  4.     public void assignRelatedAccount(){
  5.           Set<String> domains = new Set<String>();
  6.         Set<String> ldNames = new Set<String>();
  7.         Set<String> ldWebs = new Set<String>();
  8.         List<Lead> updatelds = new List<Lead>();
  9.         List<Lead> roundRobinLds = new List<Lead>();
  10.         Set<ID> leadsWithAccounts = new Set<ID>();
  11.         Map<String, Account> accDomMap = new Map<String, Account>();
  12.         Map<String, Contact> conDomMap = new Map<String,Contact>();
  13.         Map<String, Account> accWebMap = new Map<String, Account>();
  14.         Map<String, Contact> conWebMap = new Map<String, Contact>();
  15.         Map<String,Account> accNameMap = new Map<String, Account>();
  16.         Set<String> salesRoles = new Set<String>{'Large Accounts', 'Strategic Accounts', 'SDR', 'Services', 'CSM'};
  17.        
  18.         Set<String> personalDomains = new Set<String>();
  19.        
  20.         for(Personal_domain__c pd :[SELECT domain_name__c FROM Personal_Domain__c]){
  21.             personalDomains.add(pd.domain_Name__c);
  22.         }
  23.         Boolean found = false;
  24.         for(Lead ld : (List<Lead>)trigger.new){
  25.             domains.add(ld.domainName__c);
  26.             ldNames.add(ld.company);
  27.             ldWebs.add(ld.website);
  28.         }
  29.         //Gather all accounts that have domain names we pulled from the leads.  This is how we will match records
  30.         //Will iterate over a list and store values in a map
  31.         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
  32.                            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'
  33.                            ORDER BY LastActivityDate DESC LIMIT 50000]){
  34.                                system.debug('Account Name: '+acc.name+'.  Account Domain: '+acc.domainName__c+'.  Account website: '+acc.website+'.  Master Record? '+acc.master_record__c);
  35.                                //if domainName isn't null, populate domain key map
  36.                                if(acc.domainName__c != null){
  37.                                    accDomMap.put(acc.domainName__c, acc);
  38.                                }
  39.                                //if website isn't null populate website key map
  40.                                if(acc.website != null){
  41.                                    system.debug('Website: '+acc.website);
  42.                                    accWebMap.put(acc.website, acc);
  43.                                }
  44.                                //Name is a system required field so we always populate the name key map
  45.                                accNameMap.put(acc.name, acc);
  46.                            }
  47.         for(Lead ld : (List<Lead>)trigger.new){
  48.             system.debug('Lead Company: '+ld.company+'.  Lead Domain: '+ld.domainName__c+'.  Lead website: '+ld.website);
  49.            
  50.             //preset each check box to true and then flag false if match is found
  51.             ld.no_matches_found_sales_queue__c = true;
  52.             //Make sure we have accounts to look at
  53.             //if not break the loop
  54.             if(accDomMap.isEmpty() && accNameMap.isEmpty() && accWebMap.isEmpty()){
  55.                break;
  56.             }
  57.             //first things first we check if there is an exact domain match and exact company match, and see if we have a master record...
  58.             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){
  59.                 //next make sure the account owner is a sales/csm user before assigning lead
  60.                 //to the account owner. Then relate the account to the lead even though
  61.                 //it will still go through round robin territory assignment
  62.                 if(salesRoles.contains(accDomMap.get(ld.domainName__c).owner.UserRole.name)){
  63.                     ld.ownerId = accDomMap.get(ld.domainName__c).ownerId;
  64.                     ld.no_matches_found_sales_queue__c = false;
  65.                     system.debug('Assigned Lead in 1');
  66.                 }
  67.                 //if lead state/number of employees is blank and account state/number of employees isn't, then populate the lead
  68.                 if(ld.state == null && accDomMap.get(ld.domainName__c).billingState!= null){
  69.                     ld.state = accDomMap.get(ld.domainName__c).billingState;
  70.                 }
  71.                 if(ld.numberOfEmployees == null && accDomMap.get(ld.domainName__c).numberOfEmployees != null){
  72.                     ld.numberOfEMployees = accDomMap.get(ld.domainName__c).numberOfEmployees;
  73.                 }
  74.                 //relate account to lead
  75.                 ld.related_account__c = accDomMap.get(ld.domainName__c).id;
  76.             }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){
  77.                 if(salesRoles.contains(accNameMap.get(ld.company).owner.UserRole.name)){
  78.                     ld.ownerId = accNameMap.get(ld.company).ownerId;
  79.                     ld.no_matches_found_sales_queue__c = false;
  80.                     system.debug('Assigned Lead in 2');
  81.                 }
  82.                 if(ld.state == null && accNameMap.get(ld.domaiNname__c).billingState != null){
  83.                     ld.state = accNameMap.get(ld.domainName__c).billingState;
  84.                 }
  85.                 if(ld.numberOfEmployees == null && accNameMap.get(ld.domainName__c).numberOfEMployees != null){
  86.                     ld.numberOfEMployees = accNameMap.get(ld.domainName__c).numberOfEmployees;
  87.                 }
  88.                 ld.related_account__c = accNameMap.get(ld.company).id;
  89.             }else if(accDomMap.containsKey(ld.domainName__c) && accDomMap.get(ld.domainName__c).master_record__c == true){
  90.                 If(salesRoles.contains(accDomMap.get(ld.domainName__c).owner.UserRole.name)){
  91.                     ld.ownerId = accDomMap.get(ld.domainName__c).ownerid;
  92.                     ld.no_matches_found_sales_queue__c = false;
  93.                     system.debug('Assigned Lead in 3');
  94.                 }
  95.                 if(ld.state == null && accDomMap.get(ld.domainName__c).billingState!= null){
  96.                     ld.state = accDomMap.get(ld.domainName__c).billingState;
  97.                 }
  98.                 if(ld.numberOfEmployees == null && accDomMap.get(ld.domainName__c).numberOfEmployees != null){
  99.                     ld.numberOfEMployees = accDomMap.get(ld.domainName__c).numberOfEmployees;
  100.                 }
  101.                 ld.related_account__c = accDomMap.get(ld.domainName__c).id;
  102.             }else If(accNameMap.containsKey(ld.company) && accNameMap.get(ld.company).master_record__c == true){
  103.                 if(salesRoles.contains(accNameMap.get(ld.company).owner.UserRole.name)){
  104.                     ld.ownerId = accNameMap.get(ld.company).ownerId;
  105.                     ld.no_matches_found_sales_queue__c = false;
  106.                     system.debug('Assigned Lead in 4');
  107.                 }
  108.                 if(ld.state == null && accNameMap.get(ld.domaiNname__c).billingState != null){
  109.                     ld.state = accNameMap.get(ld.domainName__c).billingState;
  110.                 }
  111.                 if(ld.numberOfEmployees == null && accNameMap.get(ld.domainName__c).numberOfEMployees != null){
  112.                     ld.numberOfEMployees = accNameMap.get(ld.domainName__c).numberOfEmployees;
  113.                 }
  114.                 ld.related_account__c = accNameMap.get(ld.company).id;
  115.             }else if(accWebMap.containsKey(ld.website) && accWebMap.get(ld.website).master_record__c == true){
  116.                 If(salesRoles.contains(accWebMap.get(ld.website).owner.UserRole.name)){
  117.                     ld.ownerId = accWebMap.get(ld.website).ownerid;
  118.                     ld.no_matches_found_sales_queue__c = false;
  119.                     system.debug('Assigned Lead in 5');
  120.                 }
  121.                 if(ld.state == null && accWebMap.get(ld.domaiNname__c).billingState != null){
  122.                     ld.state = accWebMap.get(ld.domainName__c).billingState;
  123.                 }
  124.                 if(ld.numberOfEmployees == null && accWebMap.get(ld.domainName__c).numberOfEMployees != null){
  125.                     ld.numberOfEMployees = accWebMap.get(ld.domainName__c).numberOfEmployees;
  126.                 }
  127.                 ld.related_account__c = accWebMap.get(ld.website).id;
  128.             }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){
  129.                 If(salesRoles.contains(accWebMap.get(ld.website).owner.UserRole.name)){
  130.                     ld.ownerId = accWebMap.get(ld.website).ownerid;
  131.                     ld.no_matches_found_sales_queue__c = false;
  132.                     system.debug('Assigned Lead in 6');
  133.                 }
  134.                 if(ld.state == null && accWebMap.get(ld.domaiNname__c).billingState != null){
  135.                     ld.state = accWebMap.get(ld.domainName__c).billingState;
  136.                 }
  137.                 if(ld.numberOfEmployees == null && accWebMap.get(ld.domainName__c).numberOfEMployees != null){
  138.                     ld.numberOfEMployees = accWebMap.get(ld.domainName__c).numberOfEmployees;
  139.                 }
  140.                 ld.related_account__c = accWebMap.get(ld.website).id;
  141.             }else if(accDomMap.containsKey(ld.domainName__c) && accNameMap.containsKey(ld.company) && accDomMap.get(ld.domainName__c).id == accNameMap.get(ld.company).id){
  142.                 if(salesRoles.contains(accNameMap.get(ld.company).owner.UserRole.name)){
  143.                     ld.ownerId = accNameMap.get(ld.company).ownerid;
  144.                     ld.no_matches_found_sales_queue__c = false;
  145.                     system.debug('Assigned Lead in 7');
  146.                 }
  147.                 if(ld.state == null && accNameMap.get(ld.domaiNname__c).billingState != null){
  148.                     ld.state = accNameMap.get(ld.domainName__c).billingState;
  149.                 }
  150.                 if(ld.numberOfEmployees == null && accNameMap.get(ld.domainName__c).numberOfEMployees != null){
  151.                     ld.numberOfEMployees = accNameMap.get(ld.domainName__c).numberOfEmployees;
  152.                 }
  153.                 ld.related_account__c = accNameMap.get(ld.company).id;
  154.             }else if(accWebMap.containsKey(ld.website) && accNameMap.containsKey(ld.company) && accWebMap.get(ld.website).id == accNameMap.get(ld.company).id){
  155.                 if(salesRoles.contains(accNameMap.get(ld.company).owner.UserRole.name)){
  156.                     ld.ownerId = accNameMap.get(ld.company).ownerid;
  157.                     ld.no_matches_found_sales_queue__c = false;
  158.                     system.debug('Assigned Lead in 8');
  159.                 }
  160.                 if(ld.state == null && accNameMap.get(ld.domaiNname__c).billingState != null){
  161.                     ld.state = accNameMap.get(ld.domainName__c).billingState;
  162.                 }
  163.                 if(ld.numberOfEmployees == null && accNameMap.get(ld.domainName__c).numberOfEMployees != null){
  164.                     ld.numberOfEMployees = accNameMap.get(ld.domainName__c).numberOfEmployees;
  165.                 }
  166.                 ld.related_account__c = accNameMap.get(ld.company).id;
  167.             }else if(accDomMap.containsKey(ld.domainName__c) && accDomMap.get(ld.domainName__c).billingState != null && accDomMap.get(ld.domainName__c).numberOfEmployees != null){
  168.                 if(salesRoles.contains(accDomMap.get(ld.domainName__c).owner.UserRole.name)){
  169.                     ld.ownerId = accDomMap.get(ld.domainName__c).ownerId;
  170.                     ld.no_matches_found_sales_queue__c = false;
  171.                     system.debug('Assigned Lead in 9');
  172.                 }
  173.                 if(ld.state == null && accDomMap.get(ld.domainName__c).billingState!= null){
  174.                     ld.state = accDomMap.get(ld.domainName__c).billingState;
  175.                 }
  176.                 if(ld.numberOfEmployees == null && accDomMap.get(ld.domainName__c).numberOfEmployees != null){
  177.                     ld.numberOfEMployees = accDomMap.get(ld.domainName__c).numberOfEmployees;
  178.                 }
  179.                 ld.related_account__c = accDomMap.get(ld.domainName__c).id;
  180.             }else if(accNameMap.containsKey(ld.company) && accNameMap.get(ld.company).billingState != null && accNameMap.get(ld.company).numberOfEmployees != null){
  181.                 if(salesRoles.contains(accNameMap.get(ld.company).owner.UserRole.name)){
  182.                     ld.ownerId = accNameMap.get(ld.company).ownerId;
  183.                     ld.no_matches_found_sales_queue__c = false;
  184.                     system.debug('Assigned Lead in 10');
  185.                 }
  186.                 if(ld.state == null && accNameMap.get(ld.domaiNname__c).billingState != null){
  187.                     ld.state = accNameMap.get(ld.domainName__c).billingState;
  188.                 }
  189.                 if(ld.numberOfEmployees == null && accNameMap.get(ld.domainName__c).numberOfEMployees != null){
  190.                     ld.numberOfEMployees = accNameMap.get(ld.domainName__c).numberOfEmployees;
  191.                 }
  192.                 ld.related_account__c = accNameMap.get(ld.company).id;
  193.             }else if(accWebMap.containsKey(ld.website) && accNameMap.containsKey(ld.company) && accWebMap.get(ld.website).id == accNameMap.get(ld.company).id){
  194.                 if(SalesRoles.contains(accDomMap.get(ld.domainName__c).owner.userRole.name)){
  195.                     ld.ownerId = accNameMap.get(ld.company).ownerId;
  196.                     ld.no_matches_found_sales_queue__c = false;
  197.                     system.debug('Assigned Lead in 11');
  198.                 }
  199.                 if(ld.state == null && accNameMap.get(ld.domaiNname__c).billingState != null){
  200.                     ld.state = accNameMap.get(ld.domainName__c).billingState;
  201.                 }
  202.                 if(ld.numberOfEmployees == null && accNameMap.get(ld.domainName__c).numberOfEMployees != null){
  203.                     ld.numberOfEMployees = accNameMap.get(ld.domainName__c).numberOfEmployees;
  204.                 }
  205.                 ld.related_account__c = accNameMap.get(ld.company).id;
  206.             }else if(accDomMap.containsKey(ld.domainName__c)){
  207.                 if(SalesRoles.contains(accDomMap.get(ld.domainName__c).owner.userRole.name)){
  208.                     ld.ownerId = accDomMap.get(ld.domainName__c).ownerId;
  209.                     ld.no_matches_found_sales_queue__c = false;
  210.                     system.debug('Assigned Lead in 12');
  211.                 }
  212.                 if(ld.state == null && accDomMap.get(ld.domainName__c).billingState!= null){
  213.                     ld.state = accDomMap.get(ld.domainName__c).billingState;
  214.                 }
  215.                 if(ld.numberOfEmployees == null && accDomMap.get(ld.domainName__c).numberOfEmployees != null){
  216.                     ld.numberOfEMployees = accDomMap.get(ld.domainName__c).numberOfEmployees;
  217.                 }
  218.                 ld.related_account__c = accDomMap.get(ld.domainName__c).id;
  219.             }
  220.             }
  221.            
  222.         }
  223.    
  224.     public void territoryQueueAssignment(){
  225.         //create sets for each territory to compare against state field on lead
  226.         Set<String> NE = new Set<String>{'CT','MA','ME', 'NH','NY','RI','VT'};
  227.         Set<String> MDA = new Set<String>{'DC','DE','MD','NJ','PA'};
  228.         Set<String> SE = new Set<String>{'FL','GA','NC','PR','SC','VA','WV'};
  229.         Set<String> MDW = new Set<String>{'IA','IL','IN','KY','MI','MN','OH','TN','WI'};
  230.         Set<String> SO = new Set<String>{'AL','AR','KS','LA','MO','MS','ND','NE','OK','SD','TX'};
  231.         Set<String> NW = new Set<String>{'OR','WA','WY','AK','ID','MT'};
  232.         Set<String> SW = new Set<String>{'AZ','CO','HI','NM','NV','UT'};
  233.         Set<String> CA = new Set<String>{'CA'};
  234.         //create maps for storing previous and current lead data
  235.         Map<Id, Lead> oldMap = (Map<Id,Lead>)trigger.oldMap;
  236.         Map<Id, Lead> newMap = (Map<Id, Lead>)trigger.newMap;
  237.         for(Lead ld : (List<Lead>)trigger.new){
  238.             //make sure this is a lead we want to be assigning by check to see if criteria are met to skip lead routing
  239.                 if(ld.no_matches_found_sales_queue__c == false || ld.ByPass_Lead_Assignment__c == true || ld.leadSource == 'zoomInfo' || ld.leadSource == 'Contractor Research'){
  240.                     continue;
  241.                 }
  242.                 //if lead is being manually reassigned, skip the lead routing
  243.                 if(oldMap != null){
  244.                     if(oldMap.get(ld.id).ownerId != ld.ownerId /*|| (oldMap.get(ld.id).related_account__c != null && ld.related_account__c == null)*/){
  245.                             continue;
  246.                         }
  247.                     }
  248.                
  249.                 //if we have number of employees and territory we have everything we need to route the leads correctly
  250.                 //otherwise we will route them later based on lack of information
  251.             if(ld.numberOfEmployees >99 && ld.numberOfEmployees < 1000){//criteria for MM AE leads
  252.                 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
  253.                             //Assign to pro West queue
  254.                             ld.ownerId = '00G34000003zRIf';
  255.                             ld.geo_assigned__c = true;
  256.                         }else if(ld.territory2__c == 'SO' || ld.territory2__c == 'MDW' || SO.contains(ld.state) || MDW.contains(ld.state)){//MM AE Central Region territores
  257.                             //assign to Pro Central queue
  258.                             ld.ownerId = '00G34000003zRIV';
  259.                             ld.geo_assigned__c = true;
  260.                         }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
  261.                             //assign to Pro East Queue
  262.                             ld.ownerId = '00G34000003zRIG';
  263.                             ld.geo_assigned__c = true;
  264.                            
  265.                         }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)){
  266.                             //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
  267.                             //assign to ALL MM AE queue
  268.                             ld.ownerId = '00G34000003c6tf';
  269.                             ld.geo_assigned__c = true;
  270.                         }
  271.             }else if(ld.numberOfEmployees >= 1000 || ld.numberOfEmployees <100){//Criteria for SDR leads
  272.                        
  273.                         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
  274.                             //SDR West queue
  275.                             ld.ownerId = '00G340000032077';
  276.                             ld.geo_assigned__c = true;
  277.                         }else if(ld.territory2__c == 'MDW' || ld.territory2__c == 'SO' || MDW.contains(ld.state)||SO.contains(ld.state)){//SDR Central Region Territories
  278.                              //SDR central queue  
  279.                              ld.ownerId = '00G34000003206x';
  280.                              ld.geo_assigned__c = true;
  281.                         }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
  282.                            //SDR East queue
  283.                            ld.ownerId = '00G34000003206s';
  284.                             ld.geo_assigned__c = true;
  285.                         }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)){
  286.                             //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
  287.                             //assign to ALL SDR queue
  288.                             ld.ownerId = '00G34000003c6ta';
  289.                             ld.geo_assigned__c = true;
  290.                         }
  291.             }else if(ld.numberOfEmployees == null && ld.state != null){//if we don't have number of employees then we route to SDRs by region
  292.                 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
  293.                             //SDR West queue
  294.                             ld.ownerId = '00G340000032077';
  295.                             ld.geo_assigned__c = true;
  296.                         }else if(ld.territory2__c == 'MDW' || ld.territory2__c == 'SO' || MDW.contains(ld.state)||SO.contains(ld.state)){//SDR Central Region Territories
  297.                              //SDR central queue  
  298.                              ld.ownerId = '00G34000003206x';
  299.                              ld.geo_assigned__c = true;
  300.                         }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
  301.                            //SDR East queue
  302.                            ld.ownerId = '00G34000003206s';
  303.                             ld.geo_assigned__c = true;
  304.                         }
  305.             }else if (ld.state == null && ld.numberOfEmployees == null){
  306.                 //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
  307.                 //assign to ALL SDR queue
  308.                 ld.ownerId = '00G34000003c6ta';
  309.                 ld.geo_assigned__c = true;
  310.             }
  311.                
  312.  
  313.                
  314.             }
  315.     }
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement