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
PlainviewPlainview 

Assign Leads to default user in case of duplicate Accounts.

Hello,

I am a brand spanking new developer and I have code that is working (!!) for us now in our Production instance - it assigns new Leads to the Owners of matched Existing Accounts.

To level it up a bit, we would like the code to also route leads to a default user (who will function as the tie breaker) when the lead is matched to two Account Records for the same company name that have different owners. I was provided with a helpful suggestion, which was to copy the last logic loop to do this, but have no idea how to write the code myself, although the comment makes sense to me. The comment was:

"I would add some if/then logic to your leadAccountIds loop - use the same containsKey logic you have in the bottom loop to detect if a value is already in the Map for that Account - if so, then do another put (just copy the lines you have) and substitute in the "default user".

The "default user" would be MNeeley with user id 00500000006pFs8.

Can anyone please give a beginner the remaining lines of code that are needed? I really appreciate any help with this!

Here's the code as it is now:

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, OwnerId, Name FROM Account WHERE Name IN: companies];
Map<String, Id> acctNameId=new Map<String, Id>();
Map<String, Id> acctNameOwner=new Map<String, Id>();

For (Account a:leadAccountIds){
  acctNameId.put(a.name,a.Id);
  acctNameOwner.put(a.name,a.ownerId);

}

For (Lead l2:trigger.new){
  if(acctNameId.containsKey(l2.company)){
    l2.Account__c=acctNameId.get(l2.company);
    l2.ownerId=acctNameOwner.get(l2.company);

  }
  
}

}
logontokartiklogontokartik


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

// One this about maps is that it would only store one key, so even if you have 2 accts with same name, it would overrid// the first one. So instead build a map, something like below. 
Map<String,Set<String>> acctLeadsMap = new Map<String,Set<String>>();

for(Account acc : leadAccountIds){
   Set<String> ownerSet;
   if(acctLeadsMap.get(acc.Name.toLowerCase()) != null){
       ownerSet = acctLeadsMap.get(acc.Name.toLowerCase());
    }else{
        ownerSet = new Set<String>();
    }
    ownerSet.add(acc.OwnerId);
    acctLeadsMap.put(acc.Name.toLowerCase(),ownerSet);
}

for(Lead l : Trigger.new){
   if(acctLeadsMap.containsKey(l.company.toLowerCase())){ // if the account name matches, find how many ownerids.
        List<String> ownerList = acctLeadsMap.get(company.toLowerCase());
        if(ownerList.size() > 1)
           l.OwnerId = '00500000006pFs8';//Default Owner
        else
           l.OwnerId = ownerList[0];
   }

}
The above code should meet your requirement needs.

I am basically using a Map of Account to set of ownerids, so for every same account name, I am storing all different OwnerIds in a set. Later in the 2nd Lead Loop if I find a match by Name in the Map, I look to see if the size of the set is greater than 1, which tells me that there are 2 owners which are different for the account. 

Hope this helps.

PS: I handwrote this code, so if any compile errors please excuse