function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Afrose AhamedAfrose Ahamed 

Lead Convert - How to check existing Account choosen or new Account When lead convert using trigger

Hello All,
When a lead is converted, is it possible to find out, in an Apex trigger, whether the ConvertedAccountId is referecing to an existing Account or an Account created as a result of the conversion? Or in another word, can I find out whether a user selected an existing account or created a new account as part of the lead conversion process?

Thanks,

Afrose Ahamed 

Best Answer chosen by Afrose Ahamed
CharuDuttCharuDutt
Hii Afrose
Try Below Code
public class AccountFlags {
  public static Boolean hasInsertRun = false;
}
###############################################

trigger on Account (after insert) { 
AccountFlags.hasInsertRun = true; 
}

###############################################

trigger on Lead (after update) {
for(lead oLead : trigger.new){
if(!Trigger.oldMap.get[oLead.Id].IsConverted && oLead.IsConverted) {
      if(AccountFlags.hasInsertRun) {
        system.debug(This was a new account);
      } else {
        system.debug(Existing account);
      }
    }
  }
}
Please Mark It As Best Asnwer If It Helps
Thank You!

All Answers

SwethaSwetha (Salesforce Developers) 
HI Afrose,
A similar ask is posted here https://salesforce.stackexchange.com/questions/13047/is-there-a-way-to-determine-how-certain-account-has-been-created
Copying relevant text:
  1. Have a checkbox field on the Lead, Was_Lead__c (default: checked) that maps to a field on the Account. This value can be carried over on Lead conversion. If you need to trace it back to the original Lead, you could probably use a formula that provides the Lead's ID field. To configure this, go to Setup | Customize | Leads | Fields and look for the Map Lead Fields button in the Custom Fields section. 

  2. You could do a SOQL query on your closed Leads. The following query will provide you a list of all Accounts and Contacts that originated from a converted Lead. You could filter it on specific Account Ids, if necessary.

Get all converted Accounts and Contacts:

SELECT ConvertedAccountId,ConvertedContactId,Id FROM Lead WHERE Status = 'Qualified'

Find the original Lead for a specific Account:

SELECT Id FROM Lead WHERE ConvertedAccountId = '[AccountId]'
If this information helps, please mark the answer as best. Thank you
CharuDuttCharuDutt
Hii Afrose
Try Below Code
public class AccountFlags {
  public static Boolean hasInsertRun = false;
}
###############################################

trigger on Account (after insert) { 
AccountFlags.hasInsertRun = true; 
}

###############################################

trigger on Lead (after update) {
for(lead oLead : trigger.new){
if(!Trigger.oldMap.get[oLead.Id].IsConverted && oLead.IsConverted) {
      if(AccountFlags.hasInsertRun) {
        system.debug(This was a new account);
      } else {
        system.debug(Existing account);
      }
    }
  }
}
Please Mark It As Best Asnwer If It Helps
Thank You!
This was selected as the best answer
Afrose AhamedAfrose Ahamed

Hi @CharuDutt,

I copied your code and pasted in my org. but im getting error oldMap variable does not exist. Can you please help,

User-added image
Regards.
Afrose Ahamed M.G.

CharuDuttCharuDutt
Hii Afrose
It Should Be Like This " !Trigger.oldMap.get(oLead.Id).IsConverted"
Afrose AhamedAfrose Ahamed

Hi @CharuDutt,

 

Thank you so much working as expected.....