Skip to main content
When I convert a lead , I want that all the leads associated with the same company should also be converted and added as Contact on same Account. I have written follwing trigger but It is creating double contact for the lead on which I am clicking the convert button

 

trigger ConvertLeads on Account (after insert) {

 

    LeadStatus convertStatus = [

 

          select MasterLabel

 

          from LeadStatus

 

          where IsConverted = true

 

          limit 1

 

     ];

 

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

 

    //Fetch all the Leads with same Company Name

 

    for (Account NewAccount: Trigger.new){

 

        List <lead> LeadsToConvert = [select id,company, name from Lead where company = : NewAccount.name and isConverted = false];

 

        system.debug('LeadstoConvert' + LeadsToConvert);

 

        for (Lead LTC : LeadsToConvert){

 

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

 

           lc.setLeadId(LTC.Id);

 

           lc.setAccountId(NewAccount.ID);

 

           lc.setConvertedStatus(convertStatus.MasterLabel);

 

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

 

        }

 

    }

 

}
10 respuestas
  1. Ines Garcia (get: Agile) Forum Ambassador
    17 ene 2019, 7:11
    Thats more of my confort zone :p

     

    trigger the flow (maybe with process builder?) when a lead is converted,

     

    then hold the account variables you need like ID (so you can map other contacts to it), the name, email etc as you have for matching.

     

    Then loop through all leads that have not being converted where there is a match.

     

    hold them into a collection, then per each item in collection to create the new contacts under that account variable di

     

    have a look at this too for the custom conversion: https://automationchampion.com/tag/auto-convert-lead-using-process-builder/

     

     
0/9000