function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
iKnowSFDCiKnowSFDC 

Trouble getting all the values into a map

 

Hoping someone can point me in the right direction - I'm bumping against what I think is a logic issue here but can't see my way out of it.

 

I'm trying to create a map of accounts and domain names associated with the account.  The Domains field is a long text field and may have multiple domains listed separated by commas, i.e. bankofamerica.com, boa.com, bankamerica.com etc.

 

When a lead comes into the system, the email domain is parsed out and compared to the domains listed for each account and if a match is found, the lead type is set based on some values of the the account found.  I can get it working perfectly if there is only one domain listed, but as soon as there are multiple domains listed, I can't get the match to work.  

 

I have tried it using a SOSL query, however, I bump up against governor limits very quickly which is a problem when loading lists of leads. 

 

The lastest iteration takes a list of accounts with domains listed and puts them into a map<String, Account>. If there is more than one domain listed in the field, I'm parsing the domains into a list so I can put each domain in as a separate key for the account they belong to, however, I can't get the account to match up to the second domain when I loop through and am instead getting a null value for the account.  I have verified that the domain names are parsing out correctly and that the account remains in scope and the code loops through the generated list of domains for the account the right number of times, however, I can't get the account to go into the map the second time. 

 

My code is below - any suggestions/thoughts would be greatly appreciated!

 

public class setLeadType{

   public static void setLeadAccountType(Lead[] newLeads){
   	   List<String> typeValues = new List<String>();
       List<Account> accts = [SELECT Id, Name, Type, Partner_Status__c, Domains__c, Active_Subscription__c, 
                               		First_Subscription__c, Active_Funnel__c,Customer_Type_Subscription__c,Target_Category__c,
                                    Closed_Amount__c, Website, Closed_Subscription_Transactions__c
                               FROM Account 
                               WHERE Domains__c != NULL];
        map<String, Account> acctDomains = new Map<String,Account>();
       
 
        for(Account a : accts){
            if(a.Domains__c.containsNone(',')){
        		acctDomains.put(a.Domains__c, a);
            } else {
               	List<String> doms = a.Domains__c.split(',');
					for(String s : doms){
                 	   acctDomains.put(s, a);
//HERE IS WHERE I'M HAVING AN ISSUE - THE FIRST DOMAIN/ACCOUNT PAIR IS PUT INTO THE MAP, BUT THE SECOND IS NOT
               	}
           	}
         }
       
        for(Lead ls : newLeads){
            ls.Type_of_Lead__c = NULL;
            Account acct = new Account();
            acct = acctDomains.get(ls.Domain__c);
        	if(acct!=NULL){
                if(acct.Type == 'Partner' && string.isNotEmpty(acct.Partner_Status__c))
                    if(acct.Partner_Status__c != 'Prospect' || acct.Partner_Status__c  != 'Rejected' || 
                    acct.Partner_Status__c  != 'Terminated'){
                    typeValues.add('Existing Partner');
                }
                if(acct.Active_Subscription__c != False && acct.Closed_Subscription_Transactions__c>0){
                    typeValues.add('Active Subscription Customer');
                }    
            if(acct.Active_Funnel__c > 0){
                typeValues.add('Active Funnel');
            }
            if(acct.Active_Subscription__c == False && acct.Closed_Subscription_Transactions__c > 0){
                    typeValues.add('Previous Subscription Customer');
                } 
                if(acct.Closed_Amount__c > 0 && 
                   acct.Active_Subscription__c == False && 
                   acct.Closed_Subscription_Transactions__c == 0){
                    typeValues.add('Non-Subscription Purchase');
                } 
                if(string.isNotEmpty(acct.Target_Category__c) && acct.Target_Category__c == 'Data Warehouse'){
                    typeValues.add('Data Warehouse');
                } 
                if(string.isNotEmpty(acct.Target_Category__c)&& acct.Target_Category__c == 'Fortune/Forbes/Global'){
                     typeValues.add('Fortune/Global');
                }
            String lsType = string.join(typeValues, ';');
            ls.Type_of_Lead__c = lsType;
			
        	}
        }
    }
    public static void setLeadPersonalLeadType(Lead[] ignoreLeads){
        for(Lead l : ignoreLeads){
            l.Type_of_Lead__c = NULL;
        }
    }    
}

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
ForcepowerForcepower

JoAnn,

 

How are you concluding that only the second domain is getting into the Map? Can you do the following - put the code in bold and turn on debug:

 

for(String s : doms){
       acctDomains.put(s.trim(), a);
}

for (String s : acctDomains.keySet()) {
System.debug(s + ' ' + acctDomains.get(s));
}

All Answers

ForcepowerForcepower

JoAnn,

 

Try doing a trim before you put the domain into the Map:

 

acctDomains.put(s.trim(), a);

Best,
Ram
iKnowSFDCiKnowSFDC

Hi Ram, Thanks for the suggestion - I tried the trim() method and now am only getting the second value in the list into the map? Any thoughts? The format of the record I'm testing is as follows: 

 

Domains__c = 'testkaren.com, karentest.com'

 

        for(Account a : accts){
            if(a.Domains__c.containsNone(',')){
        		acctDomains.put(a.Domains__c, a);
            } else {
               	List<String> doms = a.Domains__c.split(',');
					for(String s : doms){
                        
                 	   acctDomains.put(s.trim(), a);
               	}
           	}
         }

 

ForcepowerForcepower

JoAnn,

 

How are you concluding that only the second domain is getting into the Map? Can you do the following - put the code in bold and turn on debug:

 

for(String s : doms){
       acctDomains.put(s.trim(), a);
}

for (String s : acctDomains.keySet()) {
System.debug(s + ' ' + acctDomains.get(s));
}
This was selected as the best answer
iKnowSFDCiKnowSFDC

Hi Ram,

 

Ok, I just tested that and verified that both values and the associated account are being put into the map, however, I finally figured out that the next for loop isn't executing - 

 

public class setLeadType{

   public static void setLeadAccountType(Lead[] newLeads){
   	   List<String> typeValues = new List<String>();
       List<Account> accts = [SELECT Id, Name, Type, Partner_Status__c, Domains__c, Active_Subscription__c, 
                               		First_Subscription__c, Active_Funnel__c,Customer_Type_Subscription__c,Target_Category__c,
                                    Closed_Amount__c, Website, Closed_Subscription_Transactions__c
                               FROM Account 
                               WHERE id = '001Q000000YcP3c'];
       map<String, Account> acctDomains = new Map<String,Account>();
 
       for(Account a : accts){
            if(a.Domains__c.containsNone(',')){
        		acctDomains.put(a.Domains__c, a);
                system.debug('acctsDomains before split>>>'+acctDomains.size());
            } else {
               	List<String> doms = a.Domains__c.split(',');
                	system.debug('size of list doms>>>'+doms.size());
					for(Integer x=0; x<doms.size(); x++){
                 	   acctDomains.put(doms[x].trim(), a);
                    	system.debug('trimmed domain name>>>'+doms[x].trim()+'<<<');    
               		}
           	}
         }
       system.debug('acctsDomains after split>>>'+acctDomains.size());
       Set<string> domains = acctDomains.keySet();
       for(String s : domains){
           system.debug('acct domains in acctDomains keyset>>>'+s);
           system.debug('acct name for keyset>>>'+s+'<<<'+acctDomains.get(s).name);
       }

//THIS LOOP ISN'T EXECUTING NOW for(Lead ls : newLeads){ system.debug('lead domain name>>>'+ls.Domain__c); ls.Type_of_Lead__c = NULL; Account acct = new Account(); acct = acctDomains.get(ls.Domain__c); if(acct!=NULL){ if(acct.Type == 'Partner' && string.isNotEmpty(acct.Partner_Status__c)) if(acct.Partner_Status__c != 'Prospect' || acct.Partner_Status__c != 'Rejected' || acct.Partner_Status__c != 'Terminated'){ typeValues.add('Existing Partner'); } if(acct.Active_Subscription__c != False && acct.Closed_Subscription_Transactions__c>0){ typeValues.add('Active Subscription Customer'); } if(acct.Active_Funnel__c > 0){ typeValues.add('Active Funnel'); } if(acct.Active_Subscription__c == False && acct.Closed_Subscription_Transactions__c > 0){ typeValues.add('Previous Subscription Customer'); } if(acct.Closed_Amount__c > 0 && acct.Active_Subscription__c == False && acct.Closed_Subscription_Transactions__c == 0){ typeValues.add('Non-Subscription Purchase'); } if(string.isNotEmpty(acct.Target_Category__c) && acct.Target_Category__c == 'Data Warehouse'){ typeValues.add('Data Warehouse'); } if(string.isNotEmpty(acct.Target_Category__c)&& acct.Target_Category__c == 'Fortune/Forbes/Global'){ typeValues.add('Fortune/Global'); } String lsType = string.join(typeValues, ';'); ls.Type_of_Lead__c = lsType; system.debug('lead status after trigger>>>'+ls.Type_Of_Lead__c); } } } public static void setLeadPersonalLeadType(Lead[] ignoreLeads){ for(Lead l : ignoreLeads){ l.Type_of_Lead__c = NULL; } } }

 

ForcepowerForcepower

It looks like maybe the list of leads you're passing in to this method is actually empty.

 

setLeadAccountType(Lead[] newLeads)
iKnowSFDCiKnowSFDC

Ok, finally solved this issue.  Thanks for your help!  You were right - the lead list was passing over empty, but didn't realize it as there was a formula field not behaving the way I expected. 

 

Thanks for your help!

ForcepowerForcepower
Glad to hear it. You're very welcome, JoAnn!
Ram