+ Start a Discussion
Neeraj Sharma 103Neeraj Sharma 103 

How to write test class for below trigger Please help me

Hi Everyone this is my trigger i want to write test class for this so please help me i tried but not accomplish my requirement code coverge approx 75%



trigger Distributor on Account (before insert) {
    
String role;   
String owner;
    
  list<Account> changeOwner=new list<Account>();
    
    for(Account acc:trigger.new){
        
       if(acc.Under__c=='Chhattisgarh'){
           role=' CG';
          changeOwner.add(acc);
       }
      
       else if(acc.Under__c=='Bihar'){
           role=' BI';
         changeOwner.add(acc);
       }  
        
      else if(acc.Under__c=='Rajasthan') {
           role=' RJ';
           changeOwner.add(acc);
           }  
      else if(acc.Under__c=='Gujarat'){
           role=' GJ';
          changeOwner.add(acc);
           }
        
      else if(acc.Under__c=='Haryana'){
           role=' HR';
         changeOwner.add(acc);
           
       }
        
      else if(acc.Under__c=='Jharkhand'){
           role=' JH';
         changeOwner.add(acc);
       } 
        
      else if(acc.Under__c=='Maharashtra'){
           role=' MH';
         changeOwner.add(acc);
       }
        
       else if(acc.Under__c=='Madhya Pradesh'){
           role=' MP';
         changeOwner.add(acc);
       }  
       else if(acc.Under__c=='Uttar Pradesh'){
           role=' UP';
        changeOwner.add(acc);
       } 
        
   }
    String SOSTR = String.valueOf('SO') + String.valueOf(role);  
    String ASMSTR = String.valueOf('ASM') + String.valueOf(role);
    String DRMSTR = String.valueOf('DRM') + String.valueOf(role);
    String RMSTR = String.valueOf('RM') + String.valueOf(role);


  list <User> socg1=Database.query('SELECT Id,Name FROM User WHERE UserRole.Name = :SOSTR Order By FirstName,LastName');
  list <User> socg2=Database.query('SELECT Id,Name FROM User WHERE UserRole.Name = :ASMSTR Order By FirstName,LastName');
  list <User> socg3=Database.query('SELECT Id,Name FROM User WHERE UserRole.Name = :DRMSTR  Order By FirstName,LastName');
  list <User> socg4=Database.query('SELECT Id,Name FROM User WHERE UserRole.Name = :RMSTR Order By FirstName,LastName');
  
   if(socg1.size()>0){
        owner=socg1.get(0).id;
    }
    
     else if(socg1.size()<=0){
     
        if(socg2.size()>0){
            owner=socg2.get(0).id; 
        }
        
        else if(socg2.size()<=0){
            
            if(socg3.size()>0){
              owner=socg2.get(0).id;  
             }
        
        else if(socg3.size()<=0){
            
             if(socg4.size()>0) {
               owner=socg4.get(0).id;   
             }   
        }
      }
   }
    
   for(Account acc:changeOwner){
       
        if(socg1!=null){
            acc.OwnerId=Owner;
        }
           
    }
   
}
Best Answer chosen by Neeraj Sharma 103
dandamudidandamudi
@Neeraj sharma103 Please try with below code and let me know if you get any issue.
@isTest
public class setestingTest {
    
     @testSetup static void setup(){
        List<String> unders = new List<String>{'Chhattisgarh','Bihar','Rajasthan','Gujarat','Haryana','Jharkhand','Maharashtra','Madhya Pradesh','Uttar Pradesh'};
        List<Account> acclist = new List<Account>();
        for(String under : unders){
            acclist.add(new Account(
              Under__c = under;
            ));
        }
    insert acclist;
    }
    
}
 

All Answers

dandamudidandamudi
@Neeraj sharma103 Please try with below code and let me know if you get any issue.
@isTest
public class setestingTest {
    
     @testSetup static void setup(){
        List<String> unders = new List<String>{'Chhattisgarh','Bihar','Rajasthan','Gujarat','Haryana','Jharkhand','Maharashtra','Madhya Pradesh','Uttar Pradesh'};
        List<Account> acclist = new List<Account>();
        for(String under : unders){
            acclist.add(new Account(
              Under__c = under;
            ));
        }
    insert acclist;
    }
    
}
 
This was selected as the best answer
Neeraj Sharma 103Neeraj Sharma 103
Hi @dandamudi
Its not work its gives error
line 3, column 5: Variable does not exist: ContactTriggerHandler
dandamudidandamudi
@Neeraj Sharma 103 can you post current using code here with Line Numbers, so that i can fix other wise it's hard to find 
Raj VakatiRaj Vakati
use the below code and  Add other roles and user in test class 

 
@isTest
public class DistributorTest {
    
   @isTest static void myTest() {
	   
	    Profile pf= [Select Id from profile where Name='System Administrator']; 
        
		
		UserRole obj=new UserRole(Name= 'SOAP'); 
insert obj; 
// Add other roles and userin test class


        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        
		
		User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', UserRoleId = obj.Id ,
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
		
		
        List<String> unders = new List<String>{'Chhattisgarh','Bihar','Rajasthan','Gujarat','Haryana','Jharkhand','Maharashtra','Madhya Pradesh','Uttar Pradesh'};
        List<Account> acclist = new List<Account>();
        for(String under : unders){
            acclist.add(new Account(Name = under ,
              Under__c = under;
            ));
        }
    insert acclist;
    }
    
}