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
DevidDevid 

How to know user want to create new account or selected already exists while lead conversion?

I have written a trigger on lead to enhance the lead conversion process. When converting a lead, the user has the ability to create a new account or choose existing. If a user tries to create a new account we need to run validation to see if there is already an account with the same custom field value (Company_Id__c). If there is another account with the same Company_Id__c, then we need to show an error message to the user.
But I got stuck here in code to get to know how to check that user want to create new account or selected existing account?

Here is my code.
 
trigger LeadTrigger on Lead (After Update) {
	Map<Id, Lead> = Trigger.oldMap;
	for(Lead lead : Trigger.new){
		if(lead.isConverted && oldMap.get(lead.id).isConverted) continue; //only process newly converted leads
		//check converted leads only.
		if (oldMap.get(lead.Id).isConverted == false && lead.isConverted) {
			//How to check in code that user already existing account or new one he want to create.
		}
	}
}
Please help
Best Answer chosen by Devid
Gaurav Sharma 472Gaurav Sharma 472
Not sure you can get that.
but you can get selected account (which were already there or newly creeated)  with field  ConvertedAccountId .
after  that you can do your validations.

I would like to suggest an alternate approach assuming that (Company_Id__c)  should not be duplicated in your org.
If this is the case,make this field unique. No need for any validations.