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
Sravanthi GSravanthi G 

how to create test class for this class ?can give the answer?

public with sharing class SalesLeadController {
    
    
//To fetch opportunity details
    public Opportunity opp {get;set;}
    public Id recTypeId {get;set;}
    public string rec {get;set;}
  public boolean isHide {get;set;}
  public boolean isHidecomp {get;set;}
    public integer prefcount;
  public integer prefcountcomp;
    public integer i;
  public integer j;
  public static integer chk;
  public static integer chkcomp;
  //This is used for interested vehicle
    public list<Lead_Details__c> intVehicle {get;set;} 
    
    //This is used for competitor vehicle
    public list<Lead_Details__c> compVehc {get;set;} 
    
    //This is used for Trad in vehicle
    public list<Lead_Details__c> tradeVehc {get;set;}
    public list<Lead_Details__c> allVehicles = new list<Lead_Details__c>();
    
    public SalesLeadController(ApexPages.StandardController controller) {
        if(chk != 1)isHide = true;
    if(chkcomp != 1)isHidecomp = true;
    opp = new opportunity();
    recTypeId = UtilRecordType.getRecordTypeIdByName('Opportunity', System.Label.Sales_Lead);
        opp.RecordTypeId = recTypeId;
    
        intVehicle = new list<Lead_Details__c>();
        
        compVehc = new list<Lead_Details__c>();
        
        tradeVehc = new list<Lead_Details__c>();
    Lead_Details__c tld = new Lead_Details__c();
        tld.type__c = 'Trade In';
        tradeVehc.add(tld);
    }

    //To add more records for Interested Vehicle
    public PageReference AddIntRecord() {
        
        Lead_Details__c ld = new Lead_Details__c();
        ld.type__c = 'Interested';
    ld.Vehicle_Model_Type__c = 'Own Sellable';
        intVehicle.add(ld);
        return null;
    }
    
    //To add more records for Competitor Vehicle
    public PageReference AddCompRecord() {
        
        Lead_Details__c cld = new Lead_Details__c();
        cld.type__c = 'Competitor';
    cld.Vehicle_Model_Type__c = 'Competitor';
        compVehc.add(cld);
        return null;
    }
    
  public void UpdateModInfo(){
        set<id> carModel = new set<id>();
        allVehicles.addall(intVehicle);
        allVehicles.addall(compVehc);
        for(Lead_Details__c l :allVehicles){
            carmodel.add(l.Car_Model__c);
        }
        list<Car_Model__c> cml = [select id, brand__C,series__c,Colour__c,Trim__c from car_model__C where id in: carModel];
        for(Lead_Details__c l :allVehicles){
            for(Car_Model__c cm : cml){
                if(l.Car_Model__c == cm.id){
                    l.brand__C = cm.brand__C;
                    l.Class__c = cm.series__c;
                    l.color__c = cm.colour__c;
                    l.trim__C = cm.trim__c;
                }
            }
        }
    }
  
  public void PreferredChk(){
    chk = 1;
    prefcount = 0;
    isHide = true;
  system.debug('size'+intVehicle.size());
  if(intVehicle!=null && intVehicle.size()>0)
  {
        for(i=0;i<intVehicle.size();i++)
        {
            if(intVehicle[i].Preferred__c)
          {
                system.debug('i'+i);
                prefcount++;
            }
        }
        system.debug('prefcount: '+prefcount);
        if(prefcount>1)
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Cannot select more than one Preferred Interested Vehicle'));
            isHide = false;
        }
  }
  }
  
  public void PreferredChkComp(){
    chkcomp = 1;
    prefcountcomp = 0;
    isHidecomp = true;
      system.debug('size'+compVehc.size());
      if(compVehc!=null && compVehc.size()>0)
      {
        for(j=0;j<compVehc.size();j++)
        {
          if(compVehc[j].Preferred__c)
          {
            system.debug('j'+j);
            prefcountcomp++;
          }
        }
        system.debug('prefcountcomp: '+prefcountcomp);
        if(prefcountcomp>1)
        {
          ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Cannot select more than one Preferred Competitor Vehicle'));
            isHidecomp = false;
        }
      }
  }
    
    public pageReference saveOpp(){

        try{
            DMLManagerService.insertAsSystem(opp);
        }
        catch (DmlException e){
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,e.getDMLMessage(0)));                                          
        }


        allVehicles = new list<Lead_Details__c>();
        //added for loops so that lead detail is not inserted if user oest not pick any model
    for(integer i=0;i<intVehicle.size();i++){
    system.debug('intmodel:' + intVehicle[i].Car_Model__c);
    if(intVehicle[i].Car_Model__c != null) allVehicles.addall(intVehicle);
    }
    for(integer i=0;i<compVehc.size();i++){
    system.debug('comptmodel:' + compVehc[i].Car_Model__c);
    if(compVehc[i].Car_Model__c != null) allVehicles.addall(compVehc);
    }
        for(integer i=0;i<tradeVehc.size();i++){
    system.debug('trademodel:' + tradeVehc[i].Car_Model__c);
    if(tradeVehc[i].Trade_In_Vehicle__c != null) allVehicles.addall(tradeVehc);
    }
        
        for(Lead_Details__c l :allVehicles){
            l.Related_Lead__c = opp.id;
        }
        try{
            Insert allVehicles;
        }
        catch(DmlException e){
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,e.getDMLMessage(0)));                                          
        }

        
        PageReference oppPage = new ApexPages.StandardController(opp).view();
        oppPage.setRedirect(true);
        return oppPage;
    }
    public pageReference Cancel(){
            PageReference pageRef; 
        pageRef = new PageReference('/006/o');

        return PageRef;
    }

}
 
Raj VakatiRaj Vakati
@isTest 
public class TestAfterSalesLeadEditController 
{
 static testMethod void testMethod1() 
 {
 
  Account testAcct = new Account (Name = 'My Test Account');
     insert testAcct;

    // Creates first opportunity
    Opportunity oppt = new Opportunity(Name ='New mAWS Deal',
                            AccountID = testAcct.ID,
                            StageName = 'Customer Won',
                            Amount = 3000,
                            CloseDate = System.today());

   insert oppt;

  Lead_Details__c ld = new Lead_Details__c() ;
  ld.Name='Test';
  ld.Related_Lead__c = oppt.Id ; 
  insert ld ; 
  
  Car_Model__c cm = new Car_Model__c() ;
  cm.Name ='Test' ;
  insert cm ; 
  
  
  
  
 
 Test.startTest();
  Test.setCurrentPage(Page.YOUR_PAGE);
  ApexPages.StandardSetController sc = new ApexPages.StandardSetController(oppt);
  SalesLeadController  ext = new SalesLeadController (sc);
 
 ext.AddIntRecord();
 ext.AddCompRecord();
 ext.UpdateModInfo();
 ext.PreferredChk();
 ext.PreferredChkComp();
 
 ext.saveOpp();
 ext.Cancel();
 
 
 Test.stopTest();
 }
}