We have a system that feeds vetted Accounts into our Salesforce, but the process originates in the Salesforce Lead object. Thus when approved, the Account is created by feed, so the Account will always exist before conversion. So obviously I don't want to create another Account. I do want to create a new Opportunity set it at the existing Account. I'd like to add the converted as a Contact at the Account, too.

here's my start - I can convert, but not tell it to use the existing Account.

trigger LeadConverterNewInstitution on Account (after insert) {
    LeadStatus convertStatus = [
          select MasterLabel
          from LeadStatus
          where IsConverted = true
          limit 1

    List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
    List <id> AccLeadSubIds = new List <id>(); 

    for (Account NewAccount: Trigger.new){

      if (newAccount.SubmittedLeadID__c != ''){    



      List <lead> LeadsToConvert = [select id, Business_Line__c,  company, name from Lead where ID in: AccLeadSubIds and isConverted = false];

      for (Lead LTC : LeadsToConvert){

           Database.LeadConvert lc = new Database.LeadConvert(); 

          String oppName = LTC.Business_Line__c + ' ' + LTC.Company; 


          database.LeadConvertResult lcr = Database.convertLead(lc); 

  • submittedLeadId is the field on the Account which identifies which Lead was sent to the external system.
    – jaw999
    Commented Nov 6, 2014 at 18:45

1 Answer 1


You can create map of populate in your Trigger.new loop storing the lead Ids (your custom submittedLeadId__c field) as the key, and the corresponding account Id as the value. Then inside your lead convert loop, use this map to fill in the setAccountId method on your Database.LeadCovert object, something like lc.setAccountId(acctIdsMap.get(LTC.Id));

  • Thanks! I just added lc.setAccountId(newAccount.id); just about setConvertedStatus. It is working. I am not sure if this is set to handle bulk inserts. Our feed sends 1 record at a time.
    – jaw999
    Commented Nov 6, 2014 at 20:14
  • No problem. I didn't catch it earlier because of the formatting (you're not to blame, Stack Exchange code formatting takes some getting used to) but I see now that you have your leads to convert query inside your loop over the trigger records. You'll definitely want to move that outside of said loop. If you ever do any bulk upload of 100+ Accounts (possibly less, depending on other circumstances) you'll get a SOQL query limit exception. Your code is actually already bulkified, which is why I suggested building and using the map.
    – kbentsen
    Commented Nov 6, 2014 at 20:39
  • Hmm. If I take my query out of the loop, I lose my newAccountId variable.
    – jaw999
    Commented Nov 6, 2014 at 20:52
  • That's why I suggested building the map :) You'll be able to preserve the account Id outside of the Trigger.new loop.
    – kbentsen
    Commented Nov 6, 2014 at 21:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .