+ Start a Discussion
Renuka SharmaRenuka Sharma 

How to write Test class for the below Trigger code

Hello

I am new to the apex development, can anyone please help in writing test classes for the below trigger, i have no idea how to write a test classes for Triggers and Apex classes
 
trigger UserTrigger on User (after update) {
    
    Map<String,Id> newManagerQuotaMap = new Map<String,Id>();
    Map<String,Id> oldManagerQuotaMap = new Map<String,Id>();    
    Map<String,Quota__c> newReporteeQuotaMap = new Map<String,Quota__c>();
    Map<Id,Id> managerReporteemap = new Map<Id,Id>();
    Map<Id,Id> oldManagerReporteemap = new Map<Id,Id>();
    List<Quota__c> newMapQuota = new List<Quota__c>();
	List<Quota__c> oldMapQuota = new List<Quota__c>();
    
    for(User u : Trigger.new){
        if(u.managerid != Trigger.oldMap.get(u.Id).ManagerId){
            if(u.managerid != NULL)
                managerReporteemap.put(u.Id,u.managerId);
            
             if(Trigger.oldMap.get(u.Id).ManagerId != NULL)
              	oldManagerReporteemap.put(u.Id,Trigger.oldMap.get(u.Id).ManagerId);
        }
    }
    
    if(NULL != managerReporteemap && managerReporteemap.size() > 0 ){
        for(Quota__c qcm: [SELECT Id,Quater__c,Quater_Year__c, Assigned_To__c ,Manager_Quota__c FROM Quota__c WHERE Assigned_To__c IN : managerReporteemap.values()]){
            newManagerQuotaMap.put(qcm.Assigned_To__c + qcm.Quater__c + qcm.Quater_Year__c,qcm.Id);
        }
        
        for(Quota__c qcr : [SELECT Id,Quater__c,Quater_Year__c, Assigned_To__c ,Assigned_To__r.ManagerId, Manager_Quota__c FROM Quota__c WHERE Assigned_To__c IN : managerReporteemap.keyset()]){
            newReporteeQuotaMap.put(qcr.Assigned_To__r.ManagerId+ qcr.Quater__c + qcr.Quater_Year__c,qcr);
        }
        
        List<Quota__c> quotasOfReporteeupdate = new List<Quota__c>();
        if(newReporteeQuotaMap != NULL && newReporteeQuotaMap.size() > 0 ){
            for(String key : newReporteeQuotaMap.keySet()){
                if(newManagerQuotaMap.containsKey(key)){
                    newReporteeQuotaMap.get(key).Manager_Quota__c = newManagerQuotaMap.get(key);
                    
                }else{
                    newReporteeQuotaMap.get(key).Manager_Quota__c = NULL;
                }
                quotasOfReporteeupdate.add(newReporteeQuotaMap.get(key));
            }      
            if(NULL != quotasOfReporteeupdate && quotasOfReporteeupdate.size() > 0)
            	update quotasOfReporteeupdate;            
        }
                
        for (aggregateResult result: [Select Manager_Quota__c, Sum(Inside_Sales_Roll_Up__c) 
                                      FROM Quota__c WHERE 
                                      Manager_Quota__c IN: newManagerQuotaMap.values() 
                                      GROUP BY Manager_Quota__c]) {
                                              
			newMapQuota.add(new Quota__c(Id=(Id)result.get('Manager_Quota__c'),Actual_Amount__c =(Decimal)result.get('expr0')));         
        }
        
        	
        if(NULL != newMapQuota && newMapQuota.size() > 0){
        update newMapQuota;
    }
    
    	}
    
    if(NULL != oldManagerReporteemap && oldManagerReporteemap.size() > 0){
        for(Quota__c qcm: [SELECT Id,Quater__c,Quater_Year__c, Assigned_To__c ,Manager_Quota__c FROM Quota__c WHERE Assigned_To__c IN : oldManagerReporteemap.values()]){
            oldManagerQuotaMap.put(qcm.Assigned_To__c + qcm.Quater__c + qcm.Quater_Year__c,qcm.Id);
        }
        system.debug('oldManagerQuotaMap::' + oldManagerQuotaMap);
        if(NULL != oldManagerQuotaMap && oldManagerQuotaMap.size() > 0){
            futureHandler.updaterecs(oldManagerQuotaMap);
        }
     
    }      
    }



 
Renuka SharmaRenuka Sharma
i have tried writing the test class but still i am not geting the code coverage
 
@isTest
public class UserTriggerTestclass {
    
   
    static testmethod void userTigger(){
         User u = [Select id,name from user where isactive=true Limit 1];
        List<Quota__c> Quotas = new list <Quota__c>();
        Quota__c qt = new Quota__c();
        Id RecordTypeIdQuota1 = Schema.SObjectType.Quota__c.getRecordTypeInfosByDeveloperName().get('Individual_Sales_Rep_Field_Sales').getRecordTypeId();
        Quota__c qt3 = new Quota__c();
        qt3.Name = 'test';
        qt3.Quater_Year__c = '2020';
        qt3.Month__c = 'June';
        qt3.Quater__c = 'Q4';
        qt3.Assigned_To__c = userinfo.getuserid();
        qt3.Unique_Identifier_Per_Quater_Per_User__c = qt.Assigned_To__c + qt.Quater__c + qt.Quater_Year__c;
        qt3.Unique_Identifier_Per_Quater_Per_User__c = 'Sixth Quota inserted';
        qt3.RecordTypeId = RecordTypeIdQuota1;
        Quotas.add(qt3);
        
        insert Quotas;
        Quota__c qtr = new Quota__c();
        qtr.Name = 'test';
        qtr.Manager_Quota__c = qt3.Id;
        qtr.Quater_Year__c = '2020';
        qtr.Month__c = 'July';
        qtr.Quater__c = 'Q1';
        qtr.Assigned_To__c = userinfo.getuserid();
        qtr.RecordTypeId = RecordTypeIdQuota1;
    }

 
SFDC Apex DevSFDC Apex Dev
Hi Renuka,

As your trigger will fire on after update context, in that case you need to update the user i nyour test class.
I've modified it. Try the below test class.
 
@isTest
public class UserTriggerTestclass {    
    
    static testmethod void userTigger(){
        User u = [Select id from user where isactive=true Limit 1];
        
        Profile prof = [Select id from profile where name = 'System Administrator'];
        
        User user = new User(Alias = 'standt', Email='standarduser@testorg.com', 
                           EmailEncodingKey='UTF-8', LastName='Test12ing', LanguageLocaleKey='en_US', 
                           LocaleSidKey='en_US', ProfileId = prof.Id, ManagerId = u.Id,
                           TimeZoneSidKey='America/Los_Angeles', UserName='systaduser@testorg.com');
        insert user;
        
        user.ManagerId = userinfo.getUserId();
        update user;
        
        Id recordTypeIdQuota = Schema.SObjectType.Quota__c.getRecordTypeInfosByDeveloperName().get('Individual_Sales_Rep_Field_Sales').getRecordTypeId();
        Quota__c quota = new Quota__c();
        quota.Name = 'test';
        quota.Quater_Year__c = '2020';
        //quota.Month__c = 'June';
        quota.Quater__c = 'Q4';
        quota.Manager_Quota__c = 'Manager';
        quota.Assigned_To__c = user.ManagerId;
        //quota.Unique_Identifier_Per_Quater_Per_User__c = qt.Assigned_To__c + qt.Quater__c + qt.Quater_Year__c;
        //quota.Unique_Identifier_Per_Quater_Per_User__c = 'Sixth Quota inserted';
        quota.RecordTypeId = recordTypeIdQuota;      
        insert quota;
    }
}

Please mark this answer as the best if it will ressolve your issue.

Thanks!

Chirag