You need to sign in to do that
Don't have an account?
Plainview
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);
}
}
}
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);
}
}
}
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