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
Sudeep SinghSudeep Singh 

How to add address field of contract to fetch the address details from Lead conversion.

Code :- 

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>();
            Map<String,Account> nameDOBToAccountMap = new Map<String,Account>();
            Map<String,Account> newCreatedAccounts = new Map<String,Account>();
            Map<ID,Id> leadProductMap = 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,Date_Of_Birth__c,Service__c,Ethnicity__c, Race__c,Gender__c FROM Lead WHERE Id IN :LeadIds];
            
            for(Lead lead: leads) {
                leadNames.add(String.valueOf(lead.Name));
                leadDOBs.add(lead.Date_Of_Birth__c);
                NameDOBMap.put(lead.Name, lead.Date_Of_Birth__c);
                leadProductMap.put(lead.Id,lead.Service__c);
            }
            
            Map<Id, Account> existingAccounts = new Map<Id, Account>([SELECT Id, PersonBirthdate, Name, PersonGender FROM Account WHERE Name IN :NameDOBMap.KeySet() AND PersonBirthdate IN :NameDOBMap.values()]);
            
            for(account acc: existingAccounts.values()){
                
            }
            //System.debug('Check 1'+existingAccounts);
            for(Account accc: existingAccounts.values()){
                nameDOBToAccountMap.put(accc.Name+accc.PersonBirthdate, accc);
            }
            
            for(Lead ld : leads){
                if(!nameDOBToAccountMap.containsKey(ld.Name+ld.Date_Of_Birth__c)){
                    Account existingAccount = new Account();
                    existingAccount.LastName = ld.LastName;
                    existingAccount.FirstName = ld.FirstName;
                    existingAccount.PersonEmail = ld.Email;
                    existingAccount.PersonGender = ld.Gender__c;
                    existingAccount.RecordTypeId =  personAccountRecordTypeId;
                    existingAccount.Ethnicity__pc = ld.Ethnicity__c;
                    existingAccount.Race__pc = ld.Race__c;
                    existingAccount.PersonGender = ld.Gender__c;
                    existingAccount.PersonBirthdate = ld.Date_Of_Birth__c;
                    accountsToInsert.add(existingAccount);
                }  
            }
            
            if(accountsToInsert.size()>0){
                insert accountsToInsert;
            }
            //system.debug('accountsToInsert'+accountsToInsert);
            
            for(account acc: accountsToInsert){
                newCreatedAccounts.put(acc.FirstName+acc.LastName, acc);
            }
            //system.debug('newCreatedAccounts'+newCreatedAccounts);
            for(Lead lead : leads){
                //System.debug('Check 2');
                Database.LeadConvert LeadConvert = new Database.LeadConvert();
                LeadConvert.setLeadId(lead.Id);
                LeadConvert.setConvertedStatus(cLeadStatus.MasterLabel);
                LeadConvert.setDoNotCreateOpportunity(false);
                
                if(lead.Name != null && lead.Date_Of_Birth__c != null){                    
                    if(nameDOBToAccountMap.containsKey(lead.Name+lead.Date_Of_Birth__c) && nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c)!=Null){
                        //system.debug('existingAccount==>'+nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c));
                        Account existingAccount = new Account();
                        existingAccount.Id = nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c).id;
                        //system.debug('existingAccount'+existingAccount);
                        existingAccount.Ethnicity__pc = lead.Ethnicity__c;
                        existingAccount.Race__pc = lead.Race__c;
                        existingAccount.PersonGender = lead.Gender__c;
                        accountsToUpdate.add(existingAccount);
                        LeadConvert.setAccountId(existingAccount.Id);
                    }
                    else{
                        //system.debug('lead.Name+lead.Date_Of_Birth__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() && !existingAccounts.containskey(leadConvertResult.accountid) ) {
                        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());
                        }
                        contractsToInsert.add(contract);
                    }
                }
            }
            
            if(!contractsToInsert.isEmpty()){
                insert contractsToInsert;
            }
        }catch(Exception e){
            //System.debug('Error: '+e.getMessage()+ 'Line Number'+e.getLineNumber());
        }
    }
}

 

In this code I need to modify that Address field of lead should get fetched in Contract's Billing and Shipping address.

In this conversion Lead's address is coming to Account's Billing address but not coming to Account's Shipping address.

How to achieve this"

Thanks

Best Answer chosen by Sudeep Singh
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sudeep,

You need to query Phone field in Map<Id,Lead> leadmap and also assign phone number to person phone in Account as hightlited. Any field you can do in similar way.
 
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>();
        Map<String,Account> nameDOBToAccountMap = new Map<String,Account>();
        Map<String,Account> newCreatedAccounts = new Map<String,Account>();
        Map<ID,Id> leadProductMap = 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,Date_Of_Birth__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,Date_Of_Birth__c,Service__c,Ethnicity__c, Race__c,Phone,Gender__c,City,Country,State,Street,PostalCode FROM Lead WHERE Id IN :LeadIds]);
       // for(Lead lead: leads) {
        for(Lead lead: leadmap.values()) {
            leadNames.add(String.valueOf(lead.Name));
            leadDOBs.add(lead.Date_Of_Birth__c);
            NameDOBMap.put(lead.Name, lead.Date_Of_Birth__c);
            leadProductMap.put(lead.Id,lead.Service__c);
        }
        
        Map<Id, Account> existingAccounts = new Map<Id, Account>([SELECT Id, PersonBirthdate, Name, PersonGender FROM Account WHERE Name IN :NameDOBMap.KeySet() AND PersonBirthdate IN :NameDOBMap.values()]);
        
        for(account acc: existingAccounts.values()){
            
        }
        //System.debug('Check 1'+existingAccounts);
        for(Account accc: existingAccounts.values()){
            nameDOBToAccountMap.put(accc.Name+accc.PersonBirthdate, accc);
        }
        
       // for(Lead ld : leads){
        for(Lead ld: leadmap.values()) {
            if(!nameDOBToAccountMap.containsKey(ld.Name+ld.Date_Of_Birth__c)){
                Account existingAccount = new Account();
                existingAccount.LastName = ld.LastName;
                existingAccount.FirstName = ld.FirstName;
                existingAccount.PersonEmail = ld.Email;
                existingAccount.PersonGender = ld.Gender__c;
existingAccount.PersonHomePhone=ld.Phone;
                existingAccount.RecordTypeId =  personAccountRecordTypeId;
                existingAccount.Ethnicity__pc = ld.Ethnicity__c;
                existingAccount.Race__pc = ld.Race__c;
                existingAccount.PersonGender = ld.Gender__c;
                existingAccount.PersonBirthdate = ld.Date_Of_Birth__c;
                accountsToInsert.add(existingAccount);
            }  
        }
        
        if(accountsToInsert.size()>0){
            insert accountsToInsert;
        }
        //system.debug('accountsToInsert'+accountsToInsert);
        
        for(account acc: accountsToInsert){
            newCreatedAccounts.put(acc.FirstName+acc.LastName, 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(false);
            
            if(lead.Name != null && lead.Date_Of_Birth__c != null){                    
                if(nameDOBToAccountMap.containsKey(lead.Name+lead.Date_Of_Birth__c) && nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c)!=Null){
                    //system.debug('existingAccount==>'+nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c));
                    Account existingAccount = new Account();
                    existingAccount.Id = nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c).id;
                    //system.debug('existingAccount'+existingAccount);
                    existingAccount.Ethnicity__pc = lead.Ethnicity__c;
                    existingAccount.Race__pc = lead.Race__c;
                    existingAccount.PersonGender = lead.Gender__c;
                    accountsToUpdate.add(existingAccount);
                    LeadConvert.setAccountId(existingAccount.Id);
                }
                else{
                    //system.debug('lead.Name+lead.Date_Of_Birth__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() && !existingAccounts.containskey(leadConvertResult.accountid) ) {
                    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(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;

                    }
                    contractsToInsert.add(contract);
                }
            }
        }
        
        if(!contractsToInsert.isEmpty()){
            insert contractsToInsert;
        }
    }catch(Exception e){
        //System.debug('Error: '+e.getMessage()+ 'Line Number'+e.getLineNumber());
    }
}
}

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Instead of storing the lead result in the query to list store it to Map and query all the address fields like City, Country,State, Pincode .. etc as below .
 
Map<Id,Lead> leadmap= new Map<Id,Lead>([SELECT Id,Name,LastName,FirstName,Email,Date_Of_Birth__c,Service__c ,Gender__c,City,Country,State FROM Lead WHERE Id IN :LeadIds]);

Now while iterating over the leads instead you should iterate over leadmap.values().

so you can get the address field in the Contract directly as below. I have highlited the changes in the class.

 
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>();
        Map<String,Account> nameDOBToAccountMap = new Map<String,Account>();
        Map<String,Account> newCreatedAccounts = new Map<String,Account>();
        Map<ID,Id> leadProductMap = 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,Date_Of_Birth__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,Date_Of_Birth__c,Service__c,Ethnicity__c, Race__c,Gender__c,City,Country,State,Street,PostalCode FROM Lead WHERE Id IN :LeadIds]);
       // for(Lead lead: leads) {
        for(Lead lead: leadmap.values()) {
            leadNames.add(String.valueOf(lead.Name));
            leadDOBs.add(lead.Date_Of_Birth__c);
            NameDOBMap.put(lead.Name, lead.Date_Of_Birth__c);
            leadProductMap.put(lead.Id,lead.Service__c);
        }
        
        Map<Id, Account> existingAccounts = new Map<Id, Account>([SELECT Id, PersonBirthdate, Name, PersonGender FROM Account WHERE Name IN :NameDOBMap.KeySet() AND PersonBirthdate IN :NameDOBMap.values()]);
        
        for(account acc: existingAccounts.values()){
            
        }
        //System.debug('Check 1'+existingAccounts);
        for(Account accc: existingAccounts.values()){
            nameDOBToAccountMap.put(accc.Name+accc.PersonBirthdate, accc);
        }
        
       // for(Lead ld : leads){
        for(Lead ld: leadmap.values()) {
            if(!nameDOBToAccountMap.containsKey(ld.Name+ld.Date_Of_Birth__c)){
                Account existingAccount = new Account();
                existingAccount.LastName = ld.LastName;
                existingAccount.FirstName = ld.FirstName;
                existingAccount.PersonEmail = ld.Email;
                existingAccount.PersonGender = ld.Gender__c;
                existingAccount.RecordTypeId =  personAccountRecordTypeId;
                existingAccount.Ethnicity__pc = ld.Ethnicity__c;
                existingAccount.Race__pc = ld.Race__c;
                existingAccount.PersonGender = ld.Gender__c;
                existingAccount.PersonBirthdate = ld.Date_Of_Birth__c;
                accountsToInsert.add(existingAccount);
            }  
        }
        
        if(accountsToInsert.size()>0){
            insert accountsToInsert;
        }
        //system.debug('accountsToInsert'+accountsToInsert);
        
        for(account acc: accountsToInsert){
            newCreatedAccounts.put(acc.FirstName+acc.LastName, 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(false);
            
            if(lead.Name != null && lead.Date_Of_Birth__c != null){                    
                if(nameDOBToAccountMap.containsKey(lead.Name+lead.Date_Of_Birth__c) && nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c)!=Null){
                    //system.debug('existingAccount==>'+nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c));
                    Account existingAccount = new Account();
                    existingAccount.Id = nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c).id;
                    //system.debug('existingAccount'+existingAccount);
                    existingAccount.Ethnicity__pc = lead.Ethnicity__c;
                    existingAccount.Race__pc = lead.Race__c;
                    existingAccount.PersonGender = lead.Gender__c;
                    accountsToUpdate.add(existingAccount);
                    LeadConvert.setAccountId(existingAccount.Id);
                }
                else{
                    //system.debug('lead.Name+lead.Date_Of_Birth__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() && !existingAccounts.containskey(leadConvertResult.accountid) ) {
                    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(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;

                    }
                    contractsToInsert.add(contract);
                }
            }
        }
        
        if(!contractsToInsert.isEmpty()){
            insert contractsToInsert;
        }
    }catch(Exception e){
        //System.debug('Error: '+e.getMessage()+ 'Line Number'+e.getLineNumber());
    }
}
}


Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
Sudeep SinghSudeep Singh
No its not creating in Contract object.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sudeep,

Do you meant Contract is not creating or fields were not getting mapped?

Thanks,
 
Sudeep SinghSudeep Singh
Contract is not creating but in my previous code the contract was getting created.

Thanks
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sudeep,

Is it failing with any error. Did you check the apex logs. Are you getting any error?

Thanks,
 
Sudeep SinghSudeep Singh
No Error
Sudeep SinghSudeep Singh
In my code it is working fine and please modify using my code if possible else we can connect for its solution.

Thanks
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sudeep,

Give me some time I will check your code and  modify it based on it and will share you.

Thanks,
 
Sudeep SinghSudeep Singh
Please check it once and also I mentioned :- In this conversion Lead's address is coming to Account's Billing address but not coming to Account's Shipping address. Can u check this also

Thanks
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sudeep,

Are you calling this apex action using Process Builder or flow?

Thanks,
 
Sudeep SinghSudeep Singh
Yes using flow

Thanks
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sudeep,

Can you try to convert the contract once the lead has date of birth. It is working. Even the code which you shared is noy working it Date of Birth is blank in lead. Can you check and confirm the above. 

Thanks.
 
Sudeep SinghSudeep Singh
Yes it is coming now but why lead mobile is not getting stored in account's PersonMobilePhone

Thanks
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sudeep,

You need to query Phone field in Map<Id,Lead> leadmap and also assign phone number to person phone in Account as hightlited. Any field you can do in similar way.
 
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>();
        Map<String,Account> nameDOBToAccountMap = new Map<String,Account>();
        Map<String,Account> newCreatedAccounts = new Map<String,Account>();
        Map<ID,Id> leadProductMap = 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,Date_Of_Birth__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,Date_Of_Birth__c,Service__c,Ethnicity__c, Race__c,Phone,Gender__c,City,Country,State,Street,PostalCode FROM Lead WHERE Id IN :LeadIds]);
       // for(Lead lead: leads) {
        for(Lead lead: leadmap.values()) {
            leadNames.add(String.valueOf(lead.Name));
            leadDOBs.add(lead.Date_Of_Birth__c);
            NameDOBMap.put(lead.Name, lead.Date_Of_Birth__c);
            leadProductMap.put(lead.Id,lead.Service__c);
        }
        
        Map<Id, Account> existingAccounts = new Map<Id, Account>([SELECT Id, PersonBirthdate, Name, PersonGender FROM Account WHERE Name IN :NameDOBMap.KeySet() AND PersonBirthdate IN :NameDOBMap.values()]);
        
        for(account acc: existingAccounts.values()){
            
        }
        //System.debug('Check 1'+existingAccounts);
        for(Account accc: existingAccounts.values()){
            nameDOBToAccountMap.put(accc.Name+accc.PersonBirthdate, accc);
        }
        
       // for(Lead ld : leads){
        for(Lead ld: leadmap.values()) {
            if(!nameDOBToAccountMap.containsKey(ld.Name+ld.Date_Of_Birth__c)){
                Account existingAccount = new Account();
                existingAccount.LastName = ld.LastName;
                existingAccount.FirstName = ld.FirstName;
                existingAccount.PersonEmail = ld.Email;
                existingAccount.PersonGender = ld.Gender__c;
existingAccount.PersonHomePhone=ld.Phone;
                existingAccount.RecordTypeId =  personAccountRecordTypeId;
                existingAccount.Ethnicity__pc = ld.Ethnicity__c;
                existingAccount.Race__pc = ld.Race__c;
                existingAccount.PersonGender = ld.Gender__c;
                existingAccount.PersonBirthdate = ld.Date_Of_Birth__c;
                accountsToInsert.add(existingAccount);
            }  
        }
        
        if(accountsToInsert.size()>0){
            insert accountsToInsert;
        }
        //system.debug('accountsToInsert'+accountsToInsert);
        
        for(account acc: accountsToInsert){
            newCreatedAccounts.put(acc.FirstName+acc.LastName, 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(false);
            
            if(lead.Name != null && lead.Date_Of_Birth__c != null){                    
                if(nameDOBToAccountMap.containsKey(lead.Name+lead.Date_Of_Birth__c) && nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c)!=Null){
                    //system.debug('existingAccount==>'+nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c));
                    Account existingAccount = new Account();
                    existingAccount.Id = nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c).id;
                    //system.debug('existingAccount'+existingAccount);
                    existingAccount.Ethnicity__pc = lead.Ethnicity__c;
                    existingAccount.Race__pc = lead.Race__c;
                    existingAccount.PersonGender = lead.Gender__c;
                    accountsToUpdate.add(existingAccount);
                    LeadConvert.setAccountId(existingAccount.Id);
                }
                else{
                    //system.debug('lead.Name+lead.Date_Of_Birth__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() && !existingAccounts.containskey(leadConvertResult.accountid) ) {
                    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(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;

                    }
                    contractsToInsert.add(contract);
                }
            }
        }
        
        if(!contractsToInsert.isEmpty()){
            insert contractsToInsert;
        }
    }catch(Exception e){
        //System.debug('Error: '+e.getMessage()+ 'Line Number'+e.getLineNumber());
    }
}
}

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer