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
Ashish Kumar YadavAshish Kumar Yadav 

how to cover apex class test class coverage

Hi Team,
Please help me to write the test class.
public class AssignScheme {
    public Scheme_Assign_Master__c sam{get;set;}
    public List<Scheme_Assign_Master__c> lstAssignIndividual{get;set;}
    public List<Scheme_Assign_Master__c> lstRemoveIndividual{get;set;}
    public List<Scheme_Assign_Master__c> AssignToAll{get;set;}
    public List<Scheme_Assign_Master__c> SchemeList{get;set;}
    public List<Scheme_Assign_Master__c> searchList{get;set;}
    public List<Scheme_Master__c> smdatelist{get;set;}
    public Id samid;
    public Id RecordTypeId;
    public string query;
    public String strQuery;
    public Scheme_Master__c scheme_masterobj{get;set;}
    Public List<Scheme_Assign_Master__c>removeAccountList{get;set;} 
    Public List<Scheme_Master__c> lstSM {get;set;}
    public Integer rowIndex {get;set;}
    public String showdelId {get;set;}
    public String delId {get; set;}
    public List<Scheme_Assign_Master__c> lstsave{get;set;}
    public Account acc{get;set;}
    public String message{get;set;}
    public List<Scheme_Assign_Master__c> schemepgList{get;set;}
    
    public String scheme2{get;set;}
    public integer counter = 0; 
    public integer OffsetSize = 0;
    public integer LimitSize= 5;
    public integer totalRecs = 0;
    public List<Scheme_Assign_Master__c> membs {get;set;}
    public String sapp {get;set;}
    public String soqlQuery;
    
    public AssignScheme()
    {      
        sam=new Scheme_Assign_Master__c();
        acc=new Account();  
        
    }
    
    
    public AssignScheme(ApexPages.StandardController controller)
    { 
        sam=new Scheme_Assign_Master__c();
        this.controller = controller;
        SchemeList=new List<Scheme_Assign_Master__c>();
        
        Account acc=new Account();
        System.debug('@@@@'+controller.getId());
        acc = [ SELECT Id, Name FROM Account WHERE Id =: controller.getId()];
        System.debug('acc'+acc);
        
    }
  
    public void getControllerField(){
        System.debug('------>'+sam.Scheme_Master__c);
        lstSM = [Select Id,Scheme_Applicable_For__c from Scheme_Master__c where id =: sam.Scheme_Master__c];
        if(lstSM.size() > 0){
            System.debug('lstSM------>'+lstSM[0].Scheme_Applicable_For__c);
            sam.Scheme_Applicable_For__c = lstSM[0].Scheme_Applicable_For__c;
        }
        
    }
    
    public PageReference SearchIndividual(){
        
        try{
            if(sam.Scheme_Master__c == NULL){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please select the Scheme from "Scheme Name"'));
            }
            
            else if(sam.Account__c== NULL && sam.Retailer_Account__c== NULL){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Check Distributor/Retailer Name'));
                return null;
            }
            else
            {
                Set<Id> distid = new Set<Id>();
                distid.add(sam.Retailer_Account__c);
                distid.add(sam.Account__c);
                System.debug('distid@@'+distid);
                System.debug('sam.Scheme_Master__c@@'+sam.Scheme_Master__c);
                
                
                if(sam.Scheme_Applicable_For__c =='Distributor'){
                    searchList=[select Id,Account__c,Retailer_Account__c,Scheme_Applicable_For__c,Retailer_Account__r.Name,Retailer_Account__r.SF_Customer_Code__c,Retailer_Account__r.SAP_Customer_Code__c,Account__r.Name,Account__r.SF_Customer_Code__c,Account__r.SAP_Customer_Code__c, Scheme_Master__c from Scheme_Assign_Master__c where Scheme_Master__c =: sam.Scheme_Master__c AND Account__c in :distid and Account__c != null];
                    System.debug('searchListacc'+searchList);
                }
                
                else if(sam.Scheme_Applicable_For__c =='Retailer'){
                    searchList=[select Id,Account__c,Retailer_Account__c,Scheme_Applicable_For__c,Retailer_Account__r.Name,Retailer_Account__r.SF_Customer_Code__c,Retailer_Account__r.SAP_Customer_Code__c,Account__r.Name,Account__r.SF_Customer_Code__c,Account__r.SAP_Customer_Code__c, Scheme_Master__c from Scheme_Assign_Master__c where Scheme_Master__c =: sam.Scheme_Master__c AND  Retailer_Account__c in :distid and Retailer_Account__c != null];
                    System.debug('searchListacc'+searchList);
                }
                
                else if(sam.Scheme_Applicable_For__c =='Both'){
                    searchList=[select Id,Account__c,Retailer_Account__c,Scheme_Applicable_For__c,Retailer_Account__r.Name,Retailer_Account__r.SF_Customer_Code__c,Retailer_Account__r.SAP_Customer_Code__c,Account__r.Name,Account__r.SF_Customer_Code__c,Account__r.SAP_Customer_Code__c, Scheme_Master__c from Scheme_Assign_Master__c where Scheme_Master__c =: sam.Scheme_Master__c AND Account__c in :distid and Account__c != null];
                    searchList.addAll([select Id,Account__c,Retailer_Account__c,Scheme_Applicable_For__c,Retailer_Account__r.Name,Retailer_Account__r.SF_Customer_Code__c,Retailer_Account__r.SAP_Customer_Code__c,Account__r.Name,Account__r.SF_Customer_Code__c,Account__r.SAP_Customer_Code__c, Scheme_Master__c from Scheme_Assign_Master__c where Scheme_Master__c =: sam.Scheme_Master__c AND  Retailer_Account__c in :distid and Retailer_Account__c != null]);
                }
                
                if(searchList.size() > 0 ){ 
                    System.debug('individual search list@@@'+searchList);
                    System.debug('search list size @@@'+searchList.size());
                 
                }
                else
                {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Distributor/Retailer is not assigned to this Scheme'));
                    
                }
            }
        }catch(Exception e){
            system.debug('Exception'+e.getLineNumber());
            system.debug('Exception'+e.getMessage());
            
        }
        return null;
    }
   
    public PageReference AssignIndividual(){
        try{
            if(sam.Scheme_Master__c == NULL){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please select the Scheme from "Scheme Name"'));
                return null; 
            }
            else if(sam.Account__c== NULL && sam.Retailer_Account__c== NULL){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Check Distributor/Retailer Name'));
            }
           
           
                System.debug('---> assign individual');
                List<Id> ret_accid=new List<Id>();
                List<Id> dist_accid=new List<Id>();
                if(sam.Retailer_Account__c != null){
                    ret_accid.add(sam.Retailer_Account__c);
                }
                if(sam.Account__c != null){
                    dist_accid.add(sam.Account__c);
                }
                
                List<Scheme_Assign_Master__c> membs = new List<Scheme_Assign_Master__c>();
                if(sam.Scheme_Applicable_For__c =='Distributor'){
                    membs =[SELECT Id,Account__c,Retailer_Account__c, Scheme_Master__c,Scheme_Applicable_For__c from Scheme_Assign_Master__c where Scheme_Master__c =: sam.Scheme_Master__c and Account__c in : dist_accid and Account__c != null];
                }
                else if(sam.Scheme_Applicable_For__c =='Retailer'){
                    membs =[SELECT Id,Account__c,Retailer_Account__c, Scheme_Master__c,Scheme_Applicable_For__c from Scheme_Assign_Master__c where Scheme_Master__c =: sam.Scheme_Master__c and Retailer_Account__c in : ret_accid and Retailer_Account__c != null];
                }
                else if(sam.Scheme_Applicable_For__c =='Both'){
                    membs =[SELECT Id,Account__c,Retailer_Account__c, Scheme_Master__c,Scheme_Applicable_For__c from Scheme_Assign_Master__c where Scheme_Master__c =: sam.Scheme_Master__c and Account__c in : dist_accid and Account__c != null];
                    list<Scheme_Assign_Master__c> membs1 = new list<Scheme_Assign_Master__c>();
                    membs1 =[SELECT Id,Account__c,Retailer_Account__c, Scheme_Master__c,Scheme_Applicable_For__c from Scheme_Assign_Master__c where Scheme_Master__c =: sam.Scheme_Master__c and Retailer_Account__c in : ret_accid and Retailer_Account__c != null];
                    if(membs1.size() > 0){
                        membs.addAll(membs1);
                    }
                }
                
                Set<Id> accIds = new Set<Id>();
                for(Scheme_Assign_Master__c obj : membs){
                    if(obj.Retailer_Account__c != null){
                        accIds.add(obj.Retailer_Account__c);
                    }
                    if(obj.Account__c != null){
                        accIds.add(obj.Account__c);
                    }
                }
                
                lstAssignIndividual = new List<Scheme_Assign_Master__c>();
                scheme_masterobj=new Scheme_Master__c();
                
              
                if(sam.Scheme_Master__c != null){
                    if(sam.Retailer_Account__c != null){
                        if(!accIds.contains(sam.Retailer_Account__c)){
                            Scheme_Assign_Master__c objSAM = new Scheme_Assign_Master__c();
                            objSAM.Retailer_Account__c = sam.Retailer_Account__c;
                            objSAM.Scheme_Master__c = sam.Scheme_Master__c;
                            lstAssignIndividual.add(objSAM);
                            accIds.add(sam.Retailer_Account__c);
                        }
                        else{
                            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Scheme master already assigned to this Retailer Account'));
                        }
                    }
                    
                    if(sam.Account__c != null){
                        if(!accIds.contains(sam.Account__c)){
                            Scheme_Assign_Master__c objSAM = new Scheme_Assign_Master__c();
                            objSAM.Account__c = sam.Account__c;
                            objSAM.Scheme_Master__c = sam.Scheme_Master__c;
                            lstAssignIndividual.add(objSAM);
                            accIds.add(sam.Account__c);
                        }
                        else{
                            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Scheme master already assigned to this Distributer Account'));
                        }
                    }
                }
                
                
                if(lstAssignIndividual.size() > 0){
                    insert lstAssignIndividual;
                    
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,' Assign Individual Scheme to Distributor / Retailer Successfully.'));
                    
                    List<User> userList = new List<User>();
                    userList = [SELECT Id,Email,IsActive FROM User WHERE Profile.Name = 'System Administrator' AND IsActive = True ];
                    for(User u : userList){
                        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                        String[] toAddresses = new String[] {u.Email};
                            mail.setToAddresses(toAddresses);
                        mail.setSubject('Scheme Assign to Distributor/Retailer');
                        mail.setPlainTextBody('Scheme Assigned to Distributor/Retailer successfully');
                        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                    }
                }
            }
        }
        catch(Exception e){
        }
        return null;
    }

    public PageReference showassigndel(){
        Scheme_Assign_Master__c  showdelacc=[select id from Scheme_Assign_Master__c where id =:showdelId limit 1];
        if(showdelacc !=null){
            try{
                delete showdelacc;
                getAccounts();
            } catch(DmlException e){
                ApexPages.addMessages(e);
            }
        }else{
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Distributor/Retailer is not assigned to this Scheme'));
            
        }
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,' Remove Scheme to Distributor / Retailer Successfully.'));
        
        return null;
    }
 
    public PageReference RemoveIndividual(){
        if(sam.Scheme_Master__c == NULL){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please select the Scheme from "Scheme Name"'));
            return null; 
        }
        else if(sam.Account__c== NULL && sam.Retailer_Account__c== NULL){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Check Distributor/Retailer Name'));
        }
        
        else{
            Set<Id> accIds = new Set<Id>();
            lstRemoveIndividual = new List<Scheme_Assign_Master__c>();
            if(sam.Account__c != null){
                accIds.add(sam.Account__c);
            }
            if(sam.Retailer_Account__c != null){
                accIds.add(sam.Retailer_Account__c);
            }
            
            lstRemoveIndividual = [select id,Account__c,Retailer_Account__c from Scheme_Assign_Master__c where Scheme_Master__c =:sam.Scheme_Master__c and (Account__c in :accIds OR Retailer_Account__c in:accIds)];
            
            if(lstRemoveIndividual.size() > 0){
                Delete lstRemoveIndividual;
                ApexPages.addmessage(new 

            }
        }
        return null;   
    }
    
    
    public List<Scheme_Assign_Master__c> getAccounts() {  
        if(sam.Scheme_Master__c == NULL){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please select the Scheme from "Scheme Name"'));
            return null; 
        }
        else{
            totalRecs = [select count() from Scheme_Assign_Master__c];
            /*New code end here*/
            scheme2=sam.Scheme_Master__c;
            membs=new list<Scheme_Assign_Master__c>();
            if(sam.Scheme_Applicable_For__c =='Distributor'){
                membs =[SELECT Id,Account__c,Retailer_Account__c,Retailer_Account__r.Name,Retailer_Account__r.SF_Customer_Code__c,Retailer_Account__r.SAP_Customer_Code__c,Account__r.Name,Account__r.SF_Customer_Code__c,Account__r.SAP_Customer_Code__c, Scheme_Master__c,Scheme_Applicable_For__c from Scheme_Assign_Master__c where Scheme_Master__c =:scheme2 and Account__c != null limit :LimitSize offset :OffsetSize]; 
                System.debug('membs---->'+membs.size());
            }
            else if(sam.Scheme_Applicable_For__c =='Retailer'){
                membs =[SELECT Id,Account__c,Retailer_Account__c,Retailer_Account__r.Name,Retailer_Account__r.SF_Customer_Code__c,Retailer_Account__r.SAP_Customer_Code__c,Account__r.Name,Account__r.SF_Customer_Code__c,Account__r.SAP_Customer_Code__c, Scheme_Master__c,Scheme_Applicable_For__c from Scheme_Assign_Master__c where Scheme_Master__c =:scheme2 and Retailer_Account__c != null limit :LimitSize offset :OffsetSize]; 
                System.debug('membs---->'+membs.size());
            }
            else if(sam.Scheme_Applicable_For__c =='Both'){
                membs =[SELECT Id,Account__c,Retailer_Account__c,Retailer_Account__r.Name,Retailer_Account__r.SF_Customer_Code__c,Retailer_Account__r.SAP_Customer_Code__c,Account__r.Name,Account__r.SF_Customer_Code__c,Account__r.SAP_Customer_Code__c, Scheme_Master__c,Scheme_Applicable_For__c from Scheme_Assign_Master__c where Scheme_Master__c =:scheme2 and Account__c != null limit :LimitSize offset :OffsetSize]; 
                list<Scheme_Assign_Master__c> membs1 = new list<Scheme_Assign_Master__c>();
                membs1 =[SELECT Id,Account__c,Retailer_Account__c,Retailer_Account__r.Name,Retailer_Account__r.SF_Customer_Code__c,Retailer_Account__r.SAP_Customer_Code__c,Account__r.Name,Account__r.SF_Customer_Code__c,Account__r.SAP_Customer_Code__c, Scheme_Master__c,Scheme_Applicable_For__c from Scheme_Assign_Master__c where Scheme_Master__c =:scheme2 and Retailer_Account__c != null limit :LimitSize offset :OffsetSize]; 
                if(membs1.size() > 0){
                    membs.addAll(membs1);
                }
            }
          
            if(membs.size() > 0){
                for(Scheme_Assign_Master__c sm : membs){
                    System.debug('membs@@@'+sm.Id);
                }
            }
            else {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Distributor/Retailer is not assigned to this Scheme'));
            }
        }
        return null;  
    }      
    
    private final ApexPages.StandardController controller; 
}
Suraj Tripathi 47Suraj Tripathi 47
Hi Ashish Kumar Yadav,

I think your code is incorrect because you have not inserted record in Scheme_Assign_Master__c sam object, that's why I am getting error when i am creating test class. So if you will try to fetch the field values using sam refernce (sam.Scheme_Applicable_For__c) then it will throw error because there is no data.

And the error is:-  List has no rows for assignment to SObject.

Thanks
Ashish Kumar YadavAshish Kumar Yadav
Scheme assign master is inserted you can see in apex class method AssignIndividual() I have inserted scheme assign master code .
Ashish Kumar YadavAshish Kumar Yadav
Hi Suraj Tripathi 47,

can you please help me to write the test class.