You need to sign in to do that
Don't have an account?
Lead Conversion Lead to contract
Hi,
I had written a class which will convert lead to Contract in between it will create Account and Address.
I dont want to create Contact but it is getting created.
I filled data with lead first and Last name.
Company field in lead is mandatory which is not in use for my requirement.
Here is the code I tried :
public class AutoConvertLead {
@InvocableMethod
public static void assignLeads(List<Id> LeadIds) {
try{
Map<String,Date> NameDOBMap = new Map<String,Date>();
List<String> leadNames = new List<String>();
List<Date> leadDOBs = new List<Date>();
List<Database.LeadConvert> massLeadConvert = new List<Database.LeadConvert>();
List<Account> accountsToUpdate = new List<Account>();
List<Account> accountsToInsert = new List<Account>();
List<Contract> contractsToInsert = new List<Contract>();
List<Address__c> addressesToInsert = new List<Address__c>();
//List<MemberPlan> memberplanToInsert = new List<MemberPlan>();
Map<String,Account> nameDOBToAccountMap = new Map<String,Account>();
Map<String,Account> newCreatedAccounts = new Map<String,Account>();
Map<ID,Id> leadProductMap = new Map<Id,Id>();
Map<ID,Id> leadHealthcareFacilityMap = new Map<ID, Id>();
//Id personAccountRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
LeadStatus cLeadStatus = [SELECT Id,MasterLabel FROM LeadStatus WHERE isConverted = true LIMIT 1];
// List<Lead> leads = [SELECT Id,Name,LastName,FirstName,Email,Birthdate__c,Service__c,Ethnicity__c, Race__c,Gender__c FROM Lead WHERE Id IN :LeadIds];
Map<Id,Lead> leadmap= new Map<Id,Lead>([SELECT Id,Name,LastName,FirstName,Email,Signature__c,Signature_Date_Time__c,Birthdate__c,Service__c,Ethnicity__c,MobilePhone,Phone,Race__c,Gender__c,City,Country,State,Street,PostalCode,Address_Type__c,Group_ID__c,Insurance_Provider__c,Is_Policy_Holder__c,Policy_Holder_First_Name__c,Policy_Holder_Last_Name__c,Policy_ID__c,Insurance__c FROM Lead WHERE Id IN :LeadIds]);
// for(Lead lead: leads) {
for(Lead lead: leadmap.values()) {
leadNames.add(String.valueOf(lead.Name));
leadDOBs.add(lead.Birthdate__c);
NameDOBMap.put(lead.Name, lead.Birthdate__c);
leadProductMap.put(lead.Id,lead.Service__c);
//leadHealthcareFacilityMap.put(lead.Id,lead.Depot_Name__c);
}
Map<Id, Account> existingAccounts = new Map<Id, Account>([SELECT Id, Birthdate__c, Name, Gender__c FROM Account WHERE Name IN :NameDOBMap.KeySet() AND Birthdate__c IN :NameDOBMap.values()]);
for(account acc: existingAccounts.values()){
}
System.debug('Check 1'+existingAccounts);
for(Account accc: existingAccounts.values()){
nameDOBToAccountMap.put(accc.Name+accc.Birthdate__c, accc);
}
// for(Lead ld : leads){
for(Lead ld: leadmap.values()) {
if(!nameDOBToAccountMap.containsKey(ld.Name+ld.Birthdate__c)){
Account existingAccount = new Account();
existingAccount.Name = ld.Name;
existingAccount.First_Name__c = ld.LastName;
existingAccount.First_Name__c = ld.FirstName;
existingAccount.Email__c = ld.Email;
existingAccount.Gender__c = ld.Gender__c;
//existingAccount.RecordTypeId = personAccountRecordTypeId;
existingAccount.Ethnicity__c = ld.Ethnicity__c;
existingAccount.Race__c = ld.Race__c;
existingAccount.Gender__c = ld.Gender__c;
existingAccount.Birthdate__c = ld.Birthdate__c;
existingAccount.Mobile__c = ld.MobilePhone;
existingAccount.Mobile__c = ld.Phone;
existingAccount.Insurance_Options__c = ld.Insurance__c;
existingAccount.Insurance_Provider__c = ld.Insurance_Provider__c;
existingAccount.Policy_Holder_First_Name__c = ld.Policy_Holder_First_Name__c;
existingAccount.Policy_Holder_Last_Name__c = ld.Policy_Holder_Last_Name__c;
existingAccount.Group_Number__c = ld.Group_ID__c;
existingAccount.Member_ID__c = ld.Policy_ID__c;
existingAccount.Is_Policy_Holder__c = ld.Is_Policy_Holder__c;
//existingAccount.Address_Line_2__c = ld.Address_Line_2__c;
accountsToInsert.add(existingAccount);
}
}
if(accountsToInsert.size()>0){
insert accountsToInsert;
}
system.debug('accountsToInsert'+accountsToInsert);
for(account acc: accountsToInsert){
newCreatedAccounts.put(acc.First_Name__c+acc.Last_Name__c, acc);
}
system.debug('newCreatedAccounts'+newCreatedAccounts);
//for(Lead lead : leads){
for(Lead lead : leadmap.values()){
System.debug('Check 2');
Database.LeadConvert LeadConvert = new Database.LeadConvert();
LeadConvert.setLeadId(lead.Id);
LeadConvert.setConvertedStatus(cLeadStatus.MasterLabel);
LeadConvert.setDoNotCreateOpportunity(True);
if(lead.Name != null && lead.Birthdate__c != null){
if(nameDOBToAccountMap.containsKey(lead.Name+lead.Birthdate__c) && nameDOBToAccountMap.get(lead.Name+lead.Birthdate__c)!=Null){
system.debug('existingAccount==>'+nameDOBToAccountMap.get(lead.Name+lead.Birthdate__c));
Account existingAccount = new Account();
existingAccount.Id = nameDOBToAccountMap.get(lead.Name+lead.Birthdate__c).id;
system.debug('existingAccount'+existingAccount);
existingAccount.Ethnicity__c = lead.Ethnicity__c;
existingAccount.Race__c = lead.Race__c;
existingAccount.Gender__c = lead.Gender__c;
existingAccount.Mobile__c = lead.MobilePhone;
existingAccount.Email__c = lead.Email;
existingAccount.Mobile__c = lead.Phone;
existingAccount.Mobile__c = lead.MobilePhone;
//existingAccount.Address_Line_2__c = lead.Address_Line_2__c;
existingAccount.BillingCountry = lead.Country;
existingAccount.BillingPostalCode = lead.PostalCode;
existingAccount.BillingState = lead.State;
existingAccount.BillingStreet = lead.Street;
existingAccount.BillingCity = lead.City;
existingAccount.Insurance_Options__c = lead.Insurance__c;
existingAccount.Insurance_Provider__c = lead.Insurance_Provider__c;
existingAccount.Policy_Holder_First_Name__c = lead.Policy_Holder_First_Name__c;
existingAccount.Policy_Holder_Last_Name__c = lead.Policy_Holder_Last_Name__c;
existingAccount.Group_Number__c = lead.Group_ID__c;
existingAccount.Member_ID__c = lead.Policy_ID__c;
existingAccount.Is_Policy_Holder__c = lead.Is_Policy_Holder__c;
accountsToUpdate.add(existingAccount);
LeadConvert.setAccountId(existingAccount.Id);
}
else{
system.debug('lead.Name+lead.Birthdate__c'+lead.FirstName+lead.LastName);
if(newCreatedAccounts.containskey(lead.FirstName+lead.LastName) && newCreatedAccounts.get(lead.FirstName+lead.LastName)!= Null){
LeadConvert.setAccountId(newCreatedAccounts.get(lead.FirstName+lead.LastName).Id);
}
}
System.debug('LeadConvert.getAccountId()'+LeadConvert.getAccountId());
massLeadConvert.add(LeadConvert);
system.debug('massLeadConvert'+massLeadConvert);
}
}
if(!accountsToUpdate.isEmpty()){
system.debug('accountsToUpdate'+accountsToUpdate);
update accountsToUpdate;
system.debug('accountsToUpdate'+accountsToUpdate);
}
if(!massLeadConvert.isEmpty()){
system.debug('massLeadConvert'+massLeadConvert);
List<Database.LeadConvertResult> lcr = Database.convertLead(massLeadConvert);
for(Database.LeadConvertResult leadConvertResult : lcr) {
system.debug('leadConvertResult'+leadConvertResult);
system.debug('accountid'+existingAccounts.containskey(leadConvertResult.accountid));
if(leadConvertResult.isSuccess() ) {
Contract contract = new Contract();
contract.Status = 'Draft';
contract.AccountId = leadConvertResult.getAccountId();
contract.StartDate = system.today();
contract.ContractTerm = 12;
if(leadProductMap.containskey(leadConvertResult.getLeadId()) && leadProductMap.get(leadConvertResult.getLeadId())!=Null){
contract.Service__c = leadProductMap.get(leadConvertResult.getLeadId());
}
//if(leadHealthcareFacilityMap.containskey(leadConvertResult.getLeadId()) && leadHealthcareFacilityMap.get(leadConvertResult.getLeadId())!=Null ){
//contract.Depot_Name__c = leadHealthcareFacilityMap.get(leadConvertResult.getLeadId());
if(leadmap.containskey(leadConvertResult.getLeadId()) ) {
contract.BillingCity= leadmap.get(leadConvertResult.getLeadId()).City;
contract.BillingState= leadmap.get(leadConvertResult.getLeadId()).State;
contract.BillingCountry = leadmap.get(leadConvertResult.getLeadId()).Country;
contract.BillingStreet=leadmap.get(leadConvertResult.getLeadId()).Street;
contract.BillingPostalCode=leadmap.get(leadConvertResult.getLeadId()).PostalCode;
contract.ShippingCity = leadmap.get(leadConvertResult.getLeadId()).City;
contract.ShippingState = leadmap.get(leadConvertResult.getLeadId()).State;
contract.ShippingCountry = leadmap.get(leadConvertResult.getLeadId()).Country;
contract.ShippingStreet = leadmap.get(leadConvertResult.getLeadId()).Street;
contract.ShippingPostalCode = leadmap.get(leadConvertResult.getLeadId()).PostalCode;
contract.Signature__c = leadmap.get(leadConvertResult.getLeadId()).Signature__c;
contract.Signature_Date_Time__c = leadmap.get(leadConvertResult.getLeadId()).Signature_Date_Time__c;
contract.Is_Policy_Holder__c = leadmap.get(leadConvertResult.getLeadId()).Is_Policy_Holder__c;
contract.Insurance__c = leadmap.get(leadConvertResult.getLeadId()).Insurance__c;
contract.Insurance_Provider__c = leadmap.get(leadConvertResult.getLeadId()).Insurance_Provider__c;
contract.Policy_Holder_First_Name__c = leadmap.get(leadConvertResult.getLeadId()).Policy_Holder_First_Name__c;
contract.Policy_Holder_Last_Name__c = leadmap.get(leadConvertResult.getLeadId()).Policy_Holder_Last_Name__c;
contract.Policy_ID__c = leadmap.get(leadConvertResult.getLeadId()).Policy_ID__c;
contract.Group_ID__c = leadmap.get(leadConvertResult.getLeadId()).Group_ID__c;
//contract.Address_Line_2__c = leadmap.get(leadConvertResult.getLeadId()).Address_Line_2__c;
}
Address__c Address = new Address__c();
Address.Account__c = leadConvertResult.getAccountId();
Address.Address_Type__c = leadmap.get(leadConvertResult.getLeadId()).Address_Type__c;
Address.Name = leadmap.get(leadConvertResult.getLeadId()).Street;
//Address.Street__c = leadmap.get(leadConvertResult.getLeadId()).Street;
Address.City__c = leadmap.get(leadConvertResult.getLeadId()).City;
Address.State__c = leadmap.get(leadConvertResult.getLeadId()).State;
Address.Zip_Code__c = leadmap.get(leadConvertResult.getLeadId()).PostalCode;
Address.Country__c = leadmap.get(leadConvertResult.getLeadId()).Country;
//Address.Landmark__c = leadmap.get(leadConvertResult.getLeadId()).City;
//MemberPlan member = new MemberPlan();
//member.MemberId = leadConvertResult.getAccountId();
//member.Is_Policy_Holder__c = leadmap.get(leadConvertResult.getLeadId()).Is_Policy_Holder__c;
//member.Name = leadmap.get(leadConvertResult.getLeadId()).Insurance_Name__c;
//member.Insurance_Options__c = leadmap.get(leadConvertResult.getLeadId()).Insurance__c;
//member.MemberNumber = leadmap.get(leadConvertResult.getLeadId()).Policy_ID__c;
//member.GroupNumber = leadmap.get(leadConvertResult.getLeadId()).Group_ID__c;
//member.Policy_Holder_First_Name__c = leadmap.get(leadConvertResult.getLeadId()).Policy_Holder_First_Name__c;
//member.Policy_Holder_Last_Name__c = leadmap.get(leadConvertResult.getLeadId()).Policy_Holder_Last_Name__c;
contractsToInsert.add(contract);
//memberplanToInsert.add(member);
addressesToInsert.add(Address);
}
}
}
if(!contractsToInsert.isEmpty()){
insert contractsToInsert;
}
//if(!memberplanToInsert.isEmpty()){
//insert memberplanToInsert;
if(!addressesToInsert.isEmpty()){
insert addressesToInsert;
}
}catch(Exception e){
System.debug('Error: '+e.getMessage()+ 'Line Number'+e.getLineNumber());
}
}
}
I need to create account with the lead first and last name
Replace existingAccount.Name = ld.Name; with existingAccount.LastName = ld.LastName; and existingAccount.FirstName = ld.FirstName;. The Name field on the account is a concatenation of the first and last name, so you need to set the first and last name separately.
Remove the line existingAccount.First_Name__c = ld.LastName;, since you already set the LastName field.
Change the line newCreatedAccounts.put(acc.First_Name__c+acc.Last_Name__c, acc); to newCreatedAccounts.put(acc.LastName+acc.FirstName, acc);. This will use the lead's first and last name to create the map key, which is what you want.
Here's the modified code:
If you find this answer helpful.Please mark it as the best answer.