You need to sign in to do that
Don't have an account?
Renuka 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
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); } } }
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.
Please mark this answer as the best if it will ressolve your issue.
Thanks!
Chirag