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
aswin balajiaswin balaji 

Test class for auto lead conversion

This is the code of auto lead conversion and autoLeadConversion is the function.I had written master class so i need specific function to be tested
public class AAAExisting{ 
    public List<wrapLead> wrapLeadList {get; set;}
    public List<Lead> accs {get; set;}
    
    public void ConvertedLeadList(){
       accs = [SELECT Id, Name, ConvertedAccountId, ConvertedContactId, ConvertedOpportunityId, Status FROM Lead WHERE IsConverted=true];
   // System.debug(accs);
    }
    
    
    
    public AAAExisting(){
        if(wrapLeadList == null) {
            wrapLeadList = new List<wrapLead>();
            for(Lead a: [select Id, Name, Company, Email, Phone, Status FROM Lead WHERE IsConverted=false]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapLeadList .add(new wrapLead(a));
               // System.debug(wrapLeadList);
            }
        }
    }
       
  
    
       Public void autoLeadConversion(List<Lead> autoLeadConversions) {
       //  List<Lead> autoLeadConversions= new List <Lead>();
           for(Lead lead: autoLeadConversions){
              if (lead.isConverted == false) { //to prevent recursion
       Database.LeadConvert lc = new Database.LeadConvert();
       lc.setLeadId(lead.Id); 
       String oppName = lead.LastName;
       lc.setOpportunityName(oppName);
       LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
       lc.setConvertedStatus(convertStatus.MasterLabel);
       Database.LeadConvertResult lcr = Database.convertLead(lc);
       System.assert(lcr.isSuccess());            
           }
        } 
    }
 
   
    
    public void processSelected() {
        for(wrapLead wrapAccountObj : wrapLeadList ) {
        if(wrapAccountObj.selected == true) {
            Database.LeadConvert lc = new Database.LeadConvert();
           lc.setLeadId(wrapAccountObj.acc.Id);
           lc.setopportunityname(wrapAccountObj.acc.Company);
            LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
            lc.setConvertedStatus(convertStatus.MasterLabel);
            Database.LeadConvertResult lcr = Database.convertLead(lc);
            }
            }
             }
 
    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value
    public class wrapLead{
        public Lead acc {get; set;}
        public Boolean selected {get; set;}
 
        //This is the contructor method. When we create a new wrapAccount object we pass a Account that is set to the acc property. We also set the selected value to false
        public wrapLead(Lead a) {
            acc = a;
            selected = false;
            System.debug('convertStatus'+ acc);
        }

    }
    
}



 
Best Answer chosen by aswin balaji
sachinarorasfsachinarorasf
Hi Aswin,

I have gone through your problem. Please try the below code.
 
@isTest
private class AAAExisting_Test {
    @isTest static void ConvertedLeadList_Test(){
        List<Lead> leadList = new List<Lead>();
        Lead leadObj=new Lead(LastName='Doe',FirstName='John',Company='Test',Status='Converted');
        leadList.add(leadObj);
        insert leadList; 
        Test.startTest();
        AAAExisting aaObj = new AAAExisting();
        aaObj.ConvertedLeadList();
        aaObj.autoLeadConversion(leadList);
        aaObj.processSelected();
        Test.stopTest();
    }

}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Ashwin,

The below link gives a description of writing a test class for an apex unit class.

>> https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro

Can you please check this once and in case if this was helpful for your implementation can you please choose this as the best answer so that it can be used by others in the future.

Regards,
Anutej
aswin balajiaswin balaji
Hi Anutej, sorry .....I want to rite test case for auto lead conversion like(lead account and contact and opportunity) in the test case to check
sachinarorasfsachinarorasf
Hi Aswin,

I have gone through your problem. Please try the below code.
 
@isTest
private class AAAExisting_Test {
    @isTest static void ConvertedLeadList_Test(){
        List<Lead> leadList = new List<Lead>();
        Lead leadObj=new Lead(LastName='Doe',FirstName='John',Company='Test',Status='Converted');
        leadList.add(leadObj);
        insert leadList; 
        Test.startTest();
        AAAExisting aaObj = new AAAExisting();
        aaObj.ConvertedLeadList();
        aaObj.autoLeadConversion(leadList);
        aaObj.processSelected();
        Test.stopTest();
    }

}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
This was selected as the best answer