+ Start a Discussion
Sudeep SinghSudeep Singh 

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,
M SreekanthM Sreekanth 
Write a trigger on contact to prevent duplicate records based on Contact Email & Contact Phone.But I want Indivizual Error detection,I did like below
--------------------------------------------------------------------------------------
Heleper Class:-
==========
public class ContactHelper {
//Write a trigger on contact to prevent duplicate records based on Contact Email & Contact Phone.But I want Indivizual Error detection
    public static void PreventDuplicateContacts(list<contact> conList){
        list<string> dupEmail =new list<string>();
        list<string> dupPhone =new list<string>();
        list<contact> EmailList = new list<contact>();
        list<contact> PhoneList = new list<contact>();
        for(contact c:conList){
            dupEmail.add(c.Email);
            dupPhone.add(c.Phone);
        }
        EmailList = [SELECT Id,Email FROM Contact WHERE Email =:dupEmail];
        PhoneList = [SELECT Id,Phone FROM Contact WHERE Phone =:dupPhone];
        for(contact c:conList){
            if(EmailList.size()>0){
              c.Email.addError('Email id already exists');  
            }
            if(PhoneList.size()>0){
              c.Phone.addError('Email id already exists');  
            }
        }
        
    }
}

Handler Class:-
==========
public class ContactHandler {
    public static void PreventDuplicateContacts(){
        ContactHelper.PreventDuplicateContacts(Trigger.new);
       
    }
 
}

Trigger Code:-
=========
trigger ContactTrigger on Contact (Before insert,Before Update) {
    if(Trigger.isInsert){
       ContactHandler.PreventDuplicateContacts();
    }
     else if(Trigger.isUpdate){
        ContactHandler.PreventDuplicateContacts();
    }
}
Best Answer chosen by M Sreekanth
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sreekanth,

You need to write Handler and helper. Both are same you can directly write the trigger code as below.

You can remove Handler class entirely.
I did not find any issues other than this.
trigger ContactTrigger on Contact (Before insert,Before Update) {
    if(Trigger.isInsert || Trigger.isupdate ){
        ContactHelper.PreventDuplicateContacts(Trigger.new);
    }
}

Let me know if you face any issues.


Thanks,
 
Sudeep SinghSudeep Singh 
I have a field Next order date in contract
Written a batch class to creaet automatically order if the contract is Activated. When the Contract status is made activated at that time the Next order Date will display the immediate wednesday which is handled by flow.

I need to do an setup that when Order status will be Order Dispatched then the Next Order Date should display Order Start Date + 30 days or Order Start Date + 30 days  (immediate wednesday).

Is it possible by flow If yes then how, if no then how we can achieve this.

Thanks
Best Answer chosen by Sudeep Singh
Shri RajShri Raj
Yes, it is possible to achieve this using a Flow. You can create a Flow that is triggered when the Order status is set to "Order Dispatched" and use the Flow to update the Next Order Date field on the Contract.
Here is an example of how you can accomplish this using a Flow:
Create a new Flow and choose "Record-Triggered Flow" as the type.
Use a "Record Update" element to update the Next Order Date field on the Contract.
Use a "Formula" element to calculate the Next Order Date by adding 30 days to the Order Start Date.
To get the next Wednesday you can use a formula as MOD(WEEKDAY(Order_Start_Date__c)+5,7) = 0
Use an "Update Records" element to update the Next Order Date field on the Contract with the calculated value.
Use the "Record Lookup" element to look up the contract based on the order start date.
Use the "Decision" element to check if the calculated Next Order Date is a Wednesday or not, if not use another formula element to get the next wednesday.
Finally, activate the Flow and test it by creating a new Order with the status set to "Order Dispatched."
Sudeep SinghSudeep Singh 
My test class is running but not covering 

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];
            
            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());
        }
    }
}

Test Class :- 
 
@isTest
public class Test_AutoConvertLead {
    @isTest
    static void testAutoConvertLead()
    {
        String recordTypeId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
        Account acc= new Account(
        RecordTypeID=recordTypeId ,
        FirstName='Test FName',
        LastName='Test LName',
        PersonBirthdate = system.today(),
        PersonGender = 'Male',
        PersonMailingStreet='test@yahoo.com',
        PersonMailingPostalCode='12345',
        PersonMailingCity='SFO',
        PersonEmail='test@yahoo.com',
        PersonHomePhone='1234567',
        PersonMobilePhone='12345678'
        );        
        insert acc;
        
        Product2 p = new Product2();
        p.Name = 'product';
        p.sstation__Msrp__c = 1;
        insert p;
        
        Lead ld = new lead();
        ld.LastName = 'test';
        ld.FirstName = 'class';
        ld.Email = 'test@gmail.com';
        ld.Date_Of_Birth__c = system.today();
        ld.Race__c = 'Asian';
        ld.Ethnicity__c = 'Hispanic or Latino';
        ld.Gender__c = 'Male';
        ld.Status = 'New';
        ld.Shipping_First_Name__c = 'test';
        ld.Shipping_Last_Name__c = 'class';
        ld.City = 'test';
        ld.Country = 'tes';
        ld.Street = 'test';
        ld.State = 'test';
        ld.PostalCode = '000000';
        ld.Service__c = p.Id;
        insert ld;
        
        Account accc = new Account();
        accc.LastName = ld.LastName;
        accc.FirstName = ld.FirstName;
        accc.PersonEmail = ld.Email;
        //accc.PersonBirthdate = ld.Date_Of_Birth__c-3;
        accc.Race__pc = ld.Race__c;
        accc.Ethnicity__pc = ld.Ethnicity__c;
        accc.PersonGender = ld.Gender__c;
        accc.BillingCity = ld.City;
        accc.BillingCountry = ld.Country;
        accc.BillingPostalCode = ld.PostalCode;
        accc.BillingState = ld.State;
        accc.BillingStreet = ld.Street;
        insert accc;
        
  
        Contract cont = new Contract();
        cont.AccountId= accc.id;
        cont.StartDate=date.today();
        cont.ContractTerm=2;
        cont.Status='Draft';
        //cont.Pricebook2Id = standardPricebook.Id;
        //cont.Service__c = op.Id;
        //cont.Insurance__c = mp.Id;
        cont.BillingStreet = 'test';
        cont.BillingPostalCode = '759128';
        cont.BillingState = 'test';
        cont.BillingCity = 'test';
        cont.BillingCountry = 'test';
        cont.BillingLatitude = 20.296059;
        cont.BillingLongitude = 85.824539;
        cont.ShippingCity = 'test';
        cont.ShippingCountry = 'test';
        cont.ShippingPostalCode = '777777';
        cont.ShippingLatitude = 20.296059;
        cont.ShippingLongitude = 85.824539;
        cont.Service__c = p.Id;
        insert cont;
        
      
        {

            System.assert(True, 'ErrorMessage');
            
             }
        
        
        Test.startTest();
        Lead l = new Lead(LastName = 'Test Lead',
                     Company = 'Test Company',Race__c = 'Asian',Ethnicity__c = 'Hispanic or Latino'
                     );
        insert l;
        
        AutoConvertLead obj = new AutoConvertLead();
        //String CRON_EXP = '0 0 0 3 9 ? 2042';
        //system.schedule('Test status Check9', CRON_EXP, obj );
        
        Test.stopTest();
        
        
        // For Schedulable AutoSubscriptionForOrderCreation
        
        
    }

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

Can you try the test class as below which may give you above 85% coverage.
 
@isTest
public class Test_AutoConvertLead {
    @isTest
    static void testAutoConvertLead()
    {
        String recordTypeId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
        Account acc= new Account(
        RecordTypeID=recordTypeId ,
        FirstName='Test FName',
        LastName='Test LName',
        PersonBirthdate = system.today(),
        PersonMailingStreet='test@yahoo.com',
        PersonMailingPostalCode='12345',
        PersonMailingCity='SFO',
        PersonEmail='test@yahoo.com',
        PersonHomePhone='1234567',
        PersonMobilePhone='12345678'
        );        
        insert acc;
        
        Product2 p = new Product2();
        p.Name = 'product';
        insert p;
        
        Lead ld = new lead();
        ld.LastName = 'test';
        ld.FirstName = 'class';
        ld.Email = 'test@gmail.com';
        ld.Date_Of_Birth__c = system.today();
        ld.Gender__c = 'Male';
        ld.Status = 'New';
        ld.City = 'test';
        ld.Country = 'tes';
        ld.Street = 'test';
        ld.State = 'test';
        ld.PostalCode = '000000';
        ld.Service__c = p.Id;
        insert ld;
        List<Id> leadlist= new list<Id>();
        leadlist.add(ld.id);
        Account accc = new Account();
        accc.LastName = ld.LastName;
        accc.FirstName = ld.FirstName;
        accc.PersonEmail = ld.Email;
        //accc.PersonBirthdate = ld.Date_Of_Birth__c-3;
        accc.BillingCity = ld.City;
        accc.BillingCountry = ld.Country;
        accc.BillingPostalCode = ld.PostalCode;
        accc.BillingState = ld.State;
        accc.BillingStreet = ld.Street;
        insert accc;
        
  
        Contract cont = new Contract();
        cont.AccountId= accc.id;
        cont.StartDate=date.today();
        cont.ContractTerm=2;
        cont.Status='Draft';
        //cont.Pricebook2Id = standardPricebook.Id;
        //cont.Service__c = op.Id;
        //cont.Insurance__c = mp.Id;
        cont.BillingStreet = 'test';
        cont.BillingPostalCode = '759128';
        cont.BillingState = 'test';
        cont.BillingCity = 'test';
        cont.BillingCountry = 'test';
        cont.BillingLatitude = 20.296059;
        cont.BillingLongitude = 85.824539;
        cont.ShippingCity = 'test';
        cont.ShippingCountry = 'test';
        cont.ShippingPostalCode = '777777';
        cont.ShippingLatitude = 20.296059;
        cont.ShippingLongitude = 85.824539;
        cont.Service__c = p.Id;
        insert cont;
        
      
        {

            AutoConvertLead.assignLeads(leadlist);
             }
        
        
        Test.startTest();
      
        
        Test.stopTest();
        
        
        // For Schedulable AutoSubscriptionForOrderCreation
        
        
    }

}

Let me know if you face any issues.

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

Thanks,
Harry C.Harry C. 
Hello, I'm sorry for not knowing how to phrase the question more exactly! I'm still learning how to use NPSP. Basically, our organization has 2 date-related fields for donation opportunity data entry: Close Date (comes with NPSP Default) and QB Date (for us). They are always the same thing. How can I make it so that QB Date always copies Close Date, so I don't have to enter the date twice for each opportunity?

Also, I'm just wondering how to alter the layout for new opportunities in general. I've tried doing it and I can't seem to make those changes reflect on the actual New Opportunity popup. There are a ton of fields we just don't use, and I want to hide them so they're less confusing for other staff.

Apologies again if I'm not wording these questions accurately. I don't fully understand all of the terminology yet but I'm learning!
Best Answer chosen by Harry C.
VinayVinay (Salesforce Developers) 
Hi Harry,

You can use flow to copy close date value to QB date, below is sample example that can help you with your requirement.

https://salesforce-flowsome.com/an-easy-flow-to-copy-address-field/

If QB date is new field you can try changing data type to formula so that it would populate close date as required.
QB date =  Close Date

Please mark as Best Answer if above information was helpful.

Thanks,
Louis Tran 16Louis Tran 16 
I have 3 dashboards for 3 different teams built in Lighting. How can I display those on the office TV? Ideally, set time auto refresh and rotation one by one on TV or split screen. Do you have any software or apps recomend?
Best Answer chosen by Louis Tran 16
SubratSubrat (Salesforce Developers) 
Jaye LoweJaye Lowe 
I have a use case where we have a webform that is inserted leads in salesforce. The first submission from the webform is creating a lead with a name and number. Then the second submission is a survey. Both are creating leads in salesforce. I need some way, preferrably declarative way of matching the two records and then merging them, or copying all the details from record 2 in to record 1 and then deletiing record 2. Any ideas? 
Best Answer chosen by Jaye Lowe
VinayVinay (Salesforce Developers) 
Hi Jaye,

Currently auto merge duplicate leads feature is unavailable at moment.  Kindly review below Idea and vote for this feature.

https://ideas.salesforce.com/s/idea/a0B8W00000GdhPZUAZ/allow-automerge-in-duplicate-management

However you can check below appexchange tool for merging duplicates records.

https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000G5tCPUAZ&tab=e&_ga=2.71712855.532635474.1674513347-602491664.1654971272

Please mark as Best Answer if above information was helpful.

Thanks,
Frederica KearneyFrederica Kearney 
Hello,
We have set up omni pre-chat forms which takes email address as a required field, but it doesn't pull the email address customers enter into the main SF record the system creates.
We want to use email addresses in the reports on chat transcript activity. Is there a way to pull this data into the main system please?
Thanks
Best Answer chosen by Frederica Kearney
SwethaSwetha (Salesforce Developers) 
HI Frederica,
You can make use of findOrCreate.saveToTranscript to create a record and save it to the chat transcript associated with the chat

Syntax:
<input type="hidden" name= "liveagent.prechat.findorcreate.saveToTranscript: String entityName" value= "String transcriptFieldName" />

More info : https://developer.salesforce.com/docs/atlas.en-us.live_agent_dev.meta/live_agent_dev/live_agent_creating_records_prechat_API_saveToTranscript.htm

If this information helps, please mark the answer as best. Thank you
Sudeep SinghSudeep Singh 
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.
Admin3 RadiusAdmin3 Radius 
In flow, I would like to get a records from an object and check from an array(or an alternative) if such match exist then for those that does not exist, I need to create that records with predetermined values. So here is my sequence:
1. Fetch records for object A with filter user and year
2. from the result with perhaps an array? (January~December - as text) determine which value have no match.
3. From the list of non existing record, iterate on array with missing Month and create those records with default values.
 
I dont know how to implement or create the flow for Item 2 & 3.
Any experts help is greatly appreciated. Thank you in advance.
Best Answer chosen by Admin3 Radius
Shri RajShri Raj
There are also other ways to implement this but, 
Start with a "Record Lookup" element to fetch records from object A, with a filter to retrieve records for a specific user and year.
Create a "Loop" element to iterate through the list of records returned in step 1.
Inside the loop, create a "Decision" element to check if the current record's month value matches any of the values in your array of months (January~December). You can use the "Contains" element to check if the current record's month value is contained in your array.
If the current record's month value does not match any of the values in your array, create a new record in object A using the "Create Record" element. You can use the "Assign" element to set the default values for the new record.
If the current record's month value does match a value in your array, continue to the next record in the loop.
Once all records have been processed, the flow will exit the loop and continue with any subsequent elements you have in your flow.