+ Start a Discussion
Shawn Reichner 29Shawn Reichner 29 

Lead Conversion Via Trigger Issues - Please Help

Awesome Devs!

I have the followign trigger that will fire upon a lead creation to auto convert the lead.  That is working like a charm, however the issue I am running into is doing a search for an existing account to append the Contact and Opportunity to that is created via the lead conversion. 

I have some logic built in that looks like it is not working to locate an existing account based on the Lead Company field, but my code is still creating a new account each time a lead is automaticlaly converted.  

Can anyone help with getting my code to properly append the lead to the existing account if one exists, and create a new one if one is not found?

Here is the trigger code:
 
Trigger AutoConverter on Lead (after insert) {
     LeadStatus convertStatus = [
          select MasterLabel
          from LeadStatus
          where IsConverted = true
          limit 1
     ];
     List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
	 MAP<Id,Account> AccountSearch = new MAP<Id,Account>([SELECT ID, Name FROM Account]);
    
     for (Lead lead: Trigger.new) {
          if (!lead.isConverted && lead.Company != null) {
              
              If(AccountSearch.equals(Lead.Company)){
               Account Acct = AccountSearch.get(Lead.Company);
               ID AcctId = Acct.Id;
               Database.LeadConvert lc = new Database.LeadConvert();
               String oppName = lead.Company+Lead.CreatedDate;
               
               lc.setLeadId(lead.Id);
               lc.setOpportunityName(oppName);
               lc.setConvertedStatus(convertStatus.MasterLabel);
               lc.setAccountId(AcctId);
               
               leadConverts.add(lc);
              }
              Else{
                  Database.LeadConvert lc = new Database.LeadConvert();
               String oppName = lead.Name;
               
               lc.setLeadId(lead.Id);
               lc.setOpportunityName(oppName);
               lc.setConvertedStatus(convertStatus.MasterLabel);
               
               leadConverts.add(lc);
              }
          }
     }

     if (!leadConverts.isEmpty()) {
          List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
     }
}

 
Best Answer chosen by Shawn Reichner 29
Steven NsubugaSteven Nsubuga
Hi Shawn, I have made a few changes.
Trigger AutoConverter on Lead (after insert) {
	LeadStatus convertStatus = [
		select MasterLabel
		from LeadStatus
		where IsConverted = true
		limit 1
	];
	List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
	MAP<Id,Account> AccountSearch = new MAP<Id,Account>([SELECT ID, Name FROM Account]);
    
	for (Lead lead: Trigger.new) {
		if (!lead.isConverted && lead.Company != null) {
		  
			Boolean accountSearchContainsLeadCompany = false;
			Id accountId = null;
			for (Account act: AccountSearch.values() {
				if (act.Name.equalsIgnoreCase(lead.Company) {
					accountSearchContainsLeadCompany = true;
					accountId = act.ID;
					break;
				}
			}  
		  
		  
			If(accountSearchContainsLeadCompany){
				Account Acct = AccountSearch.get(accountId);
				ID AcctId = Acct.Id;
				Database.LeadConvert lc = new Database.LeadConvert();
				String oppName = lead.Company+Lead.CreatedDate;

				lc.setLeadId(lead.Id);
				lc.setOpportunityName(oppName);
				lc.setConvertedStatus(convertStatus.MasterLabel);
				lc.setAccountId(AcctId);

				leadConverts.add(lc);
			}
			Else{
				Database.LeadConvert lc = new Database.LeadConvert();
				String oppName = lead.Name;

				lc.setLeadId(lead.Id);
				lc.setOpportunityName(oppName);
				lc.setConvertedStatus(convertStatus.MasterLabel);

				leadConverts.add(lc);
			}
		}
	}

     if (!leadConverts.isEmpty()) {
          List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
     }
}

 

All Answers

Steven NsubugaSteven Nsubuga
Hi Shawn, I have made a few changes.
Trigger AutoConverter on Lead (after insert) {
	LeadStatus convertStatus = [
		select MasterLabel
		from LeadStatus
		where IsConverted = true
		limit 1
	];
	List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
	MAP<Id,Account> AccountSearch = new MAP<Id,Account>([SELECT ID, Name FROM Account]);
    
	for (Lead lead: Trigger.new) {
		if (!lead.isConverted && lead.Company != null) {
		  
			Boolean accountSearchContainsLeadCompany = false;
			Id accountId = null;
			for (Account act: AccountSearch.values() {
				if (act.Name.equalsIgnoreCase(lead.Company) {
					accountSearchContainsLeadCompany = true;
					accountId = act.ID;
					break;
				}
			}  
		  
		  
			If(accountSearchContainsLeadCompany){
				Account Acct = AccountSearch.get(accountId);
				ID AcctId = Acct.Id;
				Database.LeadConvert lc = new Database.LeadConvert();
				String oppName = lead.Company+Lead.CreatedDate;

				lc.setLeadId(lead.Id);
				lc.setOpportunityName(oppName);
				lc.setConvertedStatus(convertStatus.MasterLabel);
				lc.setAccountId(AcctId);

				leadConverts.add(lc);
			}
			Else{
				Database.LeadConvert lc = new Database.LeadConvert();
				String oppName = lead.Name;

				lc.setLeadId(lead.Id);
				lc.setOpportunityName(oppName);
				lc.setConvertedStatus(convertStatus.MasterLabel);

				leadConverts.add(lc);
			}
		}
	}

     if (!leadConverts.isEmpty()) {
          List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
     }
}

 
This was selected as the best answer
Shawn Reichner 29Shawn Reichner 29
Steven,

Thank you so very much sir!  That worked like a charm!
Steven NsubugaSteven Nsubuga
My pleasure Shawn.