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 

My lead is not converting

public class AutoConvertLead {
    @InvocableMethod
    public static void assignLeads(List<Id> LeadIds) {
        try{
            LeadStatus cLeadStatus = [SELECT Id,MasterLabel FROM LeadStatus WHERE isConverted = true LIMIT 1];
            List<Lead> leads = [SELECT Id,Name,Date_Of_Birth__c,Ethnicity__c, Race__c FROM Lead WHERE Id IN :LeadIds];
            Map<String,Date> NameDOBMap = new Map<String,Date>();
            List<String> leadNames = new List<String>();
            List<Date> leadDOBs = new List<Date>();
            for(Lead lead: leads) {
                System.debug('Check 1'+lead.Name);
                leadNames.add(String.valueOf(lead.Name));
                leadDOBs.add(lead.Date_Of_Birth__c);
                NameDOBMap.put(lead.Name, lead.Date_Of_Birth__c);
            }
            Map<Id, Account> existingAccounts = new Map<Id, Account>([SELECT Id, PersonBirthdate, Name FROM Account WHERE Name IN :NameDOBMap.KeySet() AND PersonBirthdate IN :NameDOBMap.values()]);
            List<Database.LeadConvert> massLeadConvert = new List<Database.LeadConvert>();
            for(Lead lead : leads){
                System.debug('Check 2'+lead.Name);
                Database.LeadConvert LeadConvert = new Database.LeadConvert();
                LeadConvert.setLeadId(lead.Id);
                LeadConvert.setConvertedStatus(cLeadStatus.MasterLabel);
                if(lead.Name != null && lead.Date_Of_Birth__c != null){
                    System.debug('Check 3'+lead.Name);
                    if(NameDOBMap.containsKey(lead.Name) && lead.Date_Of_Birth__c==NameDOBMap.get(lead.Name)){
                        System.debug('Check 4'+lead.Name);
                        Account existingAccount = existingAccounts.get(lead.Id);
                        existingAccount.Ethnicity__pc = lead.Ethnicity__c;
                        existingAccount.Race__pc = lead.Race__c;
                        update existingAccount;
                        LeadConvert.setAccountId(existingAccount.Id);
                    } else {
                        LeadConvert.setDoNotCreateOpportunity(false);
                    }
                    massLeadConvert.add(LeadConvert);
                }else{
                    LeadConvert.setDoNotCreateOpportunity(false);
                }
            }
            if(!massLeadConvert.isEmpty()){
                System.debug('Check 5'+lead.Name);
                List<Database.LeadConvertResult> lcr = Database.convertLead(massLeadConvert);
                for(Database.LeadConvertResult leadConvertResult : lcr) {
                    if(leadConvertResult.isSuccess()) {
                        Contract contract = new Contract();
                        contract.Status = 'Draft';
                        contract.AccountId = leadConvertResult.getAccountId();
                        contract.StartDate = system.today();
                        contract.Description = 'New Contract';
                        insert contract;
                    }
                }
            }
        }catch(Exception e){
            //System.debug('Error: '+e);
        }
    }
}
Lead is not conveting to Contract
Best Answer chosen by Sudeep Singh
SubratSubrat (Salesforce Developers) 
Hello Sudeep ,

According to this help article -> https://help.salesforce.com/s/articleView?id=sf.contract_supplemental.htm&type=5 (https://help.salesforce.com/s/articleView?id=sf.contract_supplemental.htm&type=5)

There is a requirement of 

1) How long the contract will be in effect: the term in months (required)
2) The end date

Add thses 2 fields and let me know further.

Hope it helps.
Thank you.

All Answers

SubratSubrat (Salesforce Developers) 
Hello Sudeep ,

According to this help article -> https://help.salesforce.com/s/articleView?id=sf.contract_supplemental.htm&type=5 (https://help.salesforce.com/s/articleView?id=sf.contract_supplemental.htm&type=5)

There is a requirement of 

1) How long the contract will be in effect: the term in months (required)
2) The end date

Add thses 2 fields and let me know further.

Hope it helps.
Thank you.
This was selected as the best answer
Sudeep SinghSudeep Singh
No its not converting
Shri RajShri Raj
It looks like there is an issue with the code in the "AutoConvertLead" class. The problem seems to be that the code is not properly identifying and converting the leads.
One thing to check is that the LeadStatus object is correctly set to isConverted=true. If the LeadStatus object is not set correctly, the leads will not be converted.
Another thing to check is that the leads passed to the assignLeads method have all the necessary fields populated, such as Name, Date_Of_Birth__c, Ethnicity__c and Race__c, otherwise the leads will not be converted.
Also you are using the same name for lead variable in for loop and for LeadConvert variable, it will not work in this way.
Another issue may be that the contract is being created before the lead is fully converted. You can try moving the contract creation code within the for loop, inside the if statement where the leadConvertResult.isSuccess() check is performed.
You can also check the debug logs to see what is happening during the execution of the class and check for any error messages.