0

I am trying to convert multiple leads in apex. Here is the process i am following.

First check there is an account / contact similar to the lead, if there is then assign the accountid in the leadconvert record else leave them blank so that a new account is created.

This works fine, till the point where i dont have a similar account in the system but there are similar leads. For example i have leads with different names but with same company. So now if i convert the lead i have multiple duplicate accounts because when i do a duplicate account check before the conversion, the accounts dont exist and i dont set the setAccountId and when the first leadconvert record gets converted, new account gets created, the second leadconvert record with same company is converted it also creates a new account. Since i am converting multiple leads in list i dont want to be doing the check every leadconvert record just before the conversion for the fear of hitting the governor limits.

Can anyone think of an alternate way to avoid duplicate accounts if there are similar company leads

  • 1
    in your trigger; take your list of leads and create a set of lead.company - then look through that set using contains() before adding a new leadConvert record – cropredy Feb 10 '15 at 2:23
  • Lets say there are 3 leads with same company name, but the lead names are different, so i would need 1 account and 3 contacts. I would need to create leadcovert records for all 3 but cant set the accountid as i dont have the accountid as the accountid will be set only when the first lead converts – Prady Feb 10 '15 at 2:45
  • Marty has a decent solution below; you could also adapt approach to do leadConvert in two passes – cropredy Feb 10 '15 at 17:42
1

Prady, without knowing exactly how you've implemented your business logic, I suggest the following approach, which may be least disruptive to your existing code: Create a utility method (e.g., LeadUtil.mapAccountIds()) that takes a List<Lead> parameter and returns a Map<Id, Id>.

The idea is that LeadUtil.mapAccountIds() performs a few key functions:

  1. Compile a unique set of expected Account records, based on values in the given leads. For example, if you have two leads with Company being "Acme Corporation", the unique set should only contain one item for "Acme Corporation".
  2. Search the system for matching Account records
  3. For any remaining expected Account records that were not found, meaning that these are net-new accounts, create new Account records
  4. Map the Account IDs of the matching Account records and newly created Account records to the given leads, using each Lead ID as a key
  5. Return the map of Account IDs

Then, in your existing code, all you need to do is:

// Map the leads to accounts for conversion
Map<Id, Id> accountIdsByLeadId =
    LeadUtil.mapAccountIds(selectedLeads);

// Use the map of Account IDs to inform the
// conversion of the selected leads
...
| improve this answer | |
  • Thanks... i might not be able to convert leads which have different names, which could be contacts with the same account. But i think we can live with that – Prady Feb 10 '15 at 4:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.