I am trying to create a test class to move the apex trigger to production and i am getting only 42 % coverage and also i am unable to get the coverage option in developer console.

enter image description here

Here is my test class. Trigger works fine but dont know where am i going wrong. Please help me

PS: I am not a developer. Have minimum knowledge

public class PRclosedate {
    static void test1() {
       opportunity opp=new opportunity();
        opp.CloseDate= System.today();
        opp.id = '0063B000009YwB1QAK';

Here is my trigger:

trigger PRdateschange on Opportunity (after Update) {
  map<Id,Practice_Resource__c> mapPracResource = new map<Id,Practice_Resource__c> ();
   list<Practice_Resource__c> opptyPR=new list<Practice_Resource__c>();
    for(Opportunity oldOpp: Trigger.old)
      for(Opportunity newOpp: Trigger.new)
          if(oldOpp.id == newOpp.id && oldOpp.CloseDate != newOpp.CloseDate){
             for(Practice_Resource__c otm : [SELECT Id,Opportunity__c, Start_Date__c,End_Date__c  FROM Practice_Resource__c WHERE Opportunity__c IN : trigger.new]){
                 Practice_Resource__c PracRS =new Practice_Resource__c(); 
                Integer noOfDays =oldOpp.CloseDate.daysBetween(newOpp.CloseDate);
                 Date prStartDate = otm.Start_Date__c;
                 Date newPrStDate = prStartDate.addDays(noOfDays);
                 PracRS.Start_Date__c = newPrStDate;
                 PracRS.Id = otm.Id;
                 Date prEndDate = otm.End_Date__c;
                 Date newPrEndDate = prEndDate.addDays(noOfDays);
                 PracRS.End_Date__c = newPrEndDate;
                 update opptyPR;

1 Answer 1


You need to create a new temporary opportunity, not using an existing record. This includes creating the parent account, etc.

@isTest static void testMethod1() {
  Account a = new Account(Name='test');
  insert a;
  Opportunity o = new Opportunity(AccountId=a.Id, Amount=100.0, Name='Test', StageName='Prospecting');
  insert o;
  update o;
  // Here you should check output
  o = [SELECT Name, CloseDate FROM opportunity WHERE Id = :o.Id];
  System.assert('Some value', o.Name); // etc...

Without seeing your trigger, I have no idea why you're only at 42%. You'll need to check the logic to identify why you're only at 42%. Also, check your Setup > Apex Test Execution > Options settings, and make sure "Store Only Aggregated Code Coverage" is not checked, otherwise you won't see coverage from individual test runs.

Your trigger has a number of problems, including looping over all records exponentially, and performing repeated DML updates in a loop. Your trigger should look more like this:

trigger PRdateschange on Opportunity(after update) {
    Map<Id, Practice_Resource__c> resources = new Map<Id, Practice_Resource__c>();
    for(Practice_Resource__c record: [SELECT Opportunity__c FROM Practice_Resource__c WHERE Opportunity__c = :Trigger.new]) {
        resources.put(record.Opportunity__c, record);
    for(Integer i = 0, s = Trigger.new.size(); i < s; i++) {
        Opportunity oldOpp = Trigger.old[i], newOpp = Trigger.new[i];
        Practice_Resource__c resource = resources.get(newOpp.Id);
        if(resource == null) {
            resources.put(record.Id, resource = new Practice_Resource__c(Opportunity__c=newOpp.Id));
        Integer noOfDays = oldOpp.CloseDate.daysBetween(newOpp.CloseDate);
        Date prStartDate = resource.Start_Date__c == null? System.today(): resource.Start_Date__c;
        Date newPrStDate = prStartDate.addDays(noOfDays);
        Date prEndDate = resource.End_Date__c == null? System.today(): resource.End_Date__c;
        Date newPrEndDate = prEndDate.addDays(noOfDays);
        resource.Start_Date__c = newPrStDate;
        PracRS.End_Date__c = newPrEndDate;
    upsert resources.values();

Note that I'm making some guesses here, you'll need to fix the logic appropriately. This trigger logic should have 100% coverage given the above unit test.

  • I have unchecked the "Store Only Aggregated Code Coverage" option. Included trigger to my question
    – Nandy
    Commented Sep 4, 2020 at 13:43
  • @Nandy Your unit test demonstrated that your trigger logic was incorrect. See my edited answer.
    – sfdcfox
    Commented Sep 4, 2020 at 13:58
  • Thank you that worked. I made few changes to the logic. But i have another issue here. We have multiple line items for PR. And only one record is being updated. I need all the PR records to be updated. Can u help me please
    – Nandy
    Commented Sep 7, 2020 at 8:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .